发送邮件

通用的邮件的属性可以配置到proper中 方便修改

mail.encoding=UTF-8
mail.host=10.2.16.191
mail.port=25
#########################
mail.auth=false
 ###发件人邮箱地址###
mail.from=pi_it@ehuatai.com
###SMTP服务器地址###
mail.smtpServer=smtp.sina.com
###登录SMTP服务器的用户名###
check.mail.username=pi_it@ehuatai.com
###登录SMTP服务器的用户密码###
check.mail.password=Huatai123
###邮件主题###
mail.subject=\u4E3B\u6570\u636E\u4E2D\u5FC3\u5F02\u5E38\u6570\u636E>>>>>>> .r7053

package com.ssh.common.util;



import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;


import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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.apache.log4j.Logger;


/**
 * @description 邮件发送工具类
 * @author sjb
 * @date 2014-4-29
 * @version 1.0
 */
public class SimpleMailSender {
private static Logger log=Logger.getLogger(SimpleMailSender.class);
//发件人邮箱地址
    public static final String FROM=PropertyFactory.getProperty("mail.from");
    //SMTP服务器地址
    public static final String SMTPSERVER=PropertyFactory.getProperty("mail.smtpServer"); 
    //登录SMTP服务器的用户名
    public static final String USERNAME=PropertyFactory.getProperty("check.mail.username");
    //登录SMTP服务器的密码
    public static final String PASSWORD=PropertyFactory.getProperty("check.mail.password") ;
    /*//邮件主题
    public static final String SUBJECT=PropertyFactory.getProperty("mail.subject"); */
    //地址
    public static final String HOST=PropertyFactory.getProperty("mail.host");
    //端口
    public static final String mailPort=PropertyFactory.getProperty("mail.port");
/**
* 以文本格式发送邮件

* @param mailInfo
*            待发送的邮件的信息
* @throws Exception
*/
public static void sendTextMail(MailSenderInfo mailInfo){
// 判断是否需要身份认证
try {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session
.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
log.info("开始发送邮件邮件地址为:"+mailInfo.getToAddress());
String [] mailList = mailInfo.getToAddress().split(",");
for(String mail :mailList){
// 创建邮件的接收者地址,并设置到邮件消息中
/*Address to = new InternetAddress(mail);
mailMessage.setRecipient(Message.RecipientType.TO, to);
Transport.send(mailMessage);*/
}
} catch (Exception e) {
log.info(e);
e.printStackTrace();
}
}


/**
* 以HTML格式发送邮件

* @param mailInfo
*            待发送的邮件信息
* @throws Exception
*/
public void sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// 如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),
mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session
.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
//mailMessage.setFrom(from);
//设置自定义发件人昵称
String nick="";
 try {
  nick=javax.mail.internet.MimeUtility.encodeText("华泰个险理赔服务");
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 } 
mailMessage.setFrom(new InternetAddress(nick+" <"+from+">"));


// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
}
public static boolean sendData(MailSenderInfo mailSenderInfo){

// 设置邮件发送参数
MailSenderInfo  mailInfo=new MailSenderInfo();
mailInfo.setMailServerHost(HOST);
mailInfo.setMailServerPort(mailPort);
// 是否需要验证用户名和密码
String auth = PropertyFactory.getProperty("all.mail");
Boolean mailAuth = Boolean.valueOf(auth);
mailInfo.setValidate(mailAuth.booleanValue());
// 登录邮箱用户名和密码
mailInfo.setUserName(USERNAME);
mailInfo.setPassword(PASSWORD);// 您的邮箱密码
// 发送邮件邮箱
mailInfo.setFromAddress(USERNAME);
// 邮件主题
mailInfo.setSubject(mailSenderInfo.getSubject());

// 邮件内容
mailInfo.setContent(mailSenderInfo.getContent());
String [] mailList = mailSenderInfo.getToAddress().split(",");
String [] supermailList = PropertyFactory.getProperty("all.mail").split(",");
try {
for(String mailadd:supermailList){
mailInfo.setToAddress(mailadd);
SimpleMailSender.sendTextMail(mailInfo);
}
for(String mail :mailList){
//接收人邮箱
mailInfo.setToAddress(mail);
//SimpleMailSender.sendTextMail(mailInfo);
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}


/*
* public static void main(String[] args) { // 这个类主要是设置邮件 MailSenderInfo
* mailInfo = new MailSenderInfo();
* mailInfo.setMailServerHost("smtp.163.com");
* mailInfo.setMailServerPort("25"); mailInfo.setValidate(true);
* mailInfo.setUserName("han2000lei@163.com");
* mailInfo.setPassword("**********");// 您的邮箱密码
* mailInfo.setFromAddress("han2000lei@163.com");
* mailInfo.setToAddress("han2000lei@163.com");
* mailInfo.setSubject("设置邮箱标题 如http://www.guihua.org 中国桂花网");
* mailInfo.setContent("设置邮箱内容 如http://www.guihua.org 中国桂花网 是中国最大桂花网站==");
* // 这个类主要来发送邮件 SimpleMailSender sms = new SimpleMailSender();
* sms.sendTextMail(mailInfo);// 发送文体格式 sms.sendHtmlMail(mailInfo);//
* 发送html格式 }
*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值