JavaMail发送邮件

用commons-email 包简化邮件发送:
package tf;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class Mail {

	public static void main(String[] args) {
		sendSimpleEmail();
		sendMultiPartEmail();
		sendHtmlEmail();
	}

	/**
	 * 发送简单文本邮件
	 */
	public static void sendSimpleEmail() {
		SimpleEmail email = new SimpleEmail();
		email.setHostName("smtp.gmail.com"); // 设置发送端服务器
		email.setAuthentication("test", "test"); // 用户名和密码
		email.setCharset("UTF-8"); // 设置字符集
		email.setSSL(true); // gmail邮箱必须设置为true
		try {
			email.setFrom("[email protected]", "发件人"); // 发件人
			email.addTo("[email protected]", "收件人"); // 收件人1
			// email.addTo("[email protected]", "哈哈"); // 收件人2
			// email.addCc("[email protected]", "test"); // 抄送
			// email.addBcc("[email protected]", "test"); // 密送
			email.setSubject("测试简单邮件"); // 主题
			email.setMsg("这里是测试简单邮件内容"); // 发送内容
			email.send();
			System.out.println("success!");
		} catch (EmailException e) {
			System.out.println("failure!");
			e.printStackTrace();
		}
	}

	/**
	 * 发送带附件邮件
	 */
	public static void sendMultiPartEmail() {
		MultiPartEmail email = new MultiPartEmail();
		email.setHostName("smtp.126.com"); // 设置发送端服务器
		email.setAuthentication("test", "test"); // 用户名和密码
		email.setCharset("UTF-8"); // 设置字符集
		try {
			email.setFrom("[email protected]", "发件人"); // 发件人
			email.addTo("[email protected]", "收件人"); // 收件人1
			email.setSubject("测试附件邮件"); // 主题
			email.setMsg("这里是测试附件邮件内容"); // 发送内容

			setAttach(email, Arrays.asList("d:/test1.txt", "d:/test2.doc")); // 设置附件
			email.send();
			System.out.println("success!");
		} catch (EmailException e) {
			System.out.println("failure!");
			e.printStackTrace();
		}
	}

	/**
	 * 发送HTML邮件
	 */
	public static void sendHtmlEmail() {
		HtmlEmail email = new HtmlEmail();
		email.setHostName("smtp.126.com"); // 设置发送端服务器
		email.setAuthentication("test", "test"); // 用户名和密码
		email.setCharset("UTF-8"); // 设置字符集
		try {
			email.setFrom("[email protected]", "发件人"); // 发件人
			email.addTo("[email protected]", "收件人"); // 收件人1
			email.setSubject("测试HTML邮件"); // 主题

			URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
			String cid = email.embed(url, "Apache logo");
			email.setHtmlMsg("<html>这里是测试HTML邮件内容---<img src=\"cid:" + cid
					+ "\"></html>"); // 发送内容

			setAttach(email, Arrays.asList("d:/test1.txt", "d:/test2.doc")); // 设置附件
			email.send();
			System.out.println("success!");
		} catch (Exception e) {
			System.out.println("failure!");
			e.printStackTrace();
		}
	}

	/**
	 * 设置附件
	 */
	public static void setAttach(MultiPartEmail email, List<String> list)
			throws EmailException {
		for (String aPath : list) {
			String aName = aPath.substring(aPath.lastIndexOf("/") + 1, aPath
					.length());
			EmailAttachment attach = new EmailAttachment();
			attach.setName(aName); // 设置附件名
			attach.setDisposition(EmailAttachment.ATTACHMENT);
			attach.setDescription("attach");

			attach.setPath(aPath); // 设置附件路径
			// attach.setURL(new URL(
			// "http://www.apache.org/images/asf_logo_wide.gif"));
			email.attach(attach);
		}
	}

///////////////////////////////////////////////////////////////////////////
	/**
	 * JavaMail处理发送gmail邮件
	 */
	public static void send() throws Exception {
		final String host = "smtp.gmail.com";
		final String username = "test";
		final String password = "test";

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

		handleSSL(props); //gmail邮箱需处理

		Session session = Session.getDefaultInstance(props,
				new Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("[email protected]"));
		msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
				"[email protected]", false));
		msg.setSubject("呵呵,好啊");
		msg.setText("最近在干什么呢?");
		msg.setSentDate(new Date());
		Transport.send(msg);
		System.out.println("Email has sended.");
	}


	/**
	 * 处理安全套接层协议
	 */
	private static void handleSSL(Properties props) {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.port", "465");
		props.setProperty("mail.smtp.socketFactory.port", "465");

		props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.imap.socketFactory.fallback", "false");
		props.setProperty("mail.imap.port", "143");
		props.setProperty("mail.imap.socketFactory.port", "143");
	}
}

你可能感兴趣的:(apache,.net,qq,Security,Gmail)