GmailのSMTPサーバでメールを送る

アプリケーション引数でテキスト ファイルのファイル パスを受け取り、そのファイルの中身をGmailSMTPサーバを使用して送信します。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Security;
import java.util.Calendar;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.net.ssl.internal.ssl.Provider;

public class SmtpSample1 {

    public static void main(String[] args) {
        try {
            String to = "to@xxx.com";
            String from = "xxx@gmail.com";
            String host = "smtp.gmail.com";
            String port = "587";
            String title = getFileName(args[0]);
            String text = getFileContent(args[0]);

            Security.addProvider(new Provider());

            Properties props = new Properties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");

            Authenticator auth = new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("xxx", "xxx");
                }
            };
            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(from));
            InternetAddress[] addresses = { new InternetAddress(to) };
            msg.setRecipients(Message.RecipientType.TO, addresses);
            msg.setSubject(title, "iso-2022-jp");
            msg.setSentDate(Calendar.getInstance().getTime());
            msg.setText(text, "shift_jis");

            Transport.send(msg);
        } catch (Throwable e) {
            e.printStackTrace();
            try {
                System.in.read();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    private static String getFileName(String filePath) {
        File file = new File(filePath);
        return file.getName();
    }

    private static String getFileContent(String filePath) throws IOException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
            InputStream fileIn = new FileInputStream(filePath);
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = fileIn.read(buf)) != -1) {
                    byteOut.write(buf, 0, len);
                }
            } finally {
                fileIn.close();
            }
        } finally {
            byteOut.close();
        }
        byte[] fileContent = byteOut.toByteArray();

        String text = new String(fileContent);
        return text;
    }

}