package app.util.mail; import java.io.UnsupportedEncodingException; import java.util.*; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; public class SendMail { //SMTP服务器地址 private final static String SMTP_SERVER = "smtp.qq.com"; //登录SMTP服务器的用户名 private final static String USER_NAME =""; //登录SMTP服务器的密码 private final static String PASSWORD="" ; //发件人邮箱地址 private final static String FROM = ""; //收件人邮箱地址 private String to; //邮件主题 private String subject; //邮件正文 private String content; //记录所有附件文件的集合 List<String> attachments = new ArrayList<String>(); //无参数的构造器 public SendMail() { } public SendMail(String to , String subject , String content) { this.to = to; this.subject = subject; this.content = content; } //to属性的setter方法 public void setTo(String to) { this.to = to; } //subject属性的setter方法 public void setSubject(String subject) { this.subject = subject; } //content属性的setter方法 public void setContent(String content) { this.content = content; } //把邮件主题转换为中文 public String transferChinese(String strText) { try { strText = MimeUtility.encodeText(strText, "UTF-8", "B"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return strText; } //增加附件,将附件文件名添加的List集合中 public void attachfile(String fname) { attachments.add(fname); } //发送邮件 public boolean send() { //创建邮件Session所需的Properties对象 Properties props = new Properties(); props.put("mail.smtp.host" , SMTP_SERVER); props.put("mail.smtp.auth" , "true"); //创建Session对象 Session session = Session.getDefaultInstance(props //以匿名内部类的形式创建登录服务器的认证对象 , new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USER_NAME,PASSWORD); } }); try { //构造MimeMessage并设置相关属性值 MimeMessage msg = new MimeMessage(session); //设置发件人 msg.setFrom(new InternetAddress(FROM)); //设置收件人 InternetAddress[] addresses = {new InternetAddress(to)}; msg.setRecipients(Message.RecipientType.TO , addresses); //设置邮件主题 subject = transferChinese(subject); msg.setSubject(subject); //构造Multipart Multipart mp = new MimeMultipart(); //向Multipart添加正文 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setText(content); //将BodyPart添加到MultiPart中 mp.addBodyPart(mbpContent); //向Multipart添加附件 //遍历附件列表,将所有文件添加到邮件消息里 for(String efile : attachments) { MimeBodyPart mbpFile = new MimeBodyPart(); //以文件名创建FileDataSource对象 FileDataSource fds = new FileDataSource(efile); //处理附件 mbpFile.setDataHandler(new DataHandler(fds)); mbpFile.setFileName(fds.getName()); //将BodyPart添加到MultiPart中 mp.addBodyPart(mbpFile); } //清空附件列表 attachments.clear(); //向Multipart添加MimeMessage msg.setContent(mp); //设置发送日期 msg.setSentDate(new Date()); //发送邮件 Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); return false; } return true; } public static void sendMAIL(String reciever,String subject,String content,String filePath){ SendMail sendMail = new SendMail(); String[] recievers = reciever.split(","); //创建邮件Session所需的Properties对象 } public static void main(String[] args) { SendMail sendMail = new SendMail(); //设置收件人的地址 sendMail.setTo("*******@qq.com"); //设置发送人地址 //设置标题 sendMail.setSubject("测试邮件标题!"); //设置内容 sendMail.setContent("你好这是一个带多附件的测试邮件!"); //粘贴附件 sendMail.attachfile("D:\\360安全浏览器下载\\初中英语语法句子的成分.doc"); if (sendMail.send()) { System.out.println("---邮件发送成功---"); } } } |
|