JavaMail发送邮件工具类(不带附件)

/**
* 2017年11月1日下午12:01:34
*/
package com.jjmc.dcl.util;

import java.util.Date;
import java.util.Properties;

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;

/**
*
* @author huangtao
* 2017年11月1日下午12:01:34
* dclTask
* @parameter
* TODO
* 发送邮件
*
*
*
*/
public class EmailUtil {
//日志
private static final String DEBUG_PREFIX = "[EmailUtil]";

private static final String INFO_PREFIX = "<EmailUtil>";

private static final String ERROR_PREFIX = "EmailUtil->";

private static Logger logger = Logger.getLogger(EmailUtil.class);


//发送邮件
public static void sendEmail(String subject,String content) throws Exception{
logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" sendEmail begin");
logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" sendEmail begin");
try {
//从配置文件中读取发送邮件配置(from)
String host = ResourceUtil.getConf("dcltask.email.host");
String protocol = ResourceUtil.getConf("dcltask.email.protocol");
int port = Integer.valueOf(ResourceUtil.getConf("dcltask.email.port"));
String username = ResourceUtil.getConf("dcltask.email.username");
// String password = ResourceUtil.getConf("dcltask.email.authcode");//需要用授权码登录,不用密码
String password = ResourceUtil.getConf("dcltask.email.passsword");
String auth = ResourceUtil.getConf("dcltask.email.auth");
int connectionTimeout = Integer.valueOf(ResourceUtil.getConf("dcltask.email.connection.timeout"));
int timeout = Integer.valueOf(ResourceUtil.getConf("dcltask.email.timeout"));

//从配置文件中读取发送对象(to),多个用户用;来分隔
String customerTo = ResourceUtil.getConf("dcltask.email.customer.to");
String[] customerToArray = customerTo.split(";");
int receiverCountTo = customerToArray.length;
//从配置文件中读取发送对象(CC),多个用户用;来分隔
String customerCc = ResourceUtil.getConf("dcltask.email.customer.cc");
String[] customerCcArray = customerCc.split(";");
int receiverCountCc = customerCcArray.length;

Properties props = new Properties();
//用户名
props.put("username",username);
//密码
props.put("password",password);
//协议
props.put("mail.transport.protocol", protocol);
//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
//需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", auth);
//端口
props.put("mail.smtp.port", port);
//连接
props.put("mail.smtp.connectiontimeout", connectionTimeout);
//超时时间
props.put("mail.smtp.timeout", timeout);
//设置是否使用ssl安全连接
props.put("mail.smtp.ssl.enable", "true");
//用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
//有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
//用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(false);
//用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
//加载发件人地址
message.setFrom(new InternetAddress(username));

//加载收件人地址
InternetAddress[] addressTo = new InternetAddress[receiverCountTo];
for (int i = 0; i < receiverCountTo; i++) {
addressTo[i] = new InternetAddress(customerToArray[i]);
}
message.addRecipients(Message.RecipientType.TO,addressTo);
//加载抄送人
InternetAddress[] addressCc = new InternetAddress[receiverCountCc];
for (int i = 0; i < receiverCountCc; i++) {
addressCc[i] = new InternetAddress(customerCcArray[i]);
}
message.addRecipients(Message.RecipientType.CC,addressCc);

message.addHeader("charset", "UTF-8");
//加载标题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
//设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
//设置为html格式的邮件
contentPart.setContent(content,"text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
//添加附件
// BodyPart messageBodyPart= new MimeBodyPart();
//添加附件的标题
//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
// messageBodyPart.setFileName("=?GBK?B?"+enc.encode(affixName.getBytes())+"?=");
// multipart.addBodyPart(messageBodyPart);

//将multipart对象放到message中
message.setContent(multipart);
//保存邮件
message.saveChanges();
//发送邮件
Transport transport = session.getTransport(protocol);
//连接服务器的邮箱
transport.connect(host, port, username, password);
//把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();

} catch (Exception e) {
logger.error(ERROR_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"execute sendEmail occur error,the exception is:"+e);
throw e;
}

logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" sendEmail end");
logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" sendEmail end");
}

/**
* 邮件内容判空
*
* 2017年11月4日上午7:40:08
* @param value
* @return
* @parameter
* String
*
*/
public static String isEmptyEmailContent(String value) throws Exception{
logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" isEmptyEmailContent end");
logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" isEmptyEmailContent end");
try {
String data = null;
if(value != null){
data = value;
}else{
data = "0";
}
logger.debug(DEBUG_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" isEmptyEmailContent end");
logger.info(INFO_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+" isEmptyEmailContent end");
return data;
} catch (Exception e) {
logger.error(ERROR_PREFIX+"current time is:"+DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss")+"execute isEmptyEmailContent occur error,the exception is:"+e);
throw e;
}

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值