地址主题java项目中添加邮件发送功能(使用apache commons mail开源项目)

发一下牢骚和主题无关:

    在java项目中添加邮件发送功能 先下载jar包添加到项目中

    apache commons mail开源项目JAR下载

    代码示例:

    每日一道理
流逝的日子像一片片凋零的枯叶与花瓣,渐去渐远的是青春的纯情与浪漫。不记得曾有多少雨飘在胸前风响在耳畔,只知道沧桑早已漫进了我的心爬上了我的脸。当一个人与追求同行,便坎坷是伴,磨难也是伴。
package com.util.mail;

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;

/**
 * 使用apache commons mail开源项目发送邮件示例
 * http://commons.apache.org/proper/commons-email/
 * 
 * @author
 */
public class SendMail {

	private static final String HOSTNAME = "smtp.qq.com";
	private static final String POP_USERNAME = "83333333@qq.com";
	private static final String USERNAME = "zjx88ba"; // 个人姓名
	private static final String POP_PASSWORD = "123456";
	private static final String CODING = "UTF-8";

	public static void main(String[] args) {

	}

	/**
	 * 
	 * @Title: simpleEmail
	 * @Description: TODO
	 * @param @param toEmail :收件人地址
	 * @param @param subject :主题
	 * @param @param msg :内容
	 * @return void
	 * @throws
	 */
	public static void simpleEmail(String toEmail, String subject, String msg) {
		SimpleEmail email = new SimpleEmail();
		email.setHostName(HOSTNAME);
		email.setAuthentication(POP_USERNAME, POP_PASSWORD);// 邮件服务器验证:用户名/密码
		email.setCharset(CODING);// 必须放在前面,否则乱码
		try {
			email.addTo(toEmail);
			email.setFrom(POP_USERNAME, USERNAME);
			email.setSubject(subject);
			email.setMsg(msg);
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: multiPartEmail
	 * @Description: TODO
	 * @param @param toEmail :收件人地址
	 * @param @param subject :主题
	 * @param @param msg :内容
	 * @param @param filePath :附件路径
	 * @param @param fileName :附件名
	 * @return void
	 * @throws
	 */
	public static void multiPartEmail(String toEmail, String subject,
			String msg, String filePath, String fileName) {
		MultiPartEmail email = new MultiPartEmail();
		email.setHostName(HOSTNAME);
		email.setAuthentication(POP_USERNAME, POP_PASSWORD);
		email.setCharset(CODING);
		try {
			email.addTo(toEmail);
			email.setFrom(POP_USERNAME, USERNAME);
			email.setSubject(subject);
			email.setMsg(msg);
			EmailAttachment attachment = new EmailAttachment();
			attachment.setPath("d:/student_templet.xls");// 当地文件
			// attachment.setURL(new URL("filePath"));//远程文件filePath
			attachment.setDisposition(EmailAttachment.ATTACHMENT);
			attachment.setDescription("问难学生名单");
			attachment.setName("student_templet.xls");// fileName

			email.attach(attachment);
			email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: htmlEmail
	 * @Description: TODO
	 * @param @param toEmail :收件人地址
	 * @param @param subject :主题
	 * @param @param msg :内容
	 * @return void
	 * @throws
	 */
	public static void htmlEmail(String toEmail, String subject, String msg) {

		HtmlEmail email = new HtmlEmail();
		email.setHostName(HOSTNAME);
		email.setAuthentication(POP_USERNAME, POP_PASSWORD);
		email.setCharset(CODING);
		try {
			email.addTo(toEmail);
			email.setFrom(POP_USERNAME, USERNAME);
			email.setSubject(subject);
			email.setHtmlMsg("<b>关于论文最后问难时光</b><br/><div>2013-05-18</div>");
			// email.setHtmlMsg(msg);
			email.send();
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

    
 java开源WebMail系统

文章结束给大家分享下程序员的一些笑话语录: 程序员的愿望
  有一天一个程序员见到了上帝.上帝: 小伙子,我可以满足你一个愿望.程序员: 我希望中国国家队能再次打进世界杯.
  上帝: 这个啊!这个不好办啊,你还说下一个吧!
  程序员: 那好!我的下一个愿望是每天都能休息6个小时以上.
  上帝: 还是让中国国家打进世界杯.

posted @ 2013-05-18 18:40  xinyuyuanm  阅读(259)  评论(0编辑  收藏  举报