发送邮件

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class EmailAlert {

private final static Log log = LogFactory.getLog(EmailAlert.class);

public static void sendEMail(boolean isRebuild,String filePath,String message)throws Exception{
Properties p= new Properties();
InputStream is = null;
try{
File propsFile = new File("/mail.properties");
if(!propsFile.exists()){
is = new FileInputStream(new File(filePath,"/mail.properties"));
}else{
is = new FileInputStream(propsFile);
}
if(is!=null)
p.load(is);
}catch (Exception e) {
throw new RuntimeException("load mail.properties error!",e);
}finally {
if(is!=null)
is.close();
}

String host = p.getProperty("mail.smtp.host");
String msgFrom = p.getProperty("mail.msgFrom");
String titile;
if(isRebuild)
titile = p.getProperty("mail.title1");
else
titile = p.getProperty("mail.title2");
String name = p.getProperty("mail.name");
String password = p.getProperty("mail.password");
String msgTo = p.getProperty("mail.msgTo");
String msgCC = p.getProperty("mail.msgCC");
String content = p.getProperty("mail.Content");
content+="\n"+message;

try{
// 建立会话
p.put("mail.smtp.auth", "true");
Session session = Session.getInstance(p);
Message msg = new MimeMessage(session); // 建立信息
//解析发件人
String[] msgTos = msgTo.split(";");
Address[] addresses = new Address[msgTos.length];
for (int i = 0; i < msgTos.length; i++) {
addresses[i] = new InternetAddress(msgTos[i],false);
}
//解析抄送人
String[] msgCCs = msgCC.split(";");
Address[] addresseCCs = new Address[msgCCs.length];
for (int i = 0; i < msgCCs.length; i++) {
addresseCCs[i] = new InternetAddress(msgCCs[i],false);
}
msg.setFrom(new InternetAddress(msgFrom)); // 发件人
msg.setRecipients(Message.RecipientType.TO, addresses); // 收件人
msg.addRecipients(RecipientType.CC, addresseCCs);//抄送
msg.setSentDate(new Date()); // 发送日期
msg.setSubject(titile); // 主题

Multipart mm = new MimeMultipart();

BodyPart mdp = new MimeBodyPart();
mdp.setContent(content, "text/html;charset=utf-8");
mm.addBodyPart(mdp);
msg.setContent(mm);
Transport tran = session.getTransport("smtp");
tran.connect(host, name, password);
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
tran.close();
log.info("索引重建或更新报警邮件发送成功!");
}catch (Exception e) {
throw new RuntimeException("EmailAlert: send mail exception!",e);
}
}
}


mail.properties
-------------------------------------------------------
# Properties file with mail-related settings, used for scheduled info emails.
# Applied by PropertyPlaceholderConfigurer from "applicationContext.xml".
# Targeted at system administrators, to avoid touching the context XML files.

#mail.host=mail.yihaodian.com

#mail.yihaodian.com
#mail.host=smtp.163.com

mail.transport.protocol=smtp
mail.smtp.port=25

mail.smtp.host=192.168.110.35
mail.msgFrom=sys@yihaodian.com
mail.name=sys@yihaodian.com
mail.password=heartbeat

mail.msgTo=yuqian@yihaodian.com;lilonghui@yihaodian.com;wumin@yihaodian.com
mail.msgCC=monitor@yihaodian.com

mail.fileName=Index Alert
mail.title1=\u7d22\u5f15\u91cd\u5efa\u51fa\u9519\u62a5\u8b66
mail.title2=\u7d22\u5f15\u66f4\u65b0\u51fa\u9519\u62a5\u8b66
mail.Content=\u6b64\u90ae\u4ef6\u4e3a\u7d22\u5f15\u91cd\u5efa\u66f4\u65b0\u7cfb\u7edf\u81ea\u52a8\u53d1\u51fa\u7684\u51fa\u9519\u62a5\u8b66\u90ae\u4ef6,\u8bf7\u52ff\u56de\u590d\u3002\u5982\u6709\u7591\u95ee\uff0c\u8bf7\u8054\u7cfb domain searchproject
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值