JavaMail实现邮件发送工具类
2016-04-14 17:17
555 查看
package org.nercita.bcp.util; import java.io.IOException; 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.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.nercita.bcp.system.util.CustomSystemException; import org.nercita.core.utils.PropertyUtils; import org.nercita.core.utils.UUIDUtils; /** * 邮件发送工具类 * @author zhangwenchao * */ public class MailUtils { public static Properties sysProps = null; static{ try { sysProps = PropertyUtils.loadProperties("application.properties"); } catch (IOException e) { e.printStackTrace(); throw new CustomSystemException("1002", "属性文件加载异常"); } } public static final String ENCODEING = sysProps.getProperty("ENCODEING"); //编码 public static final String DN = sysProps.getProperty("DN"); //域名 private static final String PROTOCOL = sysProps.getProperty("PROTOCOL");// 协议 private static final String HOST = sysProps.getProperty("HOST"); // 服务器地址 private static final String FROM = sysProps.getProperty("FROM"); // 发件人的邮箱 private static final String USERNAME = sysProps.getProperty("USERNAME"); // 账号 private static final String PASSWORD = sysProps.getProperty("PASSWORD");; // 密码 /** * 发送简单邮件 */ public static void sendMail(String subject, String to, String content){ //1.设置邮件环境 Properties props = new Properties(); props.setProperty("mail.transport.protocol", PROTOCOL); props.setProperty("mail.host", HOST); props.setProperty("mail.smtp.auth", "true"); //2.获取session连接对象 Session session = Session.getInstance(props,new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME,PASSWORD); } }); session.setDebug(true); //3. 创建邮件 Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(FROM)); //设置发件人 message.setSubject(subject); //标题 message.addRecipient(RecipientType.TO, new InternetAddress(to)); //收件人 message.setSentDate(new Date()); //时间 message.setText(content); //正文 //3. 发送邮件 Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new CustomSystemException("1001", "邮件发送异常"); } } /** * 发送HTML邮件 */ public static void sendHTMLMail(String subject, String to, String content){ //1.设置邮件环境 Properties props = new Properties(); props.setProperty("mail.transport.protocol", PROTOCOL); props.setProperty("mail.host", HOST); props.setProperty("mail.smtp.auth", "true"); //2.获取session连接对象 Session session = Session.getInstance(props,new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME,PASSWORD); } }); session.setDebug(true); try { //3. 创建邮件 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(FROM)); //设置发件人 message.setSubject(subject); //标题 message.addRecipient(RecipientType.TO, new InternetAddress(to)); //收件人 message.setSentDate(new Date()); //时间 MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(content,"text/html; charset=UTF-8"); MimeMultipart mimeMultipart = new MimeMultipart(); mimeMultipart.addBodyPart(bodyPart); message.setContent(mimeMultipart); message.saveChanges(); //设置邮件内容与消息头一至 //3. 发送邮件 Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new CustomSystemException("1001", "邮件发送异常"); } } /** * 发送含有内嵌图像资源的邮件 */ public static void sendRelatedMail(String subject, String to, String content, String... relatedFilePath){ //1.设置邮件环境 Properties props = new Properties(); props.setProperty("mail.transport.protocol", PROTOCOL); props.setProperty("mail.host", HOST); props.setProperty("mail.smtp.auth", "true"); //2.获取session连接对象 Session session = Session.getInstance(props,new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME,PASSWORD); } }); session.setDebug(true); try { //3. 创建邮件 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(FROM)); //设置发件人 message.setSubject(subject); //标题 message.addRecipient(RecipientType.TO, new InternetAddress(to)); //收件人 message.setSentDate(new Date()); //时间 //html文本信息 MimeBodyPart bodyPart1 = new MimeBodyPart(); bodyPart1.setContent(content+"<br /><img src=cid:img_1 />", "text/html; charset=UTF-8"); //图像信息 MimeBodyPart bodyPart2 = new MimeBodyPart(); bodyPart2.setDataHandler(new DataHandler(new FileDataSource(relatedFilePath[0]))); bodyPart2.setContentID("img_1"); //组装 MimeMultipart mimeMultipart = new MimeMultipart(); mimeMultipart.addBodyPart(bodyPart1); mimeMultipart.addBodyPart(bodyPart2); message.setContent(mimeMultipart); //合并消息 message.saveChanges(); //设置邮件内容与消息头一至 //3. 发送邮件 Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new CustomSystemException("1001", "邮件发送异常"); } } /** * 发送含有内嵌图像资源的邮件 */ public static void sendAttachMail(String subject, String to, String content, String... attachFilePath){ //1.设置邮件环境 Properties props = new Properties(); props.setProperty("mail.transport.protocol", PROTOCOL); props.setProperty("mail.host", HOST); props.setProperty("mail.smtp.auth", "true"); //2.获取session连接对象 Session session = Session.getInstance(props,new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USERNAME,PASSWORD); } }); session.setDebug(true); try { //3. 创建邮件 Message message = new MimeMessage(session); message.setFrom(new InternetAddress(FROM)); //设置发件人 message.setSubject(subject); //标题 message.addRecipient(RecipientType.TO, new InternetAddress(to)); //收件人 message.setSentDate(new Date()); //时间 //定义Html文本信息 MimeBodyPart bodyPart1 = new MimeBodyPart(); bodyPart1.setContent(content, "text/html; charset=UTF-8"); //附件 MimeBodyPart bodyPart2 = new MimeBodyPart(); bodyPart2.setDataHandler(new DataHandler(new FileDataSource(attachFilePath[0]))); bodyPart2.setFileName(attachFilePath[0].substring(attachFilePath[0].lastIndexOf("\\")+1)); //组装 MimeMultipart mimeMultipart = new MimeMultipart(); mimeMultipart.addBodyPart(bodyPart1); mimeMultipart.addBodyPart(bodyPart2); message.setContent(mimeMultipart); //合并消息 message.saveChanges(); //设置邮件内容与消息头一至 //3. 发送邮件 Transport.send(message); } catch (MessagingException e) { e.printStackTrace(); throw new CustomSystemException("1001", "邮件发送异常"); } } public static void main(String[] args) { String activationCode = UUIDUtils.getUUID(); activationCode = "5ff6681cce0649158b4b3a87cd944050"; String msg="<h3>点击下面链接激活账户</h3><h4><a href='"+MailUtils.DN+"/system/user/activate?code="+activationCode+"'>"+MailUtils.DN+"/system/user/activate?code="+activationCode+"</a><h4>"; MailUtils.sendHTMLMail("官方账户注册激活邮件", "zhangwenchao729@163.com", msg); } }
相关文章推荐
- Java高级之Exchanger
- 1.Java之HelloWorld
- maven 工程启动找不到 Spring ContextLoaderListener 的解决办法
- java毕向东听课笔记14(线程3)
- Java对xml文件进行读写的方法
- java学习---new的对象怎么被内存回收
- MySQL通信协议栈Java实现-(1)数据类型
- MySQL通信协议栈Java实现-(1)数据类型
- Spring 学习笔记(1)注入Injection_type
- Mac 安装 eclipse
- Java Volatile 关键字
- Android Eclipse 开发引用百度地图定位
- java中的异常
- Eclipse使用技巧 - 5. Eclipse代码自动补全失效解决办法
- jdk8新特性
- json简介及在Java中的应用
- 修改java类后,ireport无法立刻生效,解决办法
- JAVA多线程学习笔记
- Spring中的Mybatis基础
- java开发中的23种模式详解