javax.mail passwordauthentication未运行

hujrc8aj  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(241)

我一直在尝试使用javax.mail开发gmail客户端。一切似乎没有错误,但它不会执行,也不会给出例外,即使我把密码错了。
我在我的google帐户中启用了访问不太安全的应用程序。

package com.email; 
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
private String user = "khairinahizar@gmail.com";
private String pass = "kh";

SendEmail(String to, String sub, String msg) {
    Properties prop = new Properties();
    prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    prop.put("mail.smtp,auth", true);
    prop.put("mail.smtp.starttls.enable", true);
    prop.put("mail.smtp.host", "smtp.gmail.com");
    prop.put("mail.smtp.port", "587");

    Session session = Session.getInstance(prop, new javax.mail.Authenticator() 
    {
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
        {
            return new javax.mail.PasswordAuthentication(user, pass);
        }
    });
    try {
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress(user));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(sub);
        message.setText(msg);

        Transport.send(message);
    }
    catch (Exception e) {

    }
}
}

终端输出:

Compiling 2 source files to C:\Users\lenovo\Desktop\email\build\classes 
compile:
run:
BUILD SUCCESSFUL (total time: 5 seconds)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题