java 阿里云邮箱发送邮件


链接: java给各个邮箱发邮件,不包括阿里邮箱发送邮件 

参考链接:阿里云邮箱发送邮件

总体流程:首先需要一个企业邮箱,及这个邮箱的密码。相当于发送者。由这个邮箱可以给任意邮箱发送邮件。

如使用QQ邮箱当作发送者,则还需要设置鉴权码。具体百度

pom.xml 引入 mail 发送邮件的依赖

<dependency>
    <groupId>javax.mailgroupId>
    <artifactId>mailartifactId>
    <version>1.4.7version>
dependency>

代码实现:

package com.systron.utils;

import org.apache.commons.lang.StringUtils;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.security.Security;
import java.util.Properties;

/**
 * 使用阿里云邮箱发送邮件工具类
 * 阿里云邮箱为联想企业邮箱
 */
public class SendMailUtil {

    public static final String ALIDM_SMTP_HOST = "smtp.qiye.aliyun.com";

    public static final String ALIDM_SMTP_PORT = "25";


    /**
     * 发送邮件
     * @param sendAddress  发件人地址
     * @param sendPassword 发件人密码
     * @param host         协议
     * @param port         端口
     * @param subject      标题
     * @param content      内容
     * @param filePath     附件地址
     * @param CC           抄送人
     * @throws Exception
     * @throws AddressException
     */
    public static void sendMail(String sendAddress, String sendPassword, String host, String port, String subject, String content, String internetAddress, String filePath, String CC) throws AddressException, Exception {
        //设置SSL连接、邮件环境
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", port);//设置端口
        props.setProperty("mail.debug", "true");//启用调试
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        //建立邮件会话
        Session session = Session.getDefaultInstance(props, new Authenticator() {   //身份认证
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sendAddress, sendPassword);//发件人账号、密码
            }
        });
        //建立邮件对象
        MimeMessage message = new MimeMessage(session);
        //设置邮件的发件人、收件人、主题
        //附带发件人名字
        //  message.setFrom(new InternetAddress("from_mail@qq.com", "optional-personal"));
        message.setFrom(new InternetAddress(sendAddress));//发件人账号
        message.setRecipients(Message.RecipientType.TO, internetAddress);//收件人账号

        //标题
        message.setSubject(subject);//邮件标题

        //内容
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(content, "text/html;charset=utf-8");//邮件内容
        multipart.addBodyPart(contentPart);

        //附件部分
        if (StringUtils.isNotEmpty(filePath)) {
            BodyPart attachPart = new MimeBodyPart();
            FileDataSource fileDataSource = new FileDataSource(filePath);//附件地址
            attachPart.setDataHandler(new DataHandler(fileDataSource));
            attachPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
            multipart.addBodyPart(attachPart);
        }

        message.setContent(multipart);

        //抄送地址
        if (StringUtils.isNotBlank(CC)) {
            InternetAddress[] internetAddressCC = new InternetAddress().parse(CC);
            message.setRecipients(Message.RecipientType.CC, internetAddressCC);
        }

        //发送邮件
        Transport.send(message);
    }

    // 测试
    public static void main(String[] args) {
//        CacheConfigUtil.getProperty("ali.email.name");
//        CacheConfigUtil.getProperty("ali.email.password");

        try {
            sendMail("发送者邮箱", "发送者邮箱密码", ALIDM_SMTP_HOST, ALIDM_SMTP_PORT, "邮件标题",
                    "邮件内容", "接收者邮箱", null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}