packagecom.tideway.javamail;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;public classSendMail {//收件人地址
privateString to;//发件人地址
privateString from;//SMTP服务器地址
privateString smtpServer;//登录副武器的用户名
privateString username;//登陆服务器的密码
privateString password;//邮件主题
privateString subject;//邮件正文
privateString content;//记录所有附件文件的集合,本人没写附件
List list=new ArrayList();publicSendMail(){}/*** 初始化各属性构造器
*@paramto 收件人地址
*@paramfrom 发件人地址
*@paramsmtpServer SMTP服务器地址
*@paramusername 服务器登录用户名
*@parampassword 密码
*@paramsubject 标题
*@paramcontent 内容*/
publicSendMail(String to,String from,String smtpServer,String username,
String password,String subject,String content){this.to=to;this.from=from;this.smtpServer=smtpServer;this.username=username;this.password=password;this.subject=subject;this.content=content;
}public voidsetTo(String to) {this.to =to;
}public voidsetFrom(String from) {this.from =from;
}public voidsetSmtpServer(String smtpServer) {this.smtpServer =smtpServer;
}public voidsetUsername(String username) {this.username =username;
}public voidsetPassword(String password) {this.password =password;
}public voidsetSubject(String subject) {this.subject =subject;
}public voidsetContent(String content) {this.content =content;
}//将附件文件名添加到List集合
public voidattachfile(String filename){
list.add(filename);
}public booleansend(){//创建Session需要的Properties对象
Properties pro=newProperties();
pro.put("mail.smtp.host", smtpServer);
pro.put("mail.smtp.auth", "true");//创建Session对象
Session session=Session.getDefaultInstance(pro,newAuthenticator() {publicjavax.mail.PasswordAuthentication getPasswordAuthentication(){return newjavax.mail.PasswordAuthentication(username, password);
}
});try{//构造MimeMessage并设置相关属性
MimeMessage msg=newMimeMessage(session);//设置发件人地址
msg.setFrom(newInternetAddress(from));//设置收件人
InternetAddress[] address={newInternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);//设置邮件主题
msg.setSubject(subject);//构造Multipart
Multipart mp=newMimeMultipart();//向mp添加正文
MimeBodyPart mbpContent=newMimeBodyPart();
mbpContent.setText(content);//将MimeBodyPart添加到Multipart容器中
mp.addBodyPart(mbpContent);//附件附件附件附//没写附件//添加
msg.setContent(mp);//设置发送时间
msg.setSentDate(newDate());//发送
Transport.send(msg);
}catch(MessagingException e) {//TODO: handle exception
e.printStackTrace();return false;
}return true;
}public static voidmain(String[] args) {
SendMail sendmail=newSendMail();
sendmail.setSmtpServer("smtp.163.com");//设置登录服务器用户名
sendmail.setUsername("xxxxxxx");//163邮箱开授权码的写授权码,没开的写邮箱密码
sendmail.setPassword("自己163邮箱授权码");//给谁发
sendmail.setTo("xxxx@qq.com");
sendmail.setFrom("xxxxxxx@163.com");
sendmail.setSubject("你是一个不讲诚信的人");
sendmail.setContent("我的海底捞,我的海底捞,我的海底捞。");if(sendmail.send()) {
System.out.println("success!");
}
}
}