「JavaMail」送信 / 受信を行なうサンプル

JavaMail で送信 / 受信を行なうサンプルをそれぞれ書いてみたので、メモしておきます。

メールサーバは以下のエントリーで構築したものを使用しています。

CentOS 6.2 に PostfixDovecot をインストールする - プログラム日記
http://a4dosanddos.hatenablog.com/entry/2015/04/16/003619

1. 送信


package test;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendTest {
	public static void main(String[] args) {
		String to = "*****@centos62.example.com";
		String from = "*****@centos62.example.com";
		String host = "centos62.example.com";
		//boolean debug = true;

		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", host);

		Session session = Session.getInstance(props, null);
		//session.setDebug(debug);

		try {
			Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
			msg.setSubject("JavaMail Test");
			msg.setSentDate(new Date());
			msg.setText("JavaMail Test Mail");

			Transport.send(msg);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}


2. 受信


package test;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import com.sun.mail.pop3.POP3Store;

public class ReceiveTest {
	public static void main(String[] args) {

		String host = "centos62.example.com";
		String username = "*****";
		String password = "*****";
		//boolean debug = true;

		try {
			Properties props = new Properties();
			props.setProperty("mail.pop3.host", host);
			Session session = Session.getInstance(props);
			//session.setDebug(debug);
			
			POP3Store store = (POP3Store) session.getStore("pop3");
			store.connect(username, password);
			
			Folder folder = store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			
			Message[] msgs = folder.getMessages();
			for (Message msg : msgs) {
				System.out.println("---------------------------------");
				System.out.println("Email Number " + msg.getMessageNumber());
				System.out.println("Subject: " + msg.getSubject());
				System.out.println("From: " + msg.getFrom()[0]);
				System.out.println("Text: " + msg.getContent().toString());
				System.out.println("Date: " + msg.getSentDate());
			}
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

以上です。

・JavaMail API Reference Implementation: Wiki: Home — Project Kenai
https://java.net/projects/javamail/pages/Home#Samples
・Example of receiving email using JavaMail API - javatpoint
http://www.javatpoint.com/example-of-receiving-email-using-java-mail-api

[ 環境情報 ]
Windows 7 SP1
Java SE 8 Update 25
JavaMail 1.4