package Test;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
/*
需要的jar 包 activation.jar mail.jar
*/
public class Send2{
// 邮箱服务器
private String host = "smtp.qq.com";// 或者是 smtp.163(sina).com
private String username = "[email protected]"; //邮箱名称
private String password = " "; //邮箱密码
private String mail_to = "[email protected]"; //发送至 人邮箱
private String mail_from = "[email protected]"; //发送人邮箱username
private String mail_subject = "送你的幸福!湘竹集美伊客!"; //邮箱主题
private String mail_body = "测试信息"; //邮箱内容
private String personalName = "[email protected]"; //发送人邮箱username
public Send2(){}
public void send() throws Exception{
try{
Properties props=new Properties();
Authenticator auth = new Email_Autherticator(); // 进行邮件服务器用户认证
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
//设置session,和邮件服务器进行通讯。
MimeMessage message = new MimeMessage(session);
message.setSubject(mail_subject); // 设置邮件主题
//添加附件信息
Multipart mp = new MimeMultipart();
BodyPart mbp1 = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d:\\测试.doc");
mbp1.setDataHandler(new DataHandler(fds));
//设置附件名称
mbp1.setFileName(MimeUtility.encodeText("d:\\测试.doc", "utf-8", null));
mp.addBodyPart(mbp1);
BodyPart mbp = new MimeBodyPart();
mbp.setContent(mail_body, "text/html;charset=utf-8");
mp.addBodyPart(mbp);
message.setContent(mp);
message.setSentDate(new Date()); // 设置邮件发送日期
Address address = new InternetAddress(mail_from, personalName);
message.setFrom(address); // 设置邮件发送者的地址
Address toAddress = new InternetAddress(mail_to); // 设置邮件接收方的地址
message.addRecipient(Message.RecipientType.TO, toAddress);
Transport.send(message); // 发送邮件
System.out.println("邮件发送成功!");
}catch (Exception ex){
//ex.printStackTrace();
if(ex instanceof javax.mail.AuthenticationFailedException){
System.out.println("认证错误");
}
throw new Exception(ex.getMessage());
}
}
/**
* 用来进行服务器对用户的认证
*/
public class Email_Autherticator extends Authenticator{
public Email_Autherticator(){
super();
}
public Email_Autherticator(String user, String pwd){
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
}
public static void main(String[] args){
Send2 sendmail = new Send2();
try{
sendmail.send();
}catch (Exception ex){
ex.printStackTrace();
}
}
}
/*注意: 当发送人使用QQ 邮箱时,QQ 邮箱必须开启PoP3/SMTP 协议
如何启用PoP3/SMTP 协议
登陆QQ邮箱 https://mail.qq.com/
设置-->账号--> 开启PoP3/SMTP 协议*/