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="" ; //發(fā)件人郵箱地址 private final static String FROM = ""; //收件人郵箱地址 private String to; //郵件主題 private String subject; //郵件正文 private String content; //記錄所有附件文件的集合 List<String> attachments = new ArrayList<String>(); //無參數(shù)的構造器 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; } //把郵件主題轉(zhuǎn)換為中文 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); } //發(fā)送郵件 public boolean send() { //創(chuàng)建郵件Session所需的Properties對象 Properties props = new Properties(); props.put("mail.smtp.host" , SMTP_SERVER); props.put("mail.smtp.auth" , "true"); //創(chuàng)建Session對象 Session session = Session.getDefaultInstance(props //以匿名內(nèi)部類的形式創(chuàng)建登錄服務器的認證對象 , new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USER_NAME,PASSWORD); } }); try { //構造MimeMessage并設置相關屬性值 MimeMessage msg = new MimeMessage(session); //設置發(fā)件人 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(); //以文件名創(chuàng)建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); //設置發(fā)送日期 msg.setSentDate(new Date()); //發(fā)送郵件 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(","); //創(chuàng)建郵件Session所需的Properties對象 } public static void main(String[] args) { SendMail sendMail = new SendMail(); //設置收件人的地址 sendMail.setTo("*******@qq.com"); //設置發(fā)送人地址 //設置標題 sendMail.setSubject("測試郵件標題!"); //設置內(nèi)容 sendMail.setContent("你好這是一個帶多附件的測試郵件!"); //粘貼附件 sendMail.attachfile("D:\\360安全瀏覽器下載\\初中英語語法句子的成分.doc"); if (sendMail.send()) { System.out.println("---郵件發(fā)送成功---"); } } } |
|