由於騰訊公司給QQ郵箱增加了一個授權碼的密碼保護,導致之前網上很多代碼都不能用,於是就自己敲了一份demo。
注意在密碼那里可能需要授權碼,具體設置:http://service.mail.qq.com/cgi-bin/help?id=28
jar:javax.mail.jar
1 package xhw; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.OutputStream; 9 import java.io.OutputStreamWriter; 10 import java.io.UnsupportedEncodingException; 11 import java.util.Properties; 12 13 import javax.activation.DataHandler; 14 import javax.activation.DataSource; 15 import javax.activation.FileDataSource; 16 import javax.mail.BodyPart; 17 import javax.mail.Message; 18 import javax.mail.Multipart; 19 import javax.mail.Session; 20 import javax.mail.Transport; 21 import javax.mail.internet.InternetAddress; 22 import javax.mail.internet.MimeBodyPart; 23 import javax.mail.internet.MimeMessage; 24 import javax.mail.internet.MimeMultipart; 25 26 public class SendMail2 { 27 28 private String host = ""; // smtp服務器 29 private String from = ""; // 發件人地址 30 private String to = ""; // 收件人地址 31 private String affix = ""; // 附件地址 32 private String affixName = ""; // 附件名稱 33 private String user = ""; // 用戶名 34 private String pwd = ""; // 密碼 35 private String subject = ""; // 郵件標題 36 37 public void setAddress(String from, String to, String subject) { 38 this.from = from; 39 this.to = to; 40 this.subject = subject; 41 } 42 43 public void setAffix(String affix, String affixName) { 44 this.affix = affix; 45 this.affixName = affixName; 46 } 47 48 public void send(String host, String user, String pwd) { 49 this.host = host; 50 this.user = user; 51 this.pwd = pwd; 52 53 Properties props = new Properties(); 54 55 // 設置發送郵件的郵件服務器的屬性(這里使用網易的smtp服務器) 56 props.put("mail.smtp.host", host); 57 // 需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證 58 props.put("mail.smtp.auth", "true"); 59 props.put("mail.smtp.port", 465); 60 props.put("mail.smtp.ssl.enable", true); 61 // 用剛剛設置好的props對象構建一個session 62 Session session = Session.getDefaultInstance(props); 63 64 // 有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使 65 // 用(你可以在控制台(console)上看到發送郵件的過程) 66 session.setDebug(true); 67 68 // 用session為參數定義消息對象 69 MimeMessage message = new MimeMessage(session); 70 try { 71 // 加載發件人地址 72 message.setFrom(new InternetAddress(from)); 73 // 加載收件人地址 74 message.addRecipient(Message.RecipientType.TO, new InternetAddress( 75 to)); 76 // 加載標題 77 message.setSubject(subject); 78 79 // 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件 80 Multipart multipart = new MimeMultipart(); 81 82 // 設置郵件的文本內容 83 BodyPart contentPart = new MimeBodyPart(); 84 contentPart.setText("第二種方法···"); 85 multipart.addBodyPart(contentPart); 86 // 添加附件 87 BodyPart messageBodyPart = new MimeBodyPart(); 88 DataSource source = new FileDataSource(affix); 89 // 添加附件的內容 90 messageBodyPart.setDataHandler(new DataHandler(source)); 91 // 添加附件的標題 92 // 這里很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼 93 sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); 94 messageBodyPart.setFileName("=?GBK?B?" 95 + enc.encode(affixName.getBytes()) + "?="); 96 multipart.addBodyPart(messageBodyPart); 97 98 // 將multipart對象放到message中 99 message.setContent(multipart); 100 // 保存郵件 101 message.saveChanges(); 102 // 發送郵件 103 Transport transport = session.getTransport("smtp"); 104 // 連接服務器的郵箱 105 transport.connect(host, user, pwd); 106 // 把郵件發送出去 107 transport.sendMessage(message, message.getAllRecipients()); 108 transport.close(); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 } 113 114 public static void main(String[] args) { 115 116 File file = new File("D:\\content.csv"); 117 try { 118 OutputStream os = new FileOutputStream(file); 119 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, 120 "utf-8")); 121 bw.write("hello"); 122 bw.close(); 123 os.close(); 124 } catch (Exception e) { 125 // TODO Auto-generated catch block 126 e.printStackTrace(); 127 } 128 129 130 SendMail2 cn = new SendMail2(); 131 // 設置發件人地址、收件人地址和郵件標題 132 cn.setAddress("發件人地址", "收件人地址", "一個帶附件的JavaMail郵件(標題)"); 133 // 設置要發送附件的位置和標題 134 cn.setAffix("D:\\content.csv", "content.csv"); 135 // 設置smtp服務器以及郵箱的帳號和密碼 136 cn.send("smtp.qq.com", "賬號", "密碼(授權碼)"); 137 } 138 }