![](/img/trans.png)
[英]AuthenticationFailedException: failed to connect error while sending email
[英]AuthenticationFailedException error while sending SMTP email
我嘗試在java中發送SMTP電子郵件,但我有這樣的錯誤,我沒有收到郵件。 我關閉所有防火牆和防病毒軟件。
錯誤:
javax.mail.AuthenticationFailedException: 534-5.7.14<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuIW
534-5.7.14 tAxHxbq4WR-vUuV1uOFAvx8NhInfxYTyNHi_8sJ80lX5lBdBla2ROiSKoysMNcFoQ6sGe
534-5.7.14 DUh173tDMolJ64W-Rahx1fhVF_08AvWrphibgQXiyyz5U1FNMMb-eGGJlUIbjyvBgQuZY6
534-5.7.14 tnykIXdVn__mg87aOmtxoss-EiFYeKdvuiBbt5eb9t_NOc97h-PkXOco-9FcYW69Iz9CTu
534-5.7.14 rfyhlo24k9oqIiWtcJwv85oUCO2g> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 pd8sm1306363pdb.93 - gsmtp
這是我的代碼:
private void btn_mailActionPerformed(java.awt.event.ActionEvent evt) {
String to = "receive.address@gmail.com";
String from = "send.address@gmail.com";
final String username = "send.address";
final String password = "sendpassword";
String host = "smtp.gmail.com";
Properties pro = new Properties();
pro.put("mail.smtp.host",host);
pro.put("mail.smtp.socketFactory.port","465");
pro.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
pro.put("mail.smtp.auth","true");
pro.put("mail.smtp.port","465");
Session session = Session.getInstance(pro,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,password);
}
}
);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("test mail");
message.setText("Hello how are you?");
Transport.send(message);
JOptionPane.showMessageDialog(null,"Send");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,e.toString());
System.out.println(e.toString());
}
我遇到過同樣的問題。 在使用Gmail進行大量測試后,我發現問題是Gmail需要OAuth登錄,而不僅僅是密碼。 解決方案是使用Gmail API 。 但是,這是一個非常復雜的解決方案,我不會詳細介紹。 如果您對此感興趣,請在此處閱讀第一個答案。
但是,如果您想要一個簡單的解決方案,我所做的只是切換到雅虎帳戶。 由於雅虎不使用相同的加密技術,因此效果非常好。 注意:不要忘記將SMTP服務器更改為“smtp.mail.yahoo.com”,將端口更改為“25”。
如果要從頭開始設置,只需按照本教程下載JavaMail API和Java Activation Framework即可 。
然后你可以復制並粘貼我的代碼,更改頂部變量,一切都應該工作! 如果我遺漏了什么,請告訴我! 謝謝!
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mailer {
public static void main(String[] args) {
final String username = "your-email@yahoo.com";
final String password = "your-password";
final String recipient = "email-recipient";
final String subject = "message-subject";
final String emailmessage = "message";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.mail.yahoo.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(recipient));
message.setSubject(subject);
message.setText(emailmessage);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.