这是我很早之前写的一个工具类,最近在整理自己所学的东西,无意中找到了,就拿出来与大家分享,代码如下:

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import javax.mail.BodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart; import org.apache.struts.util.MessageResources; import com.qq.connect.javabeans.tenpay.Address; //import com.sun.istack.internal.ByteArrayDataSource;
public class SendMails {
public int sendMail(String emailTitle, String content, String emailTo,MessageResources message) throws Exception {
System.out.println(smtpServer1);
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put( "mail.transport.protocol", "smpt");
props.put("mail.smtp.host", smtpServer1);
// 获得邮件会话对象
Session session = Session.getInstance(props,new SmtpAuthenticator1(fromMailAddress1, password1));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress1));// 发件人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));// 收件人
mimeMessage.setSubject(emailTitle);
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
StringBuffer content = new StringBuffer();
bodyPart.setDataHandler(new DataHandler(content.toString(),"text/html;charset=GBK"));// 网页格式
/** *************************************************** */
// BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
// FileDataSource fds = new FileDataSource("c:/boot.ini");
// attachBodyPart.setDataHandler(new DataHandler(fds));
// attachBodyPart.setFileName("=?GBK?B?"+ new sun.misc.BASE64Encoder().encode(fds.getName().getBytes()) + "?=");// 解决附件名中文乱码
// mp.addBodyPart(attachBodyPart);
/** *************************************************** */
// MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标
// byte[] bytes = readFile("E:/webcms/webcms/WebContent/images/bjx.jpg");
// ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,"application/octet-stream");
// imgBodyPart.setDataHandler(new DataHandler(fileds));
// imgBodyPart.setFileName("button.gif");
// imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"
// mp.addBodyPart(imgBodyPart);
/** *************************************************** */
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);//设置邮件内容对象
try {
Transport.send(mimeMessage);//发送邮件
// Transport transport=session.getTransport();
// transport.send(mimeMessage);
} catch (Exception e) {
// e.printStackTrace();
if(e.getMessage().contains("550 User not found")){
return 2;
}else{
return 3;
}
}
//system.out.println("SendEmail OK!!");
return 0;
}
public void setUsername1(String username1) {
this.username1 = username1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public void setSmtpServer1(String smtpServer1) {
this.smtpServer1 = smtpServer1;
} public void setFromMailAddress1(String fromMailAddress1) {
this.fromMailAddress1 = fromMailAddress1;
}
public static byte[] readFile(String file) {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
int bytesRead;
byte buffer[] = new byte[1024 * 1024];
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bos.toByteArray();
}
}
/**
* Smtp认证
*/
class SmtpAuthenticator1 extends Authenticator {
String username = null;
String password = null; // SMTP身份验证
public SmtpAuthenticator1(String username, String password) {
this.username = username;
this.password = password;
System.out.println(username+"::::::"+password);
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
} }

使用java向邮箱发送邮件的更多相关文章

  1. java实现邮箱发送邮件功能

    邮箱验证是一个很常见的功能了,基本上每个网站都会用的到,java也有专门的jar来处理邮件发送等服务,这里只是简单的实现一下发送邮件的功能,具体jar包就不再提供了,我会把所有需要引用的包都贴出来,方 ...

  2. java实现邮箱发送邮件

    第一步:封装发件人账号密码 import javax.mail.Authenticator;import javax.mail.PasswordAuthentication; /** * 发件人账号密 ...

  3. Java实现网易企业邮箱发送邮件

    最近项目需要用网易企业邮箱发送邮件,特意来将实现过程记录一下: maven导入jar包 <!-- javax.mai 核心包 --> <dependency> <grou ...

  4. 基于java mail实现简单的QQ邮箱发送邮件

    刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...

  5. 使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response

    使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.Messagin ...

  6. Java实现邮箱验证

    Java实现邮箱验证 JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口.它是Sun发布的用来处理email的API.它可以方便地执行一些常用的邮件传输.我们可以基于JavaMail开 ...

  7. 基于JavaMail向邮箱发送邮件

    参考:http://blog.csdn.net/ghsau/article/details/17839983 http://blog.csdn.net/never_cxb/article/detail ...

  8. Java注册帐号邮箱激活验证实现

    Java注册帐号邮箱激活验证实现 1.需要加入发送邮件的mail.jar: http://www.oracle.com/technetwork/java/javamail/index-138643.h ...

  9. 通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)

    package server; import java.io.*; import java.net.*; import java.sql.Time; import java.util.Scanner; ...

随机推荐

  1. XP访问WIN10共享打印机提示错误:操作无法完成,拒绝访问

    XP系统添加打印机--连接到此计算机的本地打印机(取消自动检测)--创建新端口(LOCAL port)----输入端口名\\计算机名\打印机名.(例如:\\adubei\\HP lasjet 1020 ...

  2. Prototype-based programming

    Prototype-based programming is a style of object-oriented programming in which behaviour reuse (know ...

  3. 如何解决zabbix中自定义监控mysql因密码造成的 Warning

    1.--show-warnings=false 在指定mysql命令获取参数时,指定不获取 Warning.不过亲测这个方法不是很有效 例如: mysql -uroot -p123 --show-wa ...

  4. OO第一单元总结__多项式求导问题

    作业一.含幂函数的简单多项式的求导 (1)基于度量的程序结构分析 1. 统计信息图: 2. 结构信息图: 3. 复杂度分析 基本复杂度(Essential Complexity (ev(G)).模块设 ...

  5. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

  6. 【codeforces 757E】Bash Plays with Functions

    [题目链接]:http://codeforces.com/problemset/problem/757/E [题意] 给你q个询问; 每个询问包含r和n; 让你输出f[r][n]; 这里f[0][n] ...

  7. TCL命令(事务控制)

     确认提交DML操作:commit;     撤销DML操作:rollback;         提示:rollback撤销的是与上一个commit之间          所做的DML操作.注意:仅对 ...

  8. Methods and systems for sharing common job information

    Apparatus and methods are provided for utilizing a plurality of processing units. A method comprises ...

  9. CSS学习(五)

    导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜单. 导航栏=链接列表 作为标准的HTML基础一个导航栏是必须的.在我们的例子中我们将建立一个 ...

  10. POJ 2906 数学期望

    开始时直接设了一个状态,dp[i][j]为发现i种bug,j个系统有bug的期望天数.但很错误,没能转移下去.... 看了题解,设状态dp[i][j]为已发现i种bug,j个系统有bug,到完成目标状 ...