JavaMail实现邮件发送工具类
来源:互联网 发布:查看文件权限 linux 编辑:程序博客网 时间:2024/05/28 23:19
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() {@Overrideprotected 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() {@Overrideprotected 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() {@Overrideprotected 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() {@Overrideprotected 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);} }
0 0
- JavaMail实现邮件发送工具类
- javamail发送邮件工具类
- JavaMail邮件发送 工具类
- javaMail发送邮件(工具类开发)
- javaMail发送邮件(工具类开发)
- JavaMail 实现邮件发送
- javamail实现邮件发送
- Javamail实现邮件发送
- JavaMail实现邮件发送
- javaMail实现邮件发送
- javamail实现发送邮件
- Javamail实现邮件发送
- javaMail实现发送邮件
- JAVAMAIL实现发送邮件
- javamail 实现邮件的发送
- JavaMail发送邮件功能实现
- javamail实现互联网邮件发送
- JavaMail发送邮件简单实现
- Swift、OC调用Framework
- BZOJ2242 [SDOI2011]计算器 题解&代码
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
- Queue、BlockingQueue以及用BlockingQueue实现生产者/消费者模式
- VB作业
- JavaMail实现邮件发送工具类
- TCP/IP详解学习笔记(12)-TCP的超时与重传
- 用C语言输入密码
- [hihoCoder 1150] 基站选址(几何题,中心点)
- composer使用
- 利用小闹钟计算1~100求和
- iOS runtime字典转模型
- TCP/IP详解学习笔记(13)-TCP坚持定时器,TCP保活定时器
- 如何保证service不被杀掉