使用Apache email组件发送邮件实例
来源:互联网 发布:台湾国民党解放军知乎 编辑:程序博客网 时间:2024/05/07 15:11
1.使用Apache-commons-email发送邮件非常简单,只需要加载三个jar包:commons-email-1.1.jar、mail.jar、activition.jar。
Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Some of the mail classes that are provided are as follows:
SimpleEmail - This class is used to send basic text based emails.
MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
HtmlEmail- This class is used to send HTML formatted emails. It has all of thecapabilities as MultiPartEmail allowing attachments to be easily added.It also supports embedded images.
EmailAttachment- This is a simple container class to allow for easy handling ofattachments. It is for use with instances of MultiPartEmail andHtmlEmail.
2.下面两个经过测试的代码:
//发送简单的文本
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
publicclass mailSender {
/**
*@测试成功!!!
*/
publicstaticvoid main(String[] args) {
// 不要使用SimpleEmail,会出现乱码问题
HtmlEmail email = new HtmlEmail();
try{
// 这里是发送服务器的名字
email.setHostName("smtp.sohu.com");
// 编码集的设置
email.setCharset("gbk");
// 收件人的邮箱
email.addTo("pengchua@gmail.com");
// 发送人的邮箱
email.setFrom("ppzhguy@sohu.com", "ppzhguy");
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("ppzhguy","XXXX");
email.setSubject("测试Email");
// 要发送的信息
email.setMsg("测试Email ");
// 发送
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
//发送附件:
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
publicclass AttachMailSender {
/**
*@paramargs
*@throwsException
*/
publicstaticvoid main(String[] args) throws Exception {
// TODO Auto-generated method stub
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D:/测试.jpg");//指定附件在本地的绝对路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of test");//附件描述
// attachment.setName("测试");//附件名称
//如果附件是中文名会在乱码,attachment.setName(MimeUtility.encodeText("测试"));
attachment.setName(MimeUtility.encodeText("测试"));
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.sohu.com");
// 编码集的设置
email.setCharset("gbk");
// 收件人的邮箱
email.addTo("pengchua@gmail.com");
// 发送人的邮箱
email.setFrom("ppzhguy@sohu.com", "ppzhguy");
// 如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("ppzhguy","XXXXX");
email.setSubject("图片");
email.setMsg("这是你想要的图片!");
// add the attachment
email.attach(attachment);
// send the email
email.send();
}
}
- 使用Apache email组件发送邮件实例
- 使用Apache email组件发送邮件实例
- 使用Apache Commons-email组件发送邮件
- 使用Apache Commons-email组件发送邮件
- 使用apache common email 组件发送邮件
- 利用Apache commons-email组件发送邮件
- apache开源Email发送邮件实例
- Apache-commons-email发送邮件
- JAVA APACHE EMAIL 发送邮件
- Apache Commons Email邮件发送
- 使用CakePHP自带Email组件通过gmail发送邮件
- 使用Spring邮件发送Email
- 使用commons email发送邮件
- 使用Spring邮件发送Email
- 利用Commons-email组件发送邮件
- apache commons-email发送邮件乱码问题
- 利用apache的Email发送邮件
- 利用Apache Commons Email发送邮件
- asp页面编码为UTF-8乱码情况之一
- C# 多线程返回值
- 跟踪调试KernelStack笔记
- .NET程序部署问题!
- Windows平台上的Apache+Tomcat配置指南
- 使用Apache email组件发送邮件实例
- delphi杀进程代码
- 高速摄影(原创)捕捉瞬间装置
- 使用Apache Commons-email组件发送邮件
- 电脑日常故障及处理(一)
- 境界
- 电脑日常故障及处理(二)
- 来到这里!
- 逻辑无误,但确实出错了,出错原因在于变量之前被修改过,已经不是理想中的那个变量值了