JavaMail 发微软邮箱终于成功了

package org.jeecgframework;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * Hello world!
 *
 */
public class App {
   

    public static void main(String[] args) throws Exception {


        final String username = "llfmuou@outlook.com";
        final String password = "密码";

        //收件箱
        final String to = "llfmuou@outlook.com";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        //不做服务器证书校验
        props.put("mail.smtp.ssl.checkserveridentity", "false");

        //添加信任的服务器地址,多个地址之间用空格分开
        props.put("mail.smtp.ssl.trust", "smtp-mail.outlook.com");
        props.put("mail.smtp.host", "smtp-mail.outlook.com");
        props.put("mail.smtp.port", "25");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
            message.setSubject("Email Testing Subject");
            message.setText("hello world!");

            Transport.send(message);

        } catch (MessagingException e) {


//打印错误信息
String sOut = "";
StackTraceElement[] trace = e.getStackTrace();
for (StackTraceElement s : trace) {
    sOut += "\tat " + s + "\r\n";
}
System.out.printf("....................................");
System.out.printf(sOut);
System.out.printf("....................................");
            throw new RuntimeException(e);
        }
    }

    
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值