java寄送附件圖檔與html格式email教學
下載點在:http://java.sun.com/products/javamail/downloads/index.html
API 文件:http://java.sun.com/products/javamail/javadocs/index.html
若你需要 source code 可見:http://kenai.com/projects/javamail/downloads
這個連結有很詳細的 code :http://www.rgagnon.com/javadetails/java-0321.html
import javax.mail.*;
//import javax.mail.Multipart.*;
import javax.mail.internet.*;
public static void sendMail(String emailAddress,String errmsg) {
System.out.println("sendMail start address="+emailAddress);
try {
// 初始設定,username 和 password 非必要
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mail.XXXXX.com.tw");
//props.setProperty("mail.user", "ifUserNameNeeded");
//props.setProperty("mail.password", "ifPasswordNeeded");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
// 產生整封 email 的主體 message
MimeMessage message = new MimeMessage(mailSession);
// 設定主旨
message.setSubject("上下班打卡時間異常");
// 文字部份,注意 img src 部份要用 cid:接下面附檔的header
MimeBodyPart textPart = new MimeBodyPart();
StringBuffer html = new StringBuffer();
html.append("<h2>"+errmsg+"</h2><br>");
html.append("<h3>如有任何問題請洽相關承辦人員,謝謝~~~</h3><br>");
//html.append("<img src='cid:image'/><br>");
textPart.setContent(html.toString(), "text/html; charset=UTF-8");
// 圖檔部份,注意 html 用 cid:image,則header要設<image>
MimeBodyPart picturePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("YourPictureFile.jpg");
picturePart.setDataHandler(new DataHandler(fds));
picturePart.setFileName(fds.getName());
picturePart.setHeader("Content-ID", "<image>");
Multipart email = new MimeMultipart();
email.addBodyPart(textPart);
//email.addBodyPart(picturePart);
message.setContent(email);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
emailAddress));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO))
;
transport.close();
} catch (AddressException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
API 文件:http://java.sun.com/products/javamail/javadocs/index.html
若你需要 source code 可見:http://kenai.com/projects/javamail/downloads
這個連結有很詳細的 code :http://www.rgagnon.com/javadetails/java-0321.html
import javax.mail.*;
//import javax.mail.Multipart.*;
import javax.mail.internet.*;
public static void sendMail(String emailAddress,String errmsg) {
System.out.println("sendMail start address="+emailAddress);
try {
// 初始設定,username 和 password 非必要
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "mail.XXXXX.com.tw");
//props.setProperty("mail.user", "ifUserNameNeeded");
//props.setProperty("mail.password", "ifPasswordNeeded");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
// 產生整封 email 的主體 message
MimeMessage message = new MimeMessage(mailSession);
// 設定主旨
message.setSubject("上下班打卡時間異常");
// 文字部份,注意 img src 部份要用 cid:接下面附檔的header
MimeBodyPart textPart = new MimeBodyPart();
StringBuffer html = new StringBuffer();
html.append("<h2>"+errmsg+"</h2><br>");
html.append("<h3>如有任何問題請洽相關承辦人員,謝謝~~~</h3><br>");
//html.append("<img src='cid:image'/><br>");
textPart.setContent(html.toString(), "text/html; charset=UTF-8");
// 圖檔部份,注意 html 用 cid:image,則header要設<image>
MimeBodyPart picturePart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("YourPictureFile.jpg");
picturePart.setDataHandler(new DataHandler(fds));
picturePart.setFileName(fds.getName());
picturePart.setHeader("Content-ID", "<image>");
Multipart email = new MimeMultipart();
email.addBodyPart(textPart);
//email.addBodyPart(picturePart);
message.setContent(email);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
emailAddress));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO))
;
transport.close();
} catch (AddressException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
留言
張貼留言