ksino's diary

覚えたことを忘れないように、小さなことでも書いていく。

Jakarta Mailを使ってメール送信(テキスト、HTML(画像なし)、HTML(画像あり))

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
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 text = "テスト本文";
        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.setText(text, "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);
            // ボディパート生成(画像1)
            BodyPart image1BodyPart = new MimeBodyPart();
            DataSource ds1 = new FileDataSource("/tmp/s1.jpg");
            image1BodyPart.setDataHandler(new DataHandler(ds1));
            image1BodyPart.setHeader("Content-ID", "<image1>");
            multipart.addBodyPart(image1BodyPart);
            // ボディパート生成(画像2)
            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();
        }
    }
}