我正在制作一个可以发送电子邮件的网络应用程序,在此代码中我使用了雅虎邮件发送。
我尝试了一些解决方案,但他们没有我太多:
这是我的代码:
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com"); // for gmail use smtp.gmail.com
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("my_mail@yahoo.com.vn", "my_pwd");
}
});
mailSession.setDebug(true); // Enable the debug mode
Message msg = new MimeMessage(mailSession);
//--[ Set the FROM, TO, DATE and SUBJECT fields
msg.setFrom(new InternetAddress("my_mail@yahoo.com.vn"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("other@mail"));
msg.setSentDate(new Date());
msg.setSubject("Hello World!");
//--[ Create the body of the mail
msg.setText("Hello from my first e-mail sent with JavaMail");
//--[ Ask the Transport class to send our mail message
Transport.send(msg);
return true;
} catch (Exception E) {
System.out.println("Oops something has gone pearshaped!");
System.out.println(E);
return false;
}
这是调试代码:
DEBUG: JavaMail version 1.4.2
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.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun 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: setDebug: JavaMail version 1.4.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 465, isSSL false
220 smtp.mail.yahoo.com ESMTP ready
DEBUG SMTP: connected to host "smtp.mail.yahoo.com", port: 465
EHLO DESKTOP-132ABCD
250-smtp.mail.yahoo.com
250-PIPELINING
250-SIZE 41697280
250-8 BITMIME
250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41697280"
DEBUG SMTP: Found extension "8", arg "BITMIME"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XYMCOOKIE"
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
AUTH LOGIN
//some code
535 5.7.0 (#MBR1240) Please verify your account by going to https://login.yahoo.com
Oops something has gone pearshaped!
javax.mail.AuthenticationFailedException: failed to connect
请帮助我,非常感谢你!
答案 0 :(得分:5)
我使用你的java代码发送电子邮件而不是我使用我的Gmail帐户。 在第一次尝试时,我也得到了“javax.mail.AuthenticationFailedException:534-5.7.14”。
发生此异常是因为Google会关闭安全性较低的应用的访问权限。之后我转到此link。
按照说明打开安全性较低的应用后,我就可以发送邮件了。
由于您使用的是雅虎邮件,因此您可以尝试使用此link来启用安全性较低的应用,方法是启用“允许使用较少安全登录的应用”选项。
答案 1 :(得分:2)
几周前,我遇到了与GMail相同的问题。原来,GMail不允许您发送带有“不太安全”的应用程序的电子邮件,除非您在GMail的帐户安全选项中激活它。
雅虎具有相同的安全标准,因此您需要访问https://login.yahoo.com/account/security并启用“允许使用安全性较低的应用”,然后才能在Java应用中使用该帐户。