• Commons Email使用


    Apache Commons Email

    Apache的一个开源项目,是基于另一个开源项目Java Mail上进行封装的,使用起来更加简单方便:

    http://commons.apache.org/proper/commons-email/index.html

    首先下载jar包:commons-email-1.5.jar

           activation.jar mail.jar

    1.简单文本邮件发送

    package com.fpc.Test;
    
    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.SimpleEmail;
    
    public class CommonsEmail {
        public static void sendEmail() {
            SimpleEmail email = new SimpleEmail();
    //        email.setTLS(true);
            //email.setSSL(true);
            email.setDebug(true);
            email.setHostName("smtp.163.com");
            email.setAuthenticator(new DefaultAuthenticator("15755502569@163.com","aa892475"));
            try {
                email.setFrom("15755502569@163.com");
                email.addTo("18500408772@163.com");
                email.addCc("1448433741@qq.com");
                email.setCharset("GB2312");
                email.setSubject("2017/11/29");
                email.setMsg("看到邮件速度到会议室来开会!");
                email.send();
                System.out.println("邮件发送成功");
            } catch (EmailException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        public static void main(String[] args) {
            CommonsEmail.sendEmail();
        }
    }

    注:

    • email.setHostName("smtp.163.com"); 协议主机
    • 使用不同的服务商邮箱,这里的HostName需要改一下,同时安全校验也是不同的 setTLS,setSSL
    • email.setDebug(true);开启debug模式,可以打印一些信息。

     2.带附件的邮件发送

    MultiPartEmail EmailAttachment

    package com.fpc.Test;
    
    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.MultiPartEmail;
    import org.apache.commons.mail.SimpleEmail;
    
    public class CommonsEmail {
        public static void sendEmail() {
            MultiPartEmail email = new MultiPartEmail();
    //        email.setTLS(true);
            //email.setSSL(true);
            email.setDebug(true);
            email.setHostName("smtp.163.com");
            email.setAuthenticator(new DefaultAuthenticator("15755502569@163.com","aa892475"));
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("C:\Users\Administrator\Desktop\test.xml");
            attachment.setDescription(EmailAttachment.ATTACHMENT);
            attachment.setDescription("test xml file");
            attachment.setName("test xml");
            try {
                email.setFrom("15755502569@163.com");
                email.addTo("18500408772@163.com");
                email.addCc("1448433741@qq.com");
                email.setCharset("GB2312");
                email.setSubject("2017/11/29");
                email.setMsg("看到邮件速度到会议室来开会!");
    //            email.attach(attachment);
                email.attach(attachment);//添加附件
                email.send();
                System.out.println("邮件发送成功");
            } catch (EmailException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        public static void main(String[] args) {
            CommonsEmail.sendEmail();
        }
    }
  • 相关阅读:
    Git 中 .gitignore 的配置语法
    DMX512协议
    k8s 报错总结
    yum 源配置
    docker 安装 docker-compose
    docker 搭建 Harbor 仓库
    linux 远程执行命令
    远程从服务器A拷贝文件到服务器B
    docker 搭建私服仓库
    awk和xargs清除docker 容器、镜像
  • 原文地址:https://www.cnblogs.com/fangpengchengbupter/p/7919441.html
Copyright © 2020-2023  润新知