009-MailUtils工具类模板
版本一:JavaMail的一个工具类
package ${enclosing_package};
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.sun.mail.util.MailSSLSocketFactory;
public class ${primary_type_name} {
//email:邮件发给谁 subject:主题 emailMsg:邮件的内容
public static void sendMail(String email, String subject, String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");//发邮件的协议
props.setProperty("mail.host", "smtp.126.com");//发送邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
//开启SSL加密 ,QQ邮箱必须开启ssl加密才可以。
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//发邮件的账号的验证 12345为授权码,并非密码。tom可以不用加上@126.com
return new PasswordAuthentication("tom", "12345");
}
};
//这个是邮箱服务器的session 和之前学的session不是同一个session
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("tom@126.com")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
message.setSubject(subject);//邮件的主题
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}
一个JavaMail的例子
package com.test.jobs; import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Properties; import javax.annotation.Resource;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory;
import com.test.bos.dao.IWorkbillDao;
import com.test.bos.domain.Workbill; /**
* 发送邮件的作业
* @author zhaoqx
*
*/
public class MailJob {
@Resource
private IWorkbillDao workbillDao;
//属性可以通过页面提交获得,也可以通过spring的配置文件注入
private String username; //发件人邮箱
private String password; //发邮件的账号的验证 12345为授权码,并非密码
private String smtpServer;//发送邮件的服务器地址
private String protocol;//发送右键的协议 public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getSmtpServer() {
return smtpServer;
} public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
} public String getProtocol() {
return protocol;
} public void setProtocol(String protocol) {
this.protocol = protocol;
} public void execute() {
System.out.println("要发邮件了。。。");
try {
//查询工单类型为新单的所有工单
List<Workbill> list = workbillDao.findAll();
if(null != list && list.size() > 0){
// 创建一个程序与邮件服务器会话对象 Session
final Properties mailProps = new Properties();
mailProps.put("mail.transport.protocol", this.getProtocol()); //发送邮件的协议
mailProps.put("mail.host", this.getSmtpServer()); //发送邮件的服务器地址
mailProps.put("mail.smtp.auth", "true"); // 指定验证为true
mailProps.put("mail.username", this.getUsername());
mailProps.put("mail.password", this.getPassword()); //开启SSL加密 ,QQ邮箱必须开启ssl加密才可以。
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) { e.printStackTrace();
}
sf.setTrustAllHosts(true);
mailProps.put("mail.smtp.ssl.enable", "true");
mailProps.put("mail.smtp.ssl.socketFactory", sf); // 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = mailProps.getProperty("mail.username");
String password = mailProps.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(mailProps, authenticator);
for(Workbill workbill : list){
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));
message.setFrom(from);
// 设置收件人
InternetAddress to = new InternetAddress("151956669@qq.com");
//设置发送方式与接收者
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject("激活邮件");
// 设置邮件的内容体
message.setContent(workbill.toString(), "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
spring的注入方式如下:
<bean name="myJob" class="com.test.jobs.MailJob">
<property name="username" value="tom@126.com"/>
<property name="password" value="123456"/>
<property name="smtpServer" value="smtp.126.com"/>
<property name="protocol" value="SMTP"/>
</bean>
009-MailUtils工具类模板的更多相关文章
- JDBC 工具类模板c3p0
JDBC 工具类模板 package com.itheima.sh.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import ja ...
- 033-JsonUtils 工具类模板
模板一:使用的是jackson package cn.e3mall.common.utils; import java.util.List; import com.fasterxml.jackson. ...
- 031-CookieUtils 工具类模板
模板一: package com.leo.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLD ...
- Swift - 简单封装一个工具类模板
创建模板类(封装一个类) 例1:新建一个名字叫做 Product 的类 Product.swift File 的内容 class Product { var name: String var desc ...
- 022-pinyin4j工具类模板
模板一 package ${enclosing_package}; import java.util.Arrays; import net.sourceforge.pinyin4j.PinyinHel ...
- 032-IDUtils 工具类模板
模板一: package cn.e3mall.common.utils; import java.util.Random; /** * 各种id生成策略 * @title:IDUtils * @des ...
- DBCP连接池工具类模板
package ${enclosing_package}; import java.io.InputStream; import java.sql.Connection; import java.sq ...
- 004-C3P0连接池工具类模板
package ${enclosing_package}; import java.sql.Connection; import java.sql.ResultSet; import java.sql ...
- 013-PaymentUtils工具类模板
package ${enclosing_package}; import java.io.UnsupportedEncodingException; import java.security.Mess ...
随机推荐
- 风尘浪子 只要肯努力,梦想总有一天会实现 WF工作流与Web服务的相互调用 —— 通过Web服务调用Workflow工作流(开发持久化工作流) _转
如果你曾经负责开发企业ERP系统或者OA系统,工作流对你来说一定并不陌生.工作流(Workflow)是对工作流程及其各操作步骤之间业务规则 的抽象.概括.描述.工作流要解决的主要问题是:为实现某个业务 ...
- 【Linux】GCC编译器
[简介] GCC是Linux下的编译工具集,是GNU Compiler Collection的缩写,包含gcc g++ 等编译器.GCC工具集不仅能编译C/C++语言,其他例如Object-c.Pas ...
- 修改mysql时区的三种方法
方法一:通过mysql命令行模式下动态修改 1.1 查看mysql当前时间,当前时区 > select curtime(); #或select now()也可以 +-----------+ | ...
- Java网络编程のOSI
我们可以把客户机和远程服务器理解为主机A和主机B,用户和主机A可以通过主机A中的应用程序进行交互,主机A与主机B之间交互则是通过计算机网络通信进行的. 网络中每台机器称为节点.大多数节点是计算机,此外 ...
- java学习(三)数组
一维数组的定义格式: int[] a; //定义一个int类型的数组a变量 int a[]; //定义一个int类型的a数组变量 初始化一个int类型的数组 int[] arr = new i ...
- Tomcat 警告:consider increasing the maximum size of the cache
最近在Tomcat8上导入原本Tomcat6的项目,报了以下错误:Tomcat 警告:consider increasing the maximum size of the cache. 这是因为to ...
- oracle数据库中修改已存在数据的字段
在oracle中,如果已经存在的数据的某些列,假如要更换类型的话,有的时候是比较麻烦的, 会出现:ORA-01439: column to be modified must be empty to c ...
- [VSTO] warning CS0467 解决方案
warning CS0467: Ambiguity between method 'Microsoft.Office.Interop.Word._Document.Close(ref object, ...
- windows中卸载Jenkins
背景: 之前安装的Jenkins没有pipeline选项,可能是之前安装时没有选择建议插件.后面使用最新版本时还是没有插件 解决: 卸载Jenkins,删除掉C:\Users\用户名\.jenkins ...
- Apache 中httpd.conf文件配置详解(转载)
httpd.conf文件配置详解 Apache的基本设置主要交由httpd.conf来设定管理,我们要修改Apache的相关设定,主要还是通过修改httpd.cong来实现.下面让我们来看看htt ...