gmail傳送郵件

esrrhs發表於2018-07-02
package xxx;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GMailUtil
{
	private static Logger log = LoggerFactory.getLogger(GMailUtil.class);

	private static String USER_NAME = "xxx@gmail.com"; // GMail user name (just the part before "@gmail.com")
	private static String PASSWORD = "xxxx"; // GMail password

	public static void main(String[] args)
	{
		String from = USER_NAME;
		String pass = PASSWORD;

		String subject = "Java send mail example";
		String body = "Welcome to JavaMail!";

		sendFromGMail(from, pass, Arrays.asList("esrrhs@163.com"), subject, body);
	}

	private static void sendFromGMail(String from, String pass, List<String> to, String subject, String body)
	{
		try
		{
			Properties props = System.getProperties();
			String host = "smtp.gmail.com";
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.user", from);
			props.put("mail.smtp.password", pass);
			props.put("mail.smtp.port", "587");
			props.put("mail.smtp.auth", "true");

			Session session = Session.getDefaultInstance(props);
			MimeMessage message = new MimeMessage(session);

			message.setFrom(new InternetAddress(from));
			InternetAddress[] toAddress = new InternetAddress[to.size()];

			// To get the array of addresses
			for (int i = 0; i < to.size(); i++)
			{
				toAddress[i] = new InternetAddress(to.get(i));
			}

			for (int i = 0; i < toAddress.length; i++)
			{
				message.addRecipient(Message.RecipientType.TO, toAddress[i]);
			}

			message.setSubject(subject);
			message.setText(body);
			Transport transport = session.getTransport("smtp");
			transport.connect(host, from, pass);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		}
		catch (Exception e)
		{
			log.error("sendFromGMail ", e);
		}
	}
}

相關文章