apache的commons-email 类库开发示例

1 篇文章 0 订阅
1 篇文章 0 订阅

利用apache的commons-email类库发送邮件示例代码,commons-email类库地址:http://commons.apache.org/proper/commons-email/

1、maven应用

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>

2、示例代码 

package com.demo.email;

import org.apache.commons.mail.*;
import org.apache.commons.mail.resolver.DataSourceUrlResolver;

import java.net.URL;

/**
 * Created by wuguowei on 2018/7/30.
 */
public class TestApacheEmail {
    private static String sHostName = "smtp.mxhichina.com";
    private static int nSmtpPort = 465;
    private static String sUserName = "your@email.com";
    private static String sPassword = "youreamilpassword";
    private static String sFromEmail = "your@email.com";
    private static String sToEmail = "receive@qq.com";


    public static void main(String[] args) throws Exception {
        //sendSimpleEmail();
        //sendEmailAttachment();
        //sendUrlAttachmentEmail();
        //sendHtmlEmail();
        sendInnerImageHtmlEmail();
    }

    /**
     * 发送纯文本邮件
     *
     * @throws Exception
     */
    public static void sendSimpleEmail() throws Exception {
        Email email = new SimpleEmail();
        email.setHostName(sHostName);
        email.setSmtpPort(nSmtpPort);
        email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
        email.setSSLOnConnect(true);
        email.setFrom(sFromEmail);
        email.setSubject("sendSimpleEmail");
        email.setMsg("This is sendSimpleEmail ... :-)");
        email.addTo(sToEmail);
        email.send();
    }

    /**
     * 发送附件邮件
     *
     * @throws Exception
     */
    public static void sendEmailAttachment() throws Exception {
        // Create the attachment
        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath("/Users/wuguowei/Downloads/baidu.png");
        //发送url地址
        //attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription("Picture of John");
        attachment.setName("John");

        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName(sHostName);
        email.setSmtpPort(nSmtpPort);
        email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
        email.setSSLOnConnect(true);
        email.setFrom(sFromEmail, "Me");
        email.addTo(sToEmail);
        email.setSubject("The sendEmailAttachment");
        email.setMsg("Here is sendEmailAttachment");

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

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

    /**
     * 发送远程的url地址的图片附件
     *
     * @throws Exception
     */
    private static void sendUrlAttachmentEmail() throws Exception {
        // Create the attachment
        EmailAttachment attachment = new EmailAttachment();
        //发送url地址
        attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription("Picture of John");
        attachment.setName("John");

        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName(sHostName);
        email.setSmtpPort(nSmtpPort);
        email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
        email.setSSLOnConnect(true);
        email.setFrom(sFromEmail, "Me");
        email.addTo(sToEmail);
        email.setSubject("sendUrlAttachmentEmail");
        email.setMsg("Here is sendUrlAttachmentEmail");

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

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

    /**
     * 发送htmlEmail
     *
     * @throws Exception
     */
    private static void sendHtmlEmail() throws Exception {
        // Create the email message
        HtmlEmail email = new HtmlEmail();
        email.setHostName(sHostName);
        email.setSmtpPort(nSmtpPort);
        email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
        email.setSSLOnConnect(true);
        email.setFrom(sFromEmail, "Me");
        email.addTo(sToEmail);
        email.setSubject("sendHtmlEmail");

        // 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();
    }

    /**
     * 发送htmlEmail,把图片转换为内部图片发送邮件
     *
     * @throws Exception
     */
    private static void sendInnerImageHtmlEmail() throws Exception {
        // load your HTML email template,可以cid的方式,或者是具体的url地址
        String htmlEmailTemplate = "<html>The apache logo - <img src=\"http://commons.apache.org/proper/commons-email/images/commons-logo.png\"></html>";

        // define you base URL to resolve relative resource locations
        URL url = new URL("http://www.apache.org");

        // create the email message
        DataSourceUrlResolver dataSourceUrlResolver = new DataSourceUrlResolver(url);
        ImageHtmlEmail email = new ImageHtmlEmail();
        email.setDataSourceResolver(dataSourceUrlResolver);
        email.setHostName(sHostName);
        email.setSmtpPort(nSmtpPort);
        email.setAuthenticator(new DefaultAuthenticator(sUserName, sPassword));
        email.setSSLOnConnect(true);
        email.setFrom(sFromEmail, "Me");
        email.addTo(sToEmail);
        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();
    }


}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值