好久没有更新自己的博客了,我胡汉三又回来啦。今天说一说使用commons-email来实现简单的发送邮件程序。这里可以发 送纯文本的,也可以发送带附件的,还可以发送HTML格式的邮件。废话少说,开始我们的邮件之旅。
好吧,想发送邮件,我们第一步要干嘛呢?你说呢?我说啊,第一步当然要找到commons-email相关的类库了啊。这里具体要用到三个类库。mail.jar commons-email-1.0.jar activation.jar。这三个库在哪里下载呢?告诉你一个好的网址啊www.google.cn。绝对可以找到你想要的。^_^
具体我也不细说,这里操作和引用其他第三方类库差不多。下面我贴三段代码,我想大家一看就会明白了。
public String send(){
String result = null;
SimpleEmail email = new SimpleEmail();
//设置主机名称
email.setHostName(rc.read("send"));
//设置用户名,密码
email.setAuthentication(rc.read("username"), rc.read("password"));
//设置字符编码方式
email.setCharset("GB2312");
try {
//设置发送源地址
email.setFrom(from);
//设置目标地址
email.addTo(to);
//设置主题
email.setSubject(subject);
//设置邮件内容
email.setMsg(msg);
//发送邮件
email.send();
result = "successful";
} catch (Exception e) {
e.printStackTrace();
result = "not successful";
}
return result;
}
上面这段代码是发送纯文本文件的邮件。rc.read方法是自己自定义的从资源文件当中读取的相关邮件参数。
该项目还支持带附件的邮件发送。如下代码所示:
public String sendEnclosure() throws EmailException{
String result = null;
EmailAttachment emailattachment = new EmailAttachment();
//设置附件路径
emailattachment.setPath(file);
//System.out.println(path);
emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
//附件描述
emailattachment.setDescription("This is Smile picture");
/*
* 设置附件的中文乱码问题,解决附件的中文名称 乱码问题
*/
BASE64Encoder enc = new BASE64Encoder();
//this.getName().getBytes()使用的是系统缺省的编码处理,这里是GBK
emailattachment.setName("=?GBK?B?"+enc.encode(file.getBytes())+"?=");
//attachment.setName(this.getName()); //不处理字符集的话,发送的附件中文名称是乱码
//创建一个email
MultiPartEmail multipartemail = new MultiPartEmail();
//设置主机名称
multipartemail.setHostName(UserInfo.sendHost);
//设置字符编码
multipartemail.setCharset("GB2312");
//设置发送邮件目的地址
multipartemail.addTo(to);
//设置发送又见源地址
multipartemail.setFrom(from);
//设置用户名和密码
multipartemail.setAuthentication(UserInfo.username, UserInfo.password);
//设置主题
multipartemail.setSubject(subject);
//设置邮件内容
multipartemail.setMsg(msg);
//添加附件
multipartemail.attach(emailattachment);
//发送邮件
multipartemail.send();
result = "The attachmentEmail send sucessful!!!";
return result;
}
同样的,发用的邮件格式依然支持HTML格式,如下代码所示:
public String sendHtml() throws EmailException, MalformedURLException{
// 创建邮件信息
String result = null;
HtmlEmail email = new HtmlEmail();
email.setHostName(UserInfo.sendHost);
email.addTo(to);
email.setFrom(from);
email.setSubject(subject);
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");
// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
return result;
}
好吧,三段代码都差上去了,其实发送起来很简单,大家一定都能看懂,你说对吧。看不懂的,告诉你啊,去google。hoho。
分享到:
相关推荐
NULL 博文链接:https://shuaigg-babysky.iteye.com/blog/355128
NULL 博文链接:https://eblddong.iteye.com/blog/1596525
javamail&commons-email 发送邮件的类库
集成邮件发送所需要的四个jar包,activation.jar、additionnal.jar、commons-email-1.4.jar、mail.jar
commons-email-1.5-src.zip 借助commons-email简化发送邮件的过程
包含邮件发送的jar包,同时添加实现过程文章的连接,个人觉着操作简单,可以轻松实现邮件发送功能。啦啦啦啦啦啦啦啦啦
Java发送邮件所需要的几个JAR包,打包发给大家,包含以下内容: commons-email-1.1.jar mail.jar additionnal.jar activation.jar
commons-email封装了邮件发送权限处理,AuthenticationFailedException错误可以解决了
用commons-email-1.2.jar 发送邮件的三个jar 文件
activation-1.1 commons-email activation java发送邮件jar 教程: http://blog.csdn.net/sir_jun/article/details/9106397
Jmeter+ant测试报告发送邮件
java简单邮件发送SimpleEmail,简单应用见博文"Java发送简单email:SimpleEmail
Apache commons-email组件发送邮件所需使用的jar文件
Java发送电子邮件的包,commons-email-1.1-src.tar.gz
其中包括:activation.jar activation-1.1.jar commons-email-1.5.jar mail-1.4.jar jmeter邮件发送所需jar包,发送邮件之前需要将jar包放到ant的lib目录下 避免 ...
commons email 发送html邮件完整优化实例
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/