javamail调用阿里企业邮箱实现推送包括多个附件

公司的一个项目需要用到邮件推送,现在是交付期,浮生半日闲,我也记录一下吧.

1.搭建maven工程;

2.下载mailjar包,https://share.weiyun.com/5QyJvwv,在resources下新建lib文件目录,将jar包粘贴到里面,导入javax.mail本地依赖;

参考格式:

<!--添加本地的javax.mail.jar包 后续把这个jar包上传仓库-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/lib/mail.jar</systemPath>
        </dependency>

3.源代码:

package com.xx.xx.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Description 发送邮箱
 * @Author 
 * @Date 2019/1/14 9:43
 */
public class BusinessEmail {

    private static Log logger = LogFactory.getLog(BusinessEmail.class);

    //smtp.mxhichina.com  服务器
    private static final String host = "smtp.mxhichina.com";
    //协议
    private static final String protocol = "smtp";

    private static String name = "企业邮箱账号";
    private static String pwd = "密码";
    private static String from = "发送者,与企业邮箱账号相同";
    private static String nickName = "某某某有限公司";

    /**
     *
     * @param to    发送给谁
     * @param cc    抄送给谁
     * @param title     邮件标题
     * @param content   邮件文本,支持html标签
     * @param fileAddress   上传的附件,可以是多个,只能传本地,不能传网络文件
     */
    public static void sendEmail(String to, String cc, String title, String content, List<String> fileAddress){
        Properties properties = new Properties();
        try {
            properties.setProperty("mail.host", host);
            properties.setProperty("mail.transport.protocol", protocol);
            properties.setProperty("mail.smtp.auth","true");
            Session session = Session.getInstance(properties);
            session.setDebug(true);
            Transport transport = session.getTransport();
            transport.connect(name,pwd);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from,nickName,"utf-8"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
            message.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc));
            message.setSubject(title);

            Multipart multipart = new MimeMultipart();

            BodyPart bodyPart = new MimeBodyPart();
            bodyPart.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + content, "text/html;charset=UTF-8");
            multipart.addBodyPart(bodyPart);
            if (fileAddress != null && fileAddress.size() > 0){
                for (String file :fileAddress) {
                    BodyPart part = new MimeBodyPart();
                    FileDataSource fileDataSource = new FileDataSource(file);
                    part.setDataHandler(new DataHandler(fileDataSource));
                    //System.out.println("==========================" + fileDataSource.getName() + "============");
                    part.setFileName(MimeUtility.encodeText(fileDataSource.getName(),"utf-8",null));
                    multipart.addBodyPart(part);
                }
            }

            message.setContent(multipart,"text/html;charset=utf-8");
            transport.sendMessage(message,message.getAllRecipients());
            System.out.println("waiting....5s左右就ok");
        }catch (Exception e){
            logger.info("err in BusinessEmail.sendEmail");
        }
    }



}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值