本文利用apaceh commons-email发送邮件,记录一下,赶紧总结完下班了哦。。哈哈
所需要引入的包:commons-email-1.2.jar(apache官网下载),mail.jar(sun官网下载),activation.jar(sun官网下载,如果你使用的jdk是1.6以上,这个包就可以不下载了,因为jdk1.6已经包括了)
直接上代码吧。
PropertiesUtils类:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*@author liudonggan Email:ldg123456@sina.com
*@date 2011-7-20 上午11:29:14
*@version v1.0
**/
public class PropertiesUtils {
private static InputStream inputStream=null;
public static String getProperties(String propertyName) throws Exception
{
String _propertyName="";
inputStream=PropertiesUtils.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
inputStream.close();
}
_propertyName=p.get(propertyName).toString();
inputStream.close();
return _propertyName;
}
}
MailUtils类:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import com.ys.product.bean.UserEmail;
/**
*@author liudonggan Email:ldg123456@sina.com
*@date 2011-7-20 上午10:49:41
*@version v1.0
**/
public class MailUtils {
/**
* sendMail 方法
* <p>方法说明:</p>
* @param path附件路迳
* @param subject邮件主题
* @param msg邮件内容
* @throws Exception
* @return void
* @author liudonggan Email:ldg123456@sina.com
* @date 2011-7-20 上午11:39:37
*/
public void sendMail(String path,List<UserEmail> emails,String subject,String msg) throws Exception
{
MultiPartEmail email = new MultiPartEmail();
//SimpleEmail email = new SimpleEmail();
// 设置邮件编码
email.setCharset("UTF-8");
// 设置邮件服务器
email.setHostName(PropertiesUtils.getProperties("mail.smtp"));
// 设置登录邮件服务器用户名和密码
email.setAuthentication(PropertiesUtils.getProperties("mail.email"), PropertiesUtils.getProperties("mail.pwd"));
// 添加收件人
for(UserEmail _email:emails)
{
email.addTo(_email.getEmailName(), _email.getTitle());
}
// 设置发件人
email.setFrom(PropertiesUtils.getProperties("mail.email"), "liudonggan");
// 设置邮件标题
email.setSubject(subject);
// 设置邮件正文内容
email.setMsg(msg);
// 要发送的附件
EmailAttachment attachment = new EmailAttachment();
File file = new File(path);
attachment.setPath(file.getPath());
attachment.setName(file.getName());
// 设置附件描述
attachment.setDescription(subject);
// 设置附件类型
attachment.setDisposition(EmailAttachment.ATTACHMENT);
// 添加附件
email.attach(attachment);
// 发送邮件
email.send();
}
public static void main(String[] arg) throws Exception
{
MailUtils mail=new MailUtils();
UserEmail _email=new UserEmail();
_email.setEmailName("20493351@qq.com");
_email.setTitle("XX标题");
List<UserEmail> emails=new ArrayList<UserEmail>();
emails.add(_email);
mail.sendMail("c:\\test.pdf",emails,"XX标题","内容");
}
}
分享到:
相关推荐
用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料
apache-commons-id.jar和commons-discovery-0.2.jar
jar包大小:135KB commons-validator-1.3.1.jar jar包大小:93KB commons-transaction-1.2.jar jar包大小:141KB commons-scxml-0.6.jar jar包大小:254KB commons-primitives-1.0.jar jar包大小:60KB commons-pool-1.3....
Maven坐标:org.apache.commons:commons-email:1.5; 标签:apache、commons、email、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译...
NULL 博文链接:https://eblddong.iteye.com/blog/1596525
基于Java调用邮件系统所需要的jar包,commons-email-1.5.jar,javax.mail-1.4.5.jar,activation.jar
1)将文件放入libs后,在gradle文件里加入implementation files('libs/commons-email-1.5.jar') 2)android收发邮件会要求引入此jar包
集成邮件发送所需要的四个jar包,activation.jar、additionnal.jar、commons-email-1.4.jar、mail.jar
Maven坐标:org.apache.commons:commons-math3:3.5; 标签:apache、commons、math3、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
Maven坐标:org.apache.commons:commons-email:1.5; 标签:apache、commons、email、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
commons-email-1.5-src.zip 借助commons-email简化发送邮件的过程
Apache Commons IO 2.5 (要求 JDK 1.6),IOUtils,FileUtils,jar包下载
NULL 博文链接:https://shuaigg-babysky.iteye.com/blog/355128
Maven坐标:org.apache.commons:commons-lang3:3.4; 标签:apache、commons、lang3、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
apache commons jar(commons所有的jar包... apache-sanselan-incubating-0.97-bin bcel-5.2 commons-beanutils-1.9.2-bin commons-chain-1.2-bin commons-cli-1.3.1-bin commons-codec-1.10-bin commons-collections4-4
apache-jakarta-commons-email.jar apache-jakarta-commons-fileupload.jar apache-jakarta-commons-httpclient.jar apache-jakarta-commons-io.jar apache-jakarta-commons-lang.jar apache-jakarta-commons-...
包含邮件发送的jar包,同时添加实现过程文章的连接,个人觉着操作简单,可以轻松实现邮件发送功能。啦啦啦啦啦啦啦啦啦
apache commons-net-3.5 实现 FTP 上传文件的神器,拿走不谢! 主要涉及上传文件的jar,希望能帮到你。
邮件开发包,email包,是开发web常用包