import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class JavaMailSample {
static String smtpHost = "smtp.bruce.com.tw";
static int smtpPort = 25;
static String[] from = new String[] { "bruce@bruce.com.tw", "Bruce Tasi" };
static String to = "nanashi@bruce.com.tw";
static String account = "account";
static String password = "password";
static String subject = "信件主旨";
static String contentType = "text/html;charset=utf-8";
static String body = "<html><body><h1 style=\"color:blue;\">Mail test</h1><img src=\"cid:image01\" /></body></html>";
static Message getMessage(Session session) throws MessagingException, UnsupportedEncodingException {
Message msg = new MimeMessage(session);
// 寄件者
msg.setFrom(new InternetAddress(from[0], from[1]));
// msg.setFrom(new InternetAddress(String.format("\"%s\"<%s>", from[1], from[0])));
// 收件者
InternetAddress[] address = InternetAddress.parse(to, false);
msg.setRecipients(Message.RecipientType.TO, address);
// 主旨 (需編碼)
msg.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
// 寄件時間
msg.setSentDate(new Date());
// 附件
File file = new File("write.png");
if (file.exists()) {
// 內文
MimeBodyPart contentBody = new MimeBodyPart();
// 內容與格式
contentBody.setContent(body, contentType);
// 附件
MimeBodyPart attachment = new MimeBodyPart();
FileDataSource fds = new FileDataSource(file.getName());
attachment.setDataHandler(new DataHandler(fds));
// 附件名稱 (需編碼)
attachment.setFileName(MimeUtility.encodeText(fds.getName(), "utf-8", "B"));
// 附件代號, 用於顯示郵件內文
attachment.setHeader("Content-ID", "<image01>");
// 組成郵件
MimeMultipart mp = new MimeMultipart();
// 內容顯示附件圖片時, 必須設定為related
mp.setSubType("related");
mp.addBodyPart(contentBody);
mp.addBodyPart(attachment);
msg.setContent(mp);
} else {
// 若沒有檔案時,就直接存郵件內容
msg.setContent(body, contentType);
}
return msg;
}
static void sendMail1() throws MessagingException, UnsupportedEncodingException {
// 建立工作階段
Session session = Session.getDefaultInstance(new Properties(), null);
// 開啟除錯模式
session.setDebug(true);
Message msg = getMessage(session);
Transport transport = session.getTransport("smtp");
transport.connect(smtpHost, smtpPort, account, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
static void sendMail2() throws UnsupportedEncodingException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.socketFactory.port", smtpPort);
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(account, password);
}
});
session.setDebug(true);
Message msg = getMessage(session);
Transport.send(msg);
}
}
JavaMail