leegh 2014-12-29
http://blog.163.com/sender@yeah/blog/static/127950071201001943836422/
^^
mail.jar下载 349k
<!-- 01-22 更新 -->
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
原因是jar包版本不统一,解决方法如下:
删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.
具体方法如下:
用rar打开X:C:\Program Files\MyEclipse 6.5\myeclipse\eclipse\plugins\com.genuitec.eclipse.j2eedt.core_6.5.0.zmyeclipse650200806\data\libraryset\EE_5\javaee.jar
,然后删除mail,一切就ok了.
<!-- ^^ -->
最近在博友spark博客上发现一篇关于使用commons email 发邮件的帖子,很感兴趣,因为以前还真没做过这方面的功能。进去看了看,然后又到Apache官网上找了UserGuide看了看,居然发现发邮件这么简单~~
本文完全参考于Apache的用户指南,有价值的地方(用户指南没有的)为 邮件乱码问题,和主机方为Google邮箱问题处理,如果这些都知道,还是看Apache的userguide.html,比我的简明多了
参考文档:http://commons.apache.org/email/userguide.html
运行程序截图:
真是战况喜人啊,第一次运行居然全部ok。
可点击进去却发现邮件中设置了中文的地方都出现了乱码
简单邮件测试中消息“你好,郵件測試,看到這,說明已經成功了”出现为?????
但到带本地附件邮件中:附件名称为中文,没有显示,显示为 No name 2.jpg,但是消息 “你好,郵件測試,看到這,說明已經成功了”却又正常显示出来,纠结!!
Google一下,发现已经有很多帖子来解决它,随便挑了个,就ok了,就是给它设置编码
//设置编码
email.setCharset("UTF-8");
附件Attachment没有setCharset方法,但可以用
MimeUtility.encodeText(String str)来转换
简单邮件测试:
带本地附件的邮件
带网络附件的邮件
HTML EMAIL
需要jar包 mail.jar 和commons-email-1.2.jar
代码
package mail;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.internet.MimeUtility;
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 Email Util
* @description commons email 使用总结
* @time 2010-1-19下午04:21:59
*/
public class MailService {
public static void main(String[] args) {
//以下参数,完全可以设置成一个JAVABEAN,但这得copy到blog中就写成这样了
String encoding = "UTF-8";
//smtp.gmail.com smtp.163.com smtp.qq.com
String hostName = "smtp.gmail.com";
String addToEmail = "[email protected]";
String addToName = "sloong";//接收方名称
String fromEmail = "[email protected]";//发送方为gmail--对应的hostname="smtp.gmail.com";
String fromName = "slg-gmail";//发送方名称
String uname = "userName";
String pwd = "userPassword";
String subject = "邮件测试:";
String msg = "你好,郵件測試,看到這,說明已經成功了";
String textMsg = "對不起,您的郵件客戶端不支持HTML Email";
//附件一参数
String attachmentURL = "http://www.apache.org/images/asf_logo_wide.gif";
String attachmentDesc = "Apache Logo";
String attachmentName = "Apache Logo";
//附件二参数
String attachmentPath = "c:/TIMS_sub.jpg";
String attachmentDesc1 = "附件描述--帅照";
String attachmentName1 = "附件名称--本人照片";
//html中的参数
String urlName = "Apache logo";
String urlPath = "http://www.apache.org/images/asf_logo_wide.gif";
// 简单邮件方法测试
simpleEmail(encoding,hostName, addToEmail, addToName, fromEmail, fromName,
uname, pwd, subject + "简单文本邮件", msg);
// 带附件邮件测试
// 1 本地文件
localAttachmentEmail(encoding,hostName, addToEmail, addToName, fromEmail, fromName,
uname, pwd, subject + "带本地附件邮件", msg, attachmentPath,
attachmentDesc1, attachmentName1);
// 2网络文件
netAttachmentEmail(encoding,hostName, addToEmail, addToName, fromEmail, fromName,
uname, pwd, subject + "带网络附件邮件", msg, attachmentURL,
attachmentDesc, attachmentName);
// htmlEmail测试
htmlEmail(encoding,hostName, addToEmail, addToName, fromEmail, fromName, uname,
pwd, subject + "HTML 邮件", urlPath, urlName, textMsg);
}
/**
* A simple text email
*
* @description 简单文本邮件
* @time 2010-1-19下午02:06:23
*/
public static boolean simpleEmail(String encoding ,String hostName, String addToEmail,
String addToName, String fromEmail, String fromName, String uname,
String pwd, String subject, String msg) {
// 1、创建简单邮件
SimpleEmail email = new SimpleEmail();
// 2、设置邮件服务器(接收方的)
email.setHostName(hostName);
// 如果为Google邮箱,需要设置它的SSL端口
if ("smtp.gmail.com".equals(hostName)) {
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
}
// 3、邮件内容
try {
// 发送至 addToEmail,该email名称为addToName
email.addTo(addToEmail, addToName);
// 发送人fromEmail,发送人名称fromName
email.setFrom(fromEmail, fromName);
// 验证发送人 uname:用户名,pwd:密码
email.setAuthentication(uname, pwd);
//设置编码
email.setCharset(encoding);
// 主题设置
email.setSubject(subject);
// 消息设置
email.setMsg(msg);
// 发送
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
return true;
}
/**
* Sending emails with attachments
*
* @description 如何要添加多个附件,最好建立一个JavaBean:Attachment {path,desc,name}
* @param 参数不做过多解释 ,详见程序
* @return
* @time 2010-1-19下午01:58:36
*/
public static boolean localAttachmentEmail(String encoding,String hostName,
String addToEmail, String addToName, String fromEmail,
String fromName, String uname, String pwd, String subject,
String msg, String attachmentPath, String attachmentDesc,
String attachmentName) {
// 1、创建一个Email附件 EmailAttachment
EmailAttachment attachment = new EmailAttachment();
// 2、本地附件的添加示例
attachment.setPath(attachmentPath);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
try {
attachment.setDescription(MimeUtility.encodeText(attachmentDesc));
attachment.setName(MimeUtility.encodeText(attachmentName));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 3、创建一个MultiPartEmail
MultiPartEmail email = new MultiPartEmail();
email.setHostName(hostName);
if ("smtp.gmail.com".equals(hostName)) {
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
}
try {
// 发送至 addToEmail,该email名称为addToName
email.addTo(addToEmail, addToName);
// 发送人fromEmail,发送人名称fromName
email.setFrom(fromEmail, fromName);
// 验证发送人 uname:用户名,pwd:密码
email.setAuthentication(uname, pwd);
//设置编码
email.setCharset(encoding);
// 主题设置
email.setSubject(subject);
// 消息设置
email.setMsg(msg);
// 4、添加附件
email.attach(attachment);
// 发送邮件
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
/**
* @description 如果添加多个附件,可以把最后三个参数设置为JavaBean的属性,再传递一个附件JavaBean集合过来
* @return
* @time 2010-1-19下午02:50:05
*/
public static boolean netAttachmentEmail(String encoding,String hostName,
String addToEmail, String addToName, String fromEmail,
String fromName, String uname, String pwd, String subject,
String msg, String urlPath, String attachmentDesc, String attachmentName) {
// 1、创建一个Email附件 EmailAttachment
EmailAttachment attachment = new EmailAttachment();
// 2、网络附件添加示例,参考Apache的UserGuide,
// 意思大致是指添加URL指向文件,该文件将被自动下载并添加到邮件的附件中
attachment.setDisposition(EmailAttachment.ATTACHMENT);
try {
attachment.setDescription(MimeUtility.encodeText(attachmentDesc));
attachment.setName(MimeUtility.encodeText(attachmentName));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URL url;
try {
url = new URL(urlPath);
attachment.setURL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 3、创建一个MultiPartEmail
MultiPartEmail email = new MultiPartEmail();
email.setHostName(hostName);
if ("smtp.gmail.com".equals(hostName)) {
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
}
try {
// 发送至 addToEmail,该email名称为addToName
email.addTo(addToEmail, addToName);
// 发送人fromEmail,发送人名称fromName
email.setFrom(fromEmail, fromName);
// 验证发送人 uname:用户名,pwd:密码
email.setAuthentication(uname, pwd);
//设置编码
email.setCharset(encoding);
// 主题设置
email.setSubject(subject);
// 消息设置
email.setMsg(msg);
// 4、添加附件
email.attach(attachment);
// 发送邮件
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
return true;
}
public static boolean htmlEmail(String encoding,String hostName, String addToEmail,
String addToName, String fromEmail, String fromName, String uname,
String pwd, String subject, String urlPath,
String urlName, String textMsg) {
// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName(hostName);
// 如果为Google邮箱,需要设置它的SSL端口
if ("smtp.gmail.com".equals(hostName)) {
email.setSSL(Boolean.TRUE); // 设定是否使用SSL
email.setSslSmtpPort("465"); // 设定SSL端口
}
try {
// 发送至 addToEmail,该email名称为addToName
email.addTo(addToEmail, addToName);
// 发送人fromEmail,发送人名称fromName
email.setFrom(fromEmail, fromName);
// 验证发送人 uname:用户名,pwd:密码
email.setAuthentication(uname, pwd);
//设置编码
email.setCharset(encoding);
// 主题设置
email.setSubject(subject);
// embed the image and get the content id
// 意思大致是指embed嵌入的是一个图片的标记,该方法返回随机生成的唯一标示字符串。
URL url = new URL(urlPath);
String cid = email.embed(url, urlName);
// set the html message
// 另外setMsg()方法在HtmlEmail中不推荐使用,相应用setHtmlMsg()和setTextMsg()代替
email.setHtmlMsg("<html> <img src=\"cid:" + cid + "\"></html>");
// set the alternative message
email.setTextMsg(textMsg);
// send the email
email.send();
} catch (EmailException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
}
一切over了