Javaweb QQ郵箱發郵件功能
需要的jar包在附件中
<接上傳功能後>
com.jspsmart.upload.File tempFile=upload.getFiles().getFile(0);
String FileName =tempFile.getFileName(); //上傳檔案的檔名
String FileNamePath= filePath+"/"+FileName; //FileName 代表檔案上傳後的地址
Properties props = new Properties(); // 建立Properties 類用於記錄郵箱的一些屬性
props.put("mail.smtp.auth", "true"); // 表示SMTP傳送郵件,必須進行身份驗證
props.put("mail.smtp.host", "smtp.qq.com"); //此處填寫SMTP伺服器
props.put("mail.smtp.port", "587"); //埠號
props.put("mail.user", "*@qq.com"); // 此處填寫你的賬號
props.put("mail.password", " **********"); // // 此處的密碼就是前面說的16位STMP口令這邊需要從qq郵箱中獲得
final String userName = props.getProperty("mail.user");
final String password = props.getProperty("mail.password");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password); // 使用者名稱、密碼
}
};
Session mailSession = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(mailSession); // 建立郵件訊息
InternetAddress form; // 設定發件人
try {
form = new InternetAddress(
props.getProperty("mail.user"));
try {
message.setFrom(form);
} catch (MessagingException e) {
e.printStackTrace();
}
} catch (AddressException e) {
e.printStackTrace();
}
InternetAddress to;
try {
to = new InternetAddress("*@qq.com");//
try {
message.setRecipient(RecipientType.TO, to);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (AddressException e1) {
e1.printStackTrace();
}
try {
message.setSubject("簡歷"); // 設定郵件標題
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Multipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart(); // 設定郵件的文字內容
try {
contentPart.setText("郵箱:"+emaill);
multipart.addBodyPart(contentPart);
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(FileNamePath);
messageBodyPart.setDataHandler(new DataHandler(source)); // 新增附件的內容
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); // 這裡很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在傳送時不會變成亂碼
messageBodyPart.setFileName("=?GBK?B?"
+ enc.encode(FileName.getBytes("GBK")) + "?=");
multipart.addBodyPart(messageBodyPart);//html.setFileName("=?GBK?B?" + enc.encode(file.getName().getBytes("GBK")) + "?=");
message.setContent(multipart); // 將multipart物件放到message中
message.saveChanges(); // 儲存郵件
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-Java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Transport.send(message);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
功能完美實現。