通过commons-email发送邮件

首先,去http://commons.apache.org/email/把commons-email-1.2-bin.zip下载下来,然后把其中的commons-email-1.2.jar包导入项目中。

 

commons-email-1.2.jar很小,只有32k,也就是9个类而已,却能省不少事。

 

commons-email-1.2.jar提供了如下9个类:ByteArrayDataSource、DefaultAuthenticator、Email、EmailAttachment、EmailException、EmailUtils、HtmlEmail、MultiPartEmail、SimpleEmail。

 

http://commons.apache.org/email/userguide.html有commons-email的使用示例。

 

1、SimpleEmail

 

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class MailTo {
	public static void main(String[] args) {
		try {
			//发送简单邮件
			SimpleEmail email = new SimpleEmail();
			email.setHostName("smtp.sina.com");
			//需要邮件发送服务器的用户名、密码验证 
			email.setAuthentication("[email protected]", "XXX");
			email.addTo("[email protected]", "Eric");
			email.setFrom("[email protected]", "Michael");
			email.setCharset("UTF-8");
			email.setSubject("测试邮件");
			email.setMsg("这是一封测试邮件");
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}
 

 2、HtmlEmail

 

import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailTo {
	public static void main(String[] args) {
		try {
			HtmlEmail email = new HtmlEmail();
			email.setHostName("smtp.sina.com");
			email.setAuthentication("[email protected]", "XXX");
			email.addTo("[email protected]", "Eric");
			email.setFrom("[email protected]", "Michael");
			email.setCharset("UTF-8");
			email.setSubject("测试邮件");
			URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
			String cid = email.embed(url, "Apache logo");
			email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid
					+ "\"></html>");
			email.setTextMsg("Your email client does not support HTML messages");
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}

 

3、EmailAttachment

 

import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class MailTo {
	public static void main(String[] args) {
		try {
			// Create the attachment
			EmailAttachment attachment = new EmailAttachment();
			attachment.setURL(new URL(
					"http://www.apache.org/images/asf_logo_wide.gif"));
			attachment.setDisposition(EmailAttachment.ATTACHMENT);
			attachment.setDescription("Apache logo");
			attachment.setName("Apache logo");

			// Create the email message
			MultiPartEmail email = new MultiPartEmail();
			email.setHostName("smtp.sina.com");
			email.setAuthentication("[email protected]", "XXX");
			email.addTo("[email protected]", "Eric");
			email.setFrom("[email protected]", "Michael");
			email.setCharset("UTF-8");
			email.setSubject("测试邮件");
			email.setMsg("这是一封测试邮件");

			// add the attachment
			email.attach(attachment);
			email.send();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java,commons-email)