GmailでJava Mail
以下のサイトからJava Mailをダウンロード
JavaMail API
export CLASSPATH=$CLASSPATH:/xxx/javamail-1.4.5/mail.jar:.
安全性の低いアプリの許可を有効化
Sign in - Google Accounts
import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class MailSample { public static void main(String[] args) { try { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.from", "xxx@gmail.com"); props.put("mail.smtp.auth", "true" ); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("xxx@gmail.com", "xxx"); } }); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", "465"); InternetAddress[] address = {new InternetAddress("xxx@xxx")}; MimeMessage msg = new MimeMessage(session); msg.setFrom(address); msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("件名", "iso-2022-jp"); msg.setSentDate(new Date()); msg.setText("本文1" + System.getProperty("line.separator") + "本文2" + System.getProperty("line.separator"), "iso-2022-jp"); Transport.send(msg); } catch (Exception e) { System.out.println(e); } } }