设为首页 收藏本站
查看: 633|回复: 0

[经验分享] 使用Apache Commons Email 发生邮件

[复制链接]
发表于 2017-12-24 06:36:55 | 显示全部楼层 |阅读模式
package app.hihn.me.mail.demo;  

  
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.ImageHtmlEmail;
  
import org.apache.commons.mail.MultiPartEmail;
  
import org.apache.commons.mail.SimpleEmail;
  
import org.apache.commons.mail.resolver.DataSourceUrlResolver;
  

  
/**
  
  *
  
  * <pre>
  
  * <h1>Debugging</h1>
  
  * The JavaMail API supports a debugging option that will can be very
  
  * useful if you run into problems. You can activate debugging on any of the

  
  * mail>  
  * to System.out.
  
  *
  
  * Sometimes you want to experiment with various security setting or features of

  
  * commons-email. A good starting point is the test>  
  * EmailConfiguration which are used for testing commons-email with real SMTP
  
  * servers.
  
  *
  
  * </pre>
  
  *
  
  * @describe Apache Commons Email 使用示例
  
  * @author liwen
  
  *
  
  */

  
public>  
     /**
  
      * <pre>
  
      *
  
      * Our first example will create a basic email message to "John Doe" and
  
      * send it through your Google Mail (GMail) account.
  
      *
  
      * The call to setHostName("mail.myserver.com") sets the address of the outgoing SMTP
  
      * server that will be used to send the message. If this is not set, the
  
      * system property "mail.host" will be used.
  
      *
  
      * </pre>
  
      *
  
      * @describe 发送内容为简单文本的邮件
  
      * @throws EmailException
  
      */
  
     public static void sendSimpleTextEmail() throws EmailException {
  
         Email email = new SimpleEmail();
  
         email.setHostName("smtp.googlemail.com");
  
         email.setSmtpPort(465);
  
         // 用户名和密码为邮箱的账号和密码(不需要进行base64编码)
  
         email.setAuthenticator(new DefaultAuthenticator("username", "password"));
  
         email.setSSLOnConnect(true);
  
         email.setFrom("user@gmail.com");
  
         email.setSubject("TestMail");
  
         email.setMsg("This is a test mail ... :-)");
  
         email.addTo("foo@bar.com");
  
         email.send();
  
     }
  

  
     /**
  
      * <pre>
  
      *
  
      * To add attachments to an email, you will need to use the MultiPartEmail

  
      *>  
      * overloaded attach() methods to add attachments to the email. You can add
  
      * an unlimited number of attachments either inline or attached. The
  
      * attachments will be MIME encoded.
  
      *
  
      * The simplest way to add the attachments is by using the EmailAttachment

  
      *>  
      *
  
      * In the following example, we will create an attachment for a picture. We
  
      * will then attach the picture to the email and send it.
  
      *
  
      * </pre>
  
      *
  
      * @describe 发送包含附件的邮件(附件为本地资源)
  
      * @throws EmailException
  
      */
  
     public static void sendEmailsWithAttachments() throws EmailException {
  
         // 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 email message
  
         MultiPartEmail email = new MultiPartEmail();
  
         email.setHostName("mail.myserver.com");
  
         email.addTo("jdoe@somewhere.org", "John Doe");
  
         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();
  
     }
  

  
     /**
  
      * <pre>
  
      *
  
      * You can also use EmailAttachment to reference any valid URL for files
  
      * that you do not have locally. When the message is sent, the file will be
  
      * downloaded and attached to the message automatically.
  
      *
  
      * The next example shows how we could have sent the apache logo to John
  
      * instead.
  
      *
  
      * </pre>
  
      *
  
      * @describe 发送包含附件的邮件(附件为在线资源)
  
      * @throws EmailException
  
      * @throws MalformedURLException
  
      */
  
     public static void sendEmailsWithOnlineAttachments() throws EmailException, MalformedURLException {
  
         // 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.myserver.com");
  
         email.addTo("jdoe@somewhere.org", "John Doe");
  
         email.setFrom("me@apache.org", "Me");
  
         email.setSubject("The logo");
  
         email.setMsg("Here is Apache's logo");
  

  
         // add the attachment
  
         email.attach(attachment);
  

  
         // send the email
  
         email.send();
  
     }
  

  
     /**
  
      * <pre>
  
      *
  
      * Sending HTML formatted email is accomplished by using the HtmlEmail

  
      *>
  
      * additional methods to set the html content,>  
      * the recipient does not support HTML email, and add inline images.
  
      *
  
      * In this example, we will send an email message with formatted HTML
  
      * content with an inline image.
  
      *
  
      * First, notice that the call to embed() returns a String. This String is a

  
      * randomly generated>  
      * the image tag.
  
      *
  
      * Next, there was no call to setMsg() in this example. The method is still
  
      * available in HtmlEmail but it should not be used if you will be using
  
      * inline images. Instead, the setHtmlMsg() and setTextMsg() methods were
  
      * used.
  
      *
  
      * <pre>
  
      *
  
      * @describe 发送内容为HTML格式的邮件
  
      * @throws EmailException
  
      * @throws MalformedURLException
  
      */
  
     public static void sendHTMLFormattedEmail() throws EmailException, MalformedURLException {
  
         // 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>  
         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>  
         email.setTextMsg("Your email client does not support HTML messages");
  

  
         // send the email
  
         email.send();
  
     }
  

  
     /**
  
      * <pre>
  
      *
  
      * The previous example showed how to create a HTML email with embedded
  
      * images but you need to know all images upfront which is inconvenient when
  
      * using a HTML email template. The ImageHtmlEmail helps you solving this
  
      * problem by converting all external images to inline images.
  
      *
  
      * First we create a HTML email template referencing some images. All
  
      * referenced images are automatically transformed to inline images by the
  
      * specified DataSourceResolver.
  
      *
  
      * </pre>
  
      *
  
      * @describe 发送内容为HTML格式的邮件(嵌入图片更方便)
  
      * @throws MalformedURLException
  
      * @throws EmailException
  
      */
  
     public static void sendHTMLFormattedEmailWithEmbeddedImages() throws MalformedURLException, EmailException {
  
         // load your HTML email template
  
         String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ....";
  


  
         // define you base URL to resolve>  
         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>  
         email.setTextMsg("Your email client does not support HTML messages");
  

  
         // send the email
  
         email.send();
  
     }
  

  
     public static void main(String[] args) throws EmailException {
  
         Email email = new SimpleEmail();
  
         email.setHostName("smtp.163.com");
  
         email.setSmtpPort(465);
  
         email.setAuthenticator(new DefaultAuthenticator("liwenzlw@163.com", "xxxxxxxxxxxxx"));
  
         email.setSSLOnConnect(true);
  
         email.setFrom("liwenzlw@163.com", "利文");
  
         email.setSubject("异常信息");
  
         email.setMsg("This is a test mail ... :-)");
  
         email.addTo("1309928657@qq.com");
  
         email.send();
  
     }
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-427393-1-1.html 上篇帖子: Springboot集成权限管理框架apache shiro 下篇帖子: linux下apache和tomcat整合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表