JavaEE开源博客-阿Q ~用心做事,诚信做人!以德为本,兼顾各方!细心观察,联系推理! ~遵守规范->重构->可复用代码->可扩展性 *大道至简* ~时间是挤出来的,要成功,你就必须在最短的时间内采取最大量的行动!坚持到底,看到的就是胜利!再低下、再简单、再不愿意做的事情,你也要把它当成自己的事业做好!以马内利,阿们! |
|
package org.javaee.mail; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @author zhanjia * */ public class SendMail { private final static Log log = LogFactory.getLog(SendMail.class); /** * @param args */ public static void main(String[] args) { String to1 = "681519@qq.com,zhanji@163.com,279679@qq.com"; // 多个接收邮件由逗号隔开 String from = "zhan@163.com"; // 发送人邮箱 String host = "smtp.163.com"; // 发送人邮箱服务器主机名 String user = "zhan"; // 发送人邮箱用户名 String password = "jsj"; // 发送人邮箱密码 String protocol = "smtp"; // 发送协议 String personal = "山姆"; // 发件人名称 String subject = "最优邮件发送程序测试..."; // 邮件主题 String content = "你好, 这是我的测试邮件内容!!"; // 邮件内容 String[] accessories = { "E:/a.txt", "E:/b.txt", "E:/c.txt" }; // 附件路径 SendMail obj = new SendMail(); obj.sendMail(from, personal, user, password, subject, content, accessories, to1, host, protocol); // 发送 log.info("第一次发送完成"); log.info("");
try { InternetAddress[] to2 = { new InternetAddress("681519@qq.com"), new InternetAddress("zhanji@163.com"), new InternetAddress("279679@qq.com") }; // 接收邮件数组 obj.sendMail(from, personal, user, password, subject, content, accessories, to2, host, protocol); // 发送 } catch (AddressException e) { e.printStackTrace(); } log.info("第二次发送完成"); } public void sendMail(String from, String personal, String user, String password, String subject, String content, String[] accessories, Object to, String host, String protocol) { try { // 初始化邮件发送属性 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); // props.put("mail.bebug", "true"); // 初始化发件人地址及个人姓名信息 Address fromAddress = null; if (personal == null || "".equals(personal.trim())) { fromAddress = new InternetAddress(from); } else { fromAddress = new InternetAddress(from, personal, "GBK"); } // 初始化接收人邮件地址信息 Address[] address = null; if (to != null) { if (to instanceof String) { log .info("--------------to is instanceof String-------------"); address = InternetAddress.parse((String) to); } else if (to instanceof InternetAddress[]) { log .info("-----------to is instanceof InternetAddress[]-----"); address = (InternetAddress[]) to; } } log.info("---------------------获取session-------------------"); // 获取默认session Session sendMailSession = Session.getDefaultInstance(props, null); // sendMailSession.setDebug(true); // 获取指定协议的传输对象 Transport transport = sendMailSession.getTransport(protocol); log.info("---------------------创建消息对象-------------------"); // 根据邮件会话创建消息对象 MimeMessage msg = new MimeMessage(sendMailSession); msg.setFrom(fromAddress); // 设置邮件发件人 msg.setRecipients(Message.RecipientType.TO, address); // 设置邮件接收人 msg.setSubject(subject, "GBK"); // 设置邮件主题 msg.setHeader("X-Mailer", "msgsend"); // 头部信息 msg.setSentDate(new Date()); // 发送日期 log.info("---------------------创建Multipart-------------------"); // 创建多部分邮件发送对象 Multipart mp = new MimeMultipart(); // 邮件正文内容 MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(content, "GBK"); mp.addBodyPart(mbp1); // 添加附件 for (int i = 0; i < accessories.length; i++) { MimeBodyPart mbp2 = new MimeBodyPart(); FileDataSource fds = new FileDataSource(accessories[i]); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); mp.addBodyPart(mbp2); }
// 将多部分的邮件内容添加到Message中, 最终的邮件内容都要放到Message中来发送 msg.setContent(mp); log.info("--------------------正式发送--------------------"); // 发送人邮件连接并发送邮件 transport.connect(host, user, password); transport.sendMessage(msg, msg .getRecipients(Message.RecipientType.TO)); transport.close(); log.info("恭喜, 邮件发送成功!"); } catch (Exception ex) { ex.printStackTrace(); } } }
|