`

Commons Email

    博客分类:
  • JAVA
 
阅读更多

(1)下载Commons Email 

Commons Email位于Apache的Commons下。

地址是:http://commons.apache.org/email/  

下载地址是:http://commons.apache.org/downloads/download_email.cgi

(2)官方简介:

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 the capabilities 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 of attachments . It is for use with instances of MultiPartEmail and HtmlEmail.

(3)

Commons-Email 简单教程

Commons Emails ,目的是为了简化JavaMail。

知道有它几个class吗?你一定想不到,只有8个!

一:Quick Start
通过SimpleEmail发送邮件

1 java.lang.Object
2    org.apache.commons.mail.Email
3        org.apache.commons.mail.SimpleEmail

 

1 SimpleEmail email  =   new  SimpleEmail();
2  email.setHostName( " mail.4ya.cn " );
3  email.setAuthentication( " <username> " , " <password> " )
4  email.addTo( " martin.xus@gmail.com " ,  " martin " );
5  email.setFrom( " martin@4ya.cn " ,  " martin " );
6  email.setSubject( " 测试主题 " );
7  email.setMsg( " 这里是邮件内容 " );
8  email.send();


就如代码里字面上的意思一样简单:
1:创建以SimpleEmail对象
2:设定发送信件的smtp服务器,如果没有设定,会寻找系统变量中mail.host值。
3:设定smtp的用户和密码
4:收件人
5:发件人
6:主题
7:内容
8:发送

二:发送带附件的邮件
我们可以发送本机的附件,当然我们也可以发送非本机的附件,如果发送的是一个存在网络上的附件的url,则邮件发送的时候会自动下载,添加到附件中。

   1:)发送本地附件:

1 EmailAttachment attachment  =   new  EmailAttachment();
2  attachment.setPath( " test/test.rar " );
3  attachment.setDisposition(EmailAttachment.ATTACHMENT);
4  attachment.setDescription( " python resource " );
5  attachment.setName( " resource " );


   2:)发送不存在本地的附件

1 EmailAttachment attachment  =   new  EmailAttachment();
2  attachment.setURL( new  URL( " http://www.smilinglibrary.org/sldoc/pics/index03.jpg " ));
3  attachment.setDisposition(EmailAttachment.ATTACHMENT);
4  attachment.setDescription( " 微笑图书馆 " );
5  attachment.setName( " 微笑图书馆 " );



next,添加附件到我们的邮件中

 1 MultiPartEmail email  =   new  MultiPartEmail();
 2  email.setHostName( " mail.4ya.cn " );
 3    email.setAuthentication(" <username> " , " <password> " ) 
 4  email.addTo( " martin.xus@gmail.com " ,  " martin " );
 5  email.setFrom( " martin@4ya.cn " ,  " martin " );
 6  email.setSubject( " 邮件主题 " );
 7  email.setMsg( " 邮件内容 " );

 8  // 添加附件 
 9  email.attach(attachment);
10 
11  // 发送邮件 
12  email.send();


如果需要发送多个附件,只需创建多个EmailAttachement,即可

1 email.attach(attachment1)
2  email.attach(attachment2)


三:发送html格式的邮件
通过HtmlEmail我们可以发送Html格式的邮件:

1 java.lang.Object
2    org.apache.commons.mail.Email
3        org.apache.commons.mail.MultiPartEmail
4            org.apache.commons.mail.HtmlEmail
5 


如下:

 1 // HtmlEmail! 
 2  HtmlEmail email  =   new  HtmlEmail();
 3  email.setHostName( " mail.4ya.cn " );
 3   email.setAuthentication(" <username> " , " <password> " ) 
 5  email.addTo("martin@4ya.cn " martin " );
 6  email.setFrom("martin.xus@gmail.com " martin " );
 7  email.setSubject( "主题:该邮件包括html格式内容 " );
  
 8  //  embed the image and get the content id
 9  //  注意这里:embed 将帮助我们创建标签如:cid:xxx url 
10  URL url  =   new  URL( " http://www.apache.org/images/asf_logo_wide.gif " );
11  String cid  =  email.embed(url,  " Apache logo " );
12 
13  /** 
14  set the html message
15  我们看到HtmlEmail extends Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码
16  * */ 
17  email.setHtmlMsg( " <html>The apache logo - <img src=\ " cid: " +cid+ " \ " ></html> " );
18 
19  //  set the alternative message 
20  email.setTextMsg( " Your email client does not support HTML messages " );
21  [iocblog.net 来源]
22  // set mail 
23  email.send();
24 


四:最后一步 
如果需要实现更复杂authenticator 你可以extends javax.mail.Authenticator ,实现你自己的东西,然后调用Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可

这一点jakarta也做了,给我们提供了一个defaultAuthenticator

1 java.lang.Object
2    javax.mail.Authenticator
3        org.apache.commons.mail.DefaultAuthenticator


覆盖掉该方法,实现你自己的东东 o_o

1 protected  javax.mail.PasswordAuthentication getPasswordAuthentication()

分享到:
评论

相关推荐

    Java通过Apache提供的Commons Email工具类实现邮箱发送验证码

    Apache Commons Email库为Java开发者提供了一个便捷的方式来实现这个功能。本篇将深入讲解如何使用Apache Commons Email工具类来发送带有验证码的电子邮件。 首先,Apache Commons Email是Apache软件基金会的一个...

    commons email 发送html邮件完整优化实例

    Apache Commons Email 是一个Java库,专门用于简化电子邮件的发送。这个库提供了丰富的API,使得开发者可以轻松地构建和发送各种类型的邮件,包括HTML格式的邮件。在这个“commons email 发送html邮件完整优化实例”...

    新版邮件编程---Apache Commons Email

    Apache Commons Email是一个强大的Java库,专门用于简化电子邮件的创建和发送。这个库是Apache软件基金会的一个项目,旨在提供一个简单、直接的API,使得开发者在处理邮件相关的编程任务时能够更加便捷。Apache ...

    commons email,另附所需框架,导入即可

    Apache Commons Email 是一个Java库,专门用于简化电子邮件的创建和发送。这个库是Apache Commons项目的一部分,它提供了许多实用工具和方法,使得开发者在处理电子邮件时可以避免直接操作复杂的JavaMail API。...

    Commons: 封装Apache Commons Email简化邮件发送

    Apache Commons Email是Java开发中一个非常实用的工具库,它为简化邮件发送提供了丰富的功能。这个库封装了JavaMail API,使得开发者可以更加方便、高效地处理电子邮件的发送任务,而无需深入理解复杂的邮件协议和...

    commons email项目

    Apache Commons Email 是一个Java库,它为创建和发送电子邮件提供了简单和强大的API。这个项目旨在简化处理SMTP(简单邮件传输协议)和其他邮件协议的过程,使开发者可以轻松地在应用程序中集成电子邮件功能。在这个...

    commons email

    commons email commons email commons email

    commons-email-1.5.jar

    《Android应用中使用Apache Commons Email库发送邮件》 在Android应用程序开发中,有时我们需要集成邮件发送功能,以便向用户发送验证码、通知或者提供其他服务。Apache Commons Email库是一个强大的Java库,它简化...

    commons-email jar

    Commons Email是一个Java库,专门设计用于简化电子邮件的创建和发送。这个jar包在Web应用程序开发中非常常见,因为它提供了一套强大的API,帮助开发者轻松处理复杂的邮件功能。下面将详细介绍这个包及其相关组件。 ...

    用commons-email-1.2.jar实现发邮件功能

    在Java编程中,发送电子邮件是一项常见的任务,Apache Commons Email库为开发者提供了便利的API来实现这一功能。本篇文章将详细讲解如何使用`commons-email-1.2.jar`这个开源库来实现邮件发送,并通过`...

    利用Apache Commons Email发送邮件

    这份资料是+++利用Apache Commons Email发送邮件+++的内容,希望能给有志成为IT人才一点帮助!你的进步就是对我最大的回报! Thank you!

    邮件发送--commons-email-1.4.jar

    在Java编程环境中,发送电子邮件是一项常见的任务,而`commons-email-1.4.jar`是Apache Commons Email库的一个版本,这个库专门设计用于简化Java中的邮件发送。Apache Commons Email是Apache软件基金会开发的一个...

    commons-email-1.4.jar

    《Apache Commons Email 1.4:构建高效电子邮件的利器》 Apache Commons Email 是一个Java库,专注于简化创建和发送电子邮件的过程。在Java编程中,处理电子邮件可能会涉及到复杂的SMTP协议和其他技术细节,而...

    commons-email发送邮件

    Apache Commons Email 是一个Java库,专门用于简化电子邮件的发送。这个库是Apache Commons项目的一部分,提供了许多方便的API,使得开发者能够轻松地构建并发送包含文本、HTML内容以及附件的邮件。下面我们将深入...

    Java Mail和Commons-email发简单的邮件所用到的jar包

    JavaMail和Apache Commons Email是Java平台上用于发送电子邮件的两个重要库。这两个库为开发者提供了方便的API,使得在Java应用程序中实现邮件发送功能变得简单易行。以下将详细阐述这两个库以及它们所需的jar包。 ...

    commons-email-1.2.jar/email.jar/activation.jar

    在Java编程领域,发送电子邮件是一项常见的任务,而`commons-email-1.2.jar`和`activation.jar`这两个库文件在其中扮演着至关重要的角色。Apache Commons Email是Apache Commons项目的一部分,提供了一个简单、直观...

    commons-email需要的几个包

    Apache Commons Email 是一个Java库,专门用于简化电子邮件的创建和发送。这个库是Apache Commons项目的一部分,它提供了许多高级功能,使得开发人员可以方便地处理复杂的邮件操作,而不需要直接与JavaMail API进行...

    mail.jar、commons-email-1.2.jar 、activation.jar下载

    "mail.jar、commons-email-1.2.jar、activation.jar下载"这个标题暗示了我们正在处理用于处理电子邮件功能的Java库。这些库对于使用Java编程语言构建邮件发送功能至关重要。 1. **JavaMail API**(mail.jar): ...

    commons-email-1.3.1-bin

    《Apache Commons Email 1.3.1:Java 邮件发送的核心库》 Apache Commons Email 是一个由 Apache 软件基金会开发的 Java 库,它的主要目标是简化 Java 程序中的电子邮件发送。在Java编程中,邮件发送是一项常见的...

Global site tag (gtag.js) - Google Analytics