Apache Commons Email组件介绍使用

来源:互联网 发布:网络机顶盒排行榜 编辑:程序博客网 时间:2024/05/21 22:41

Apache Commons Email组件介绍使用

介绍

发送邮件功能这几乎是很多应用程序都会使用到的,所以Apache Commons提供了Email组件便捷的发送邮件方式。只需要简单的几个步骤就能够达到发送邮件的功能。

几种常见的发送邮件格式

SimpleEmail(普通文本)

    // 定义发送简单邮件对象    SimpleEmail simpleEmail = new SimpleEmail();    // 设置SMTP服务器,比如:smtp.163.com    // Set the hostname of the outgoing mail server.    simpleEmail.setHostName("smtp.163.com");    // 设置登入认证服务器的用户名和密码    // Sets the userName and password if authentication is needed. If this    // method is not used, no authentication will be performed.    simpleEmail.setAuthentication("username", "password");    // 默认是25端口    simpleEmail.setSmtpPort(25);    // SSL enabled SMTP server,即如果是支持SSL服务器的端口是465    // simpleEmail.setSmtpPort(465);//gmail邮箱服务器就是支持SSL的。    // 设置发送人邮箱和名字    simpleEmail.setFrom("xxx@163.com", "xuyi", "utf-8");    // 设置收件人可以是多个    simpleEmail.addTo("xxx@qq.com", "sky-xuyi");    // simpleEmail.addTo(String ...emails);    // 设置发送主题    simpleEmail.setSubject("use apache email send mial");    // 设置发送主体内容    simpleEmail.setMsg("apache commons emial is a very good commonent for send email");    // 确定发送邮件动作    simpleEmail.send();    System.out.println("send mail success");

MultiPartEmail(携带附件的邮件)

    // Create the attachment    // 创建附件(本地已存在的文件)    EmailAttachment attachment = new EmailAttachment();    attachment.setPath("mypictures/john.jpg");    attachment.setDisposition(EmailAttachment.ATTACHMENT);    attachment.setDescription("Picture of John");    attachment.setName("John");    // Create the attachment    // 创建附件(本地不存在的附件,在网络上下载)    EmailAttachment attachment = new EmailAttachment();    attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));    attachment.setDisposition(EmailAttachment.ATTACHMENT);    attachment.setDescription("Apache logo");    attachment.setName("Apache logo");    // Create the email message    // 设置邮件信息    MultiPartEmail email = new MultiPartEmail();    // 设置邮件服务器    email.setHostName("mail.163.com");    //设置认证用户名和密码    simpleEmail.setAuthentication("xxx@163.com", "xxx");    // 设置收件人邮箱地址    email.addTo("xxx@qq", "xuyi");    // 设置发件人邮箱地址    email.setFrom("me@apache.org", "Me");    email.setSubject("The picture");    email.setMsg("Here is the picture you wanted");    // add the attachment    // 添加附件到邮箱中    email.attach(attachment);    // send the email    // 发送邮件    email.send();

HtmlEmail(内嵌html代码的Email)

  // Create the email message  HtmlEmail email = new HtmlEmail();  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("Test email with inline image");  // 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();

ImageHtmlEmail(内嵌图片的html代码Email)

  // load your HTML email template  String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ....";  // define you base URL to resolve relative resource locations  URL url = new URL("http://www.apache.org");  // create the email message  ImageHtmlEmail email = new ImageHtmlEmail();  email.setDataSourceResolver(new DataSourceUrlResolver(url));  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("Test email with inline image");  // set the html message  email.setHtmlMsg(htmlEmailTemplate);  // set the alternative message  email.setTextMsg("Your email client does not support HTML messages");  // send the email  email.send();

总结

使用apache commons Email组件来发送邮件非常的简单便捷,并且可以很轻松的实现几种不同类型邮件发送。

参考

1、http://commons.apache.org/proper/commons-email/userguide.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小学科学只考88分中学怎么办 收银机关机时才上传数据是怎么办 刚做的系统玩cf卡屏怎么办 办健康证的资料掉了怎么办 刚刚办得的健康证掉了怎么办 房子都过户了银行贷不了款怎么办 我要办大病迁出应该怎么办啊? 遗产继承后户口没地迁出怎么办 安徽蒙城怎么办去韩国的签证的 夜间有人私自收停车费应该怎么办 上次摸不到环尾丝这次摸到了怎么办 法院判决书下来后对方不给钱怎么办 法院判决书下来了钱还保全么怎么办 深圳路边泊车不知道泊车编号怎么办 当事人进拘留所了我的工资怎么办 昆明公租房住满5年后怎么办 昆明公租房房子到期缴纳金怎么办 看守所犯人银行卡里钱没用完怎么办 中国邮政迟迟没有把信件寄到怎么办 拘留15天放出来还不肯还钱怎么办 人死在拘留所不让看监控怎么办 家人吸毒可他又不愿强戒怎么办 容留他人吸毒时签了强戒怎么办 拘留后发现被拘留是人大代表怎么办 执行局要拘留人找不到人怎么办 开设赌场罪拘留37天了该怎么办 对治安处罚光罚款不拘留怎么办 打架和解后警察不给消案怎么办 12个人片诈骗刑拘了28天怎么办 交警拘留几天后还是没钱赔偿怎么办 平安车主信用卡车牌号填错了怎么办 起诉借钱的人逮起来了怎么办 在法院查不到坐牢人的档案怎么办 人被冤枉敲诈勒索进看守所了怎么办 打架被拘留孩子怎么办三周了 技师被拘留她的家人该怎么办 我是担保人贷款人跑了我该怎么办 公司依旷工理由辞退我该怎么办 试工两天被辞退了不发工资怎么办 员工在单位被打监控调不出来怎么办 老师罚做60张试卷家长该怎么办