package Util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import Joyin.Idc.Core.Beans.Util.GlobalConfig;
public class SendEmail {
private String emailSubjectTxt = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime()) + "理财兑付数据";
public void postMail(String[] recipients, String message,
String files){
try {
boolean debug = true;
Properties props = new Properties();
String SMTP_HOST_NAME = GlobalConfig.getProperty("/file.properties",
"SMTP_HOST_NAME");
String SMTP_PORT = GlobalConfig.getProperty("/file.properties",
"SMTP_PORT");
String SMTP_AUTH_USER = GlobalConfig.getProperty("/file.properties",
"SMTP_AUTH_USER");
String SMTP_AUTH_PWD = GlobalConfig.getProperty("/file.properties",
"SMTP_AUTH_PWD");
props.setProperty("mail.host", SMTP_HOST_NAME);
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.auth", "true");
//props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.port",SMTP_PORT);
props.setProperty("mail.smtp.socketFactory.port",SMTP_PORT);
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
Transport transport = session.getTransport("smtp");//"smtps"
transport.connect(SMTP_HOST_NAME,SMTP_AUTH_USER,SMTP_AUTH_PWD);
MimeMessage msg = new MimeMessage(session);
msg.setSubject(emailSubjectTxt);
System.out.print("发送邮箱:");
for(int i = 0 ;i< recipients.length; i ++){
System.out.print(recipients[i]+" ");
}
//--------发送地址------
msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
msg.addRecipient(Message.RecipientType.TO, addressTo[i]);
}
MimeMultipart mp = new MimeMultipart("mixed");
msg.setContent(mp);
msg.saveChanges();
//------文件发送--------
if (files != null && files.length() > 0) { //邮件附件
System.out.println("需要发送附件!");
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(new File(files));
mbp.setDataHandler(new DataHandler(fds));
try {
mbp.setFileName(java.net.URLDecoder.decode(MimeUtility.encodeWord(files.substring(files.lastIndexOf("/")+1)), "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.addBodyPart(mbp);
}
MimeBodyPart ms = new MimeBodyPart();
ms.setContent(message, "text/plain;charset=UTF-8");
mp.addBodyPart(ms);
// msg.setContent(mp);
System.out.println("开始发送邮件~");
Transport.send(msg);
System.out.println("发送邮件成功~");
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
String SMTP_AUTH_USER = GlobalConfig.getProperty("/file.properties",
"SMTP_AUTH_USER");
String SMTP_AUTH_PWD = GlobalConfig.getProperty("/file.properties",
"SMTP_AUTH_PWD");
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}