浅试一下apache commons-email.jar 发邮件的功能,代码暂时记录在此,代码还没有整理过,只是弄了几个方法测试了一下。包和文档可以在此找到
http://commons.apache.org/email/
图如下:[img]
[/img]
package com.mail.test;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
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 TestSendMail {
//发送文本邮件
public static void sendTextMail(){
Email email = new SimpleEmail();
email.setHostName("smtp.qq.com");
// email.setHostName("smtp.163.com");
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("123456@qq.com", "*******"));
email.setTLS(true);
try {
email.setFrom("123456@qq.com");
email.setMsg("This is a test mail__common_email");
email.addTo("bill@126.com");
email.setSubject("TestMail文本邮件");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
//发送带附件的邮件
public static void sendAttachmentMail(){
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D://lib/activation.jar");
//也可以以网络的方式
// attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("发给你的jar包");
attachment.setName("jar包");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.qq.com");
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("123456@qq.com", "******"));
email.setTLS(true);
try {
//发件人地址 发件人昵称
email.setFrom("123456@qq.com","发件人昵称"); //也可以不要第二个参数
email.addTo("bill@126.com","收件人昵称");
email.setSubject("The jar"); //标题
email.setMsg("Here is the jar you wanted"); //邮件内容
// add the attachment
email.attach(attachment); //附件
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//发送HTML的邮件
public static void sendHtmlEmail(){
// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.qq.com");
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("123456@qq.com", "*******"));
email.setTLS(true);
try {
email.addTo("bill@126.com", "收件人昵称");
// 发件人地址 发件人昵称
email.setFrom("123456@qq.com", "发件人昵称"); // 也可以不要第二个参数
email.setSubject("The jar"); //标题
email.setMsg("This is a test mail__common_email");
// 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, "appach logo");
// set the html message
email.setHtmlMsg("<html><a href='http://www.baidu.com'>baidu.com</a> <img src=\"cid:" + cid
+ "\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
sendTextMail();
//sendAttachmentMail();
//sendHtmlEmail();
}
}
有两个小问题还没整就要睡觉了:1,我用163作发送,总是验证不通过,还没抽空找原因,用QQ就可以发送,但我不用appach的邮件的功能,用其他就可以发过去,当然不会是用户名密码等没有输对,也不是什么服务没开的问题。
2,发送内容的编码还没整。如果发送带附件的邮件,下载时附件没有默认的后辍名,保存时要手功改保存类型。不知道怎么整。
如果有大侠路过指点一下。
- 大小: 15.2 KB
分享到:
相关推荐
用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料
apache开源项目源码commons-email-1.2-src 全部高质量开发邮件的java源程序 各种操作邮件的工具类源码,你会从中得到意想不到的效果! apache开源组织开发的开源项目源码,其优良的代码风格和高质量的源码是学习者...
NULL 博文链接:https://eblddong.iteye.com/blog/1596525
这份资料是Apache Commons Email的内容,希望能给有志成为IT人才一点帮助!! Thank you!
commons-email-1.1.jar: 这是Apache组织下的一个项目,主要就是对java的邮件功能进行了封装,用起来更加方便,简单。
Apache commons-email组件发送邮件所需使用的jar文件
Apache Commons电子邮件 Apache Commons Email旨在提供用于发送电子邮件的API。 它建立在旨在简化的JavaMail API之上。文献资料可以在上找到更多信息。 可以浏览 。 与Apache Commons Email使用有关的问题应张贴到。...
这份资料是+++利用Apache Commons Email发送邮件+++的内容,希望能给有志成为IT人才一点帮助!你的进步就是对我最大的回报! Thank you!
NULL 博文链接:https://liuzidong.iteye.com/blog/1116785
网站中经常用到的功能就是简单的发送一些邮件,比如发送 找回密码信息、报警信息,所以使用Apache Commons Email 写了一个可以复用的类,功能就是简单的发送一些邮件,可以含有收信人、抄送人、按送人、邮件主题、...
ploinMailFactory描述ploinMailFactory(过去称为“javaMailFactory”)是一个邮件框架,它提供了诸如负载平衡、模板机制、多租户和本地化等扩展功能。 在内部,它使用来自 Apache 的 commons-email API。文档文档:
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/
自己写的starter;使用的apache-commons-email 包内包含源码和已打包的jar,亲测可用,可以直接给126,qq邮箱等直接发邮件,可以包含附件.
commons-email是apache提供的一个开源的API,是对javamail的封装,因此在使用时要将javamail.jar加到 class path中,主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四个类。 HtmlEmail:HTML格式邮件...
javamail邮件开发.rar中包含apache-james-3.0-beta4-app服务器...开源工具commons-email-1.4.jar(这个其实也没什么用,因为使用maven添加依赖会自动下载这个jar包)。以及foxmail免安装版用于自己创建邮件服务器测试。
1、在 jmeter 的 .\JMeter\apache-jmeter-3.2\...2、commons-email.jar包,然后放在 ant 的 \lib 目录下 3、activation-1.1.1.jar包,然后放在 ant 的 \lib 目录下 4、javax.mail.jar包,然后放在 ant 的 \lib 目录下
import org.apache.commons.mail.SimpleEmail; public class email { public static void main(String[] args){ try{ SimpleEmail email = new SimpleEmail(); //smtp host email.setHostName("smtp....
我们将使用JavaMail和Apache Commons Email库来创建一个简单的电子邮件客户端。主要功能包括: 配置电子邮件服务器和账户信息。 发送电子邮件。 接收电子邮件。 我们将通过以下步骤来实现这个应用: 创建Java项目...
经过一个上午的折腾,终于能够发送邮件了.主要是jar包的处理.下面是附件的使用说明.欢迎大家交流. 将文件夹中5个jar包放入lib下即可. 在实际开发中,需要避免...http://commons.apache.org/email/apidocs/index.html
整理ant脚本发邮件、远程连接sshexec及登录FTP所需Jar包。 jzlib-1.1.3.jar jsch-0.1.54.jar javax.mail-1.6.2.jar commons-net-3.6.jar apache-jakarta-oro-2.0.8.jar