gpt4 book ai didi

Java邮件: sending email via gmail with TLS

转载 作者:行者123 更新时间:2023-12-02 07:45:28 25 4
gpt4 key购买 nike

我正在尝试使用我的java应用程序发送简单的电子邮件,但是当我执行发送邮件时,我在控制台上只得到“220 2.0.0 Ready to start TLS”,之后什么也没有发生。方法代码如下:

try
{
/** Recipient address **/
String messageRecipient = "@live.com";

String pass = "";
/** sender address **/
String messageSender = "@gmail.com";

/** get my properties **/
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.user", messageSender);
properties.put("mail.smtp.password", pass);
properties.put("mail.debug", "true");
properties.put("mail.smtp.debug", "true");
properties.put("mail.smtp.port", "587");

/** get default session object **/
Session session = Session.getDefaultInstance(properties,// null);
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("@gmail.com", "");
}
});

try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(messageSender));

//set to: header field of the header
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(messageRecipient));
//set subject: header field
message.setSubject("Welcome to java mail!");

//send message
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", 587, messageSender, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

System.out.println(Errors.MESSAGE_SENT_SUCCESSFULLY);
return null;
}
catch(MessagingException mex)
{
return Errors.MESSAGE_NOT_SENT;
}
}
catch(Exception e)
{
return Errors.FILE_NOT_FOUND;
}
}

并在控制台中调试输出:

     DEBUG: JavaMail version 1.4.5
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSS LTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTranspo rt,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Su n Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Su n Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP e20sm6680361wiv.7
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO gadon-Komputer
250-mx.google.com at your service, [83.22.91.238]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 mx.google.com ESMTP et10sm6688757wib.2
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO gadon-Komputer
250-mx.google.com at your service, [83.22.91.238]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
STARTTLS
220 2.0.0 Ready to start TLS

有人能告诉我这是怎么回事吗?我阅读了数百个有关 javamail 的主题,但找不到我的问题的答案。如果我更改密码,输出不会发生任何变化。

最佳答案

您可以使用Gmail example in the JavaMail FAQ来简化您的代码。 (不要设置端口号,使用“smtps”协议(protocol),并摆脱身份 validator 。)此外,change getDefaultInstance to getInstance .

关于Java邮件: sending email via gmail with TLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com