java mail 发outlook邮件

package com.tensquare.gathering;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class JavaxJavaMailClient {
    public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
        JavaxJavaMailClient javaxJavaMailClient = new JavaxJavaMailClient();
        javaxJavaMailClient.clientTestA();
    }

    public String emailHost = "smtp.office365.com";       //发送邮件的主机
    public String transportType = "smtp";           //邮件发送的协议
    public String fromUser = "[email protected]";           //发件人名称
    public String fromEmail = "[email protected]";  //发件人邮箱
    public String authCode = "pwd";    //发件人邮箱授权码
    public String toEmail = "[email protected]";   //收件人邮箱
    public String subject = "电子专票开具";           //主题信息

    public void clientTestA() throws UnsupportedEncodingException, javax.mail.MessagingException {

        //初始化默认参数
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", emailHost);
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.user", fromUser);
        props.setProperty("mail.smtp.from", fromEmail);
        props.setProperty("mail.smtp.starttls.enable", "true"); // 启用TLS加密
        // props.setProperty("mail.smtp.ssl.enable", "true"); // 启用TLS加密
        //获取Session对象
        Session session = Session.getInstance(props, null);
        //开启后有调试信息
        session.setDebug(true);

        //通过MimeMessage来创建Message接口的子类
        MimeMessage message = new MimeMessage(session);
        String formName = MimeUtility.encodeWord("蚂蚁小哥") + " <" + fromEmail + ">";
        InternetAddress from = new InternetAddress(formName);
        message.setFrom(from);

        //设置收件人:
        InternetAddress to = new InternetAddress(toEmail);
        message.setRecipient(Message.RecipientType.TO, to);

        //设置抄送人(两个)可有可无抄送人:
        List addresses = Arrays.asList(new InternetAddress("[email protected]"), new InternetAddress("[email protected]"));
        InternetAddress[] addressesArr = (InternetAddress[]) addresses.toArray();
        message.setRecipients(Message.RecipientType.CC, addressesArr);

        //设置密送人 可有可无密送人:
        //InternetAddress toBCC = new InternetAddress(toEmail);
        //message.setRecipient(Message.RecipientType.BCC, toBCC);

        //设置邮件主题
        message.setSubject(subject);

        //设置邮件内容,这里我使用html格式,其实也可以使用纯文本;纯文本"text/plain"
        message.setContent("

蚂蚁小哥祝大家工作顺利!

", "text/html;charset=UTF-8"); //保存上面设置的邮件内容 message.saveChanges(); //获取Transport对象 Transport transport = session.getTransport(); //smtp验证,就是你用来发邮件的邮箱用户名密码(若在之前的properties中指定默认值,这里可以不用再次设置) transport.connect(null, null, authCode); //发送邮件 transport.sendMessage(message, message.getAllRecipients()); // 发送 } }

你可能感兴趣的:(springboot,java,outlook,服务器)