package htmlmail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
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;
public class App {
public static void main(String[] args) {
new App().sendHtmlMailWithImage();
}
public void send() {
Properties props = new Properties();
props.put("mail.smtp.host", "127.0.0.1");
props.put("mail.smtp.port", "2525");
Session session = Session.getInstance(props, null);
String from = "test_from@example.com";
String to = "test_to@example.com";
String subject = "テキストメッセージ";
String text = "テスト本文";
try {
MimeMessage sendMessage = new MimeMessage(session);
sendMessage.setFrom(new InternetAddress(from));
sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to));
sendMessage.setSubject(subject, "UTF-8");
sendMessage.setText(text, "UTF-8");
Transport.send(sendMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void sendHtmlMail() {
Properties props = new Properties();
props.put("mail.smtp.host", "127.0.0.1");
props.put("mail.smtp.port", "2525");
Session session = Session.getInstance(props, null);
String from = "test_from@example.com";
String to = "test_to@example.com";
String subject = "HTMLメッセージ(画像なし)";
String html="<html><body><h1>Hello</h1>こんにちは</body></html>";
try {
MimeMessage sendMessage = new MimeMessage(session);
sendMessage.setFrom(new InternetAddress(from));
sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to));
sendMessage.setSubject(subject, "UTF-8");
sendMessage.setContent(html, "text/html; charset=\"UTF-8\"");
Transport.send(sendMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void sendHtmlMailWithImage() {
Properties props = new Properties();
props.put("mail.smtp.host", "127.0.0.1");
props.put("mail.smtp.port", "2525");
Session session = Session.getInstance(props, null);
String from = "test_from@example.com";
String to = "test_to@example.com";
String subject = "HTMLメッセージ(画像あり)";
String html="<html><body>"
+ "<h1>Hello</h1>こんにちは<br/>"
+ "<img src=\"cid:image1\"><br/>"
+ "<img src=\"cid:image2\"><br/>"
+ "</body></html>";
try {
MimeMessage sendMessage = new MimeMessage(session);
sendMessage.setFrom(new InternetAddress(from));
sendMessage.setRecipient(RecipientType.TO, new InternetAddress(to));
sendMessage.setSubject(subject, "UTF-8");
sendMessage.setContent(html, "text/html; charset=\"UTF-8\"");
MimeMultipart multipart = new MimeMultipart("related");
sendMessage.setContent(multipart);
BodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setContent(html, "text/html; charset=\"UTF-8\"");
multipart.addBodyPart(msgBodyPart);
BodyPart image1BodyPart = new MimeBodyPart();
DataSource ds1 = new FileDataSource("/tmp/s1.jpg");
image1BodyPart.setDataHandler(new DataHandler(ds1));
image1BodyPart.setHeader("Content-ID", "<image1>");
multipart.addBodyPart(image1BodyPart);
BodyPart image2BodyPart = new MimeBodyPart();
DataSource ds2 = new FileDataSource("/tmp/s2.jpg");
image2BodyPart.setDataHandler(new DataHandler(ds2));
image2BodyPart.setHeader("Content-ID", "<image2>");
multipart.addBodyPart(image2BodyPart);
Transport.send(sendMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}