String readFilePath = "/user/sousuo/data/search/cs/yqy/brandNameTxt.txt";
String writeFilePath = "/user/sousuo/data/search/cs/yqy/sparkResult";
String subject = "品牌相似度为1的结果已经出,请下载,链接为:";
String toMail = "xxx@cnsuning.com_xxxx@cnsuning.com_xxxx@cnsuning.com";
String fileContent = "xxxxxxxxx";
sendEmail(subject,toMail,fileContent);
}
public static void sendEmail(String subject, String toMail, String fileContent){
try {
String smtpFromMail = "xxxx"; //账号
String pwd = "xxxx"; //密码
int port = 25; //端口
String host = "xxxx"; // 设置邮件服务器主机名
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(smtpFromMail, subject));
String[] toMailList = toMail.split("_");
int len = toMailList.length;
for(int i=0;i<len;i++)
{
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(toMailList[i]));
}
message.setSubject(subject);
message.addHeader("charset", "UTF-8");
/*添加正文内容*/
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(fileContent);
contentPart.setHeader("Content-Type", "text/html; charset=UTF-8");
multipart.addBodyPart(contentPart);
/*添加附件*/
// File usFile = new File(file);
// MimeBodyPart fileBody = new MimeBodyPart();
// DataSource source = new FileDataSource(file);
// fileBody.setDataHandler(new DataHandler(source));
// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
// fileBody.setFileName("=?GBK?B?"+ enc.encode(usFile.getName().getBytes()) + "?=");
// multipart.addBodyPart(fileBody);
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, port, smtpFromMail, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}