javaMail
来源:互联网 发布:淘宝客服旺旺怎么联系 编辑:程序博客网 时间:2024/06/05 19:40
javaMail发送邮件
jar包:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version></dependency><dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version></dependency>
工具类
package com.ufclub.util.mail;import com.ufclub.DTO.mail.FoxmailPropertiesDTO;import org.apache.commons.lang.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;/** * 发送邮件 * * @author zhoushixia * @date 2017-08-10 */public class SendMailUtil { private static final Logger logger = LoggerFactory.getLogger(SendMailUtil.class); private static boolean isInit = false; private static Properties prop; public static void setProperty(FoxmailPropertiesDTO foxmailPropertiesDTO){ if(!isInit){ logger.info("{0}邮箱初始化开始,发送者为{1},接受者为{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress()); prop = new Properties(); prop.setProperty("mail.host",foxmailPropertiesDTO.getMailHost()); prop.setProperty("mail.transport.protocol", "smtp"); prop.setProperty("mail.smtp.auth", "true"); isInit = true; logger.info("{0}邮箱初始化成功,发送者为{1},接受者为{2}",foxmailPropertiesDTO.getMailCode(),foxmailPropertiesDTO.getSenderAddress(),foxmailPropertiesDTO.getReceiveAddress()); } } public static void sendMail(FoxmailPropertiesDTO foxmailPropertiesDTO){ if(StringUtils.isBlank(foxmailPropertiesDTO.getReceiveAddress())){ logger.info("{0}发送邮件,邮件无接收者",foxmailPropertiesDTO.getMailCode()); return; } try{ setProperty(foxmailPropertiesDTO); //创建session Session session = Session.getInstance(prop); //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态 session.setDebug(true); //通过session得到transport对象 Transport ts = session.getTransport(); //使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。 ts.connect(foxmailPropertiesDTO.getMailHost(), foxmailPropertiesDTO.getSenderAddress(), foxmailPropertiesDTO.getSenderPassword()); //创建邮件 Message message = createSimpleMail(session,foxmailPropertiesDTO); //发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); }catch (Exception e) { logger.error("发送邮件异常"); e.printStackTrace(); } } public static MimeMessage createSimpleMail(Session session,FoxmailPropertiesDTO foxmailPropertiesDTO) throws Exception { //创建邮件对象 MimeMessage message = new MimeMessage(session); //指明邮件的发件人 message.setFrom(new InternetAddress(foxmailPropertiesDTO.getSenderAddress())); InternetAddress[] internetAddressTo = new InternetAddress().parse(foxmailPropertiesDTO.getReceiveAddress()); message.addRecipients(Message.RecipientType.TO, internetAddressTo); //邮件的文本内容 message.setContent(foxmailPropertiesDTO.getSendContent(), "text/html;charset=UTF-8"); //返回创建好的邮件对象 return message; }}
数据库截图
调用截图
阅读全文
0 0
- JavaMail
- JavaMail
- javaMail
- JavaMail
- javamail
- javamail
- javamail
- JAVAMAIL
- javamail
- JavaMail
- JAVAMail
- JavaMail
- javaMail
- javamail
- JavaMail
- javaMail
- javamail
- JavaMail
- context.Response.End()的用法和本质
- Mybatis 动态 SQL
- 某安全浏览器竟然也被查出高危漏洞?开源安全问题不容忽视
- C#人脸识别入门篇-STEP BY STEP人脸识别—静态照片人脸检测
- 【python StringIO模块】感受内存文件的速度优势
- javaMail
- spark之3:安装部署
- 2017第六次多校联合hdu6103
- SPRING 启动加载BEAN 的代码过程
- sql优化
- spark之2:原理介绍
- 《信号与系统学习笔记》—信号与系统(三)
- H
- 2017.8.11 亚瑟王 失败总结