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 = "[email protected]";
        final String password = "密码";

        //收件箱
        final String to = "[email protected]";

        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);
        }
    }

    
}

猜你喜欢

转载自blog.csdn.net/u013208718/article/details/81228714