import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 javax.mail.internet.MimeUtility;
import com.sun.mail.util.MailSSLSocketFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
public class TestEmailSend {
public static void main(String[] args) {
String email_filepath =“D:\order1622515607844_823278.pdf”;
//sendEmail(xxxx@xxx.com", “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
sendEmail(“xxxx@xxxx.cn”, “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
// sendEmail(“xxxx@qq.com”, “测试供应商”, “测试供应商”, “测试供应商”, email_filepath);
}
public static void sendEmail(String email_addr,String email_receiver_name,String email_title,String email_content,String email_filepath){
System.out.println(“sendEmail__start_____”);
System.out.println(“email_addr_____”+email_addr);
System.out.println(“email_receiver_name_____”+email_receiver_name);
System.out.println(“email_title_____”+email_title);
System.out.println(“email_content_____”+email_content);
System.out.println(“email_filepath_____”+email_filepath);
if(!"".equals(email_addr)) {
try {
send(email_addr,email_title,email_content,email_filepath);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(“sendEmail__end_____”);
}
private static void send(String emailAddress,String title,String cons,String filePath) throws UnsupportedEncodingException {
System.out.println("send_filepath_____"+filePath);
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", "smtp");
//服务器
prop.setProperty("mail.smtp.host", "mail.xxx.com");
//端口
prop.setProperty("mail.smtp.port", "587");
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", "true");
//使用SSL,企业邮箱必需!
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "false");
prop.put("mail.smtp.ssl.socketFactory", sf);
//
//获取Session对象
Session s = Session.getDefaultInstance(prop,new Authenticator() {
//此访求返回用户和密码的对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication("srinf@xxx.com", "密码");
return pa;
}
});
//设置session的调试模式,发布时取消
s.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(s);
try {
mimeMessage.setFrom(new InternetAddress("srinf@xxx.com","srinf@xxx.com"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
//设置主题
mimeMessage.setSubject(title);
mimeMessage.setSentDate(new Date());
//设置内容
mimeMessage.setText(cons);
if(!"".equals(filePath)){
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText(cons);
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
//设置要发送附件的文件路径
File file = new File(filePath);
DataSource source = new FileDataSource(filePath);
System.out.println("send_fileName_____"+file.getName());
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
//处理附件名称中文(附带文件路径)乱码问题
messageBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
mimeMessage.setContent(multipart);
}
mimeMessage.saveChanges();
//发送
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println(emailAddress+"----邮件发送完成");
}
}