[英]Sending email using java authentication error
我已經看到很多關於發送電子郵件和主機是 gmail 的身份驗證錯誤的錯誤。 我也在沒有身份驗證的情況下嘗試了不同的屬性,但仍然沒有任何反應。 我不知道為什么在其他教程中,此代碼有效,但當我嘗試在此處運行它時,出現身份驗證錯誤。 我已經導入了我的庫,但它仍然是錯誤的。 我也嘗試了不同的 gmail 帳戶,但沒有任何反應。 我試過的所有帳戶都經過驗證。 怎么了? 這是代碼:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail {
public static void main(String[] args) {
Properties properties = new Properties();
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.port", "587");
Session session = Session.getDefaultInstance(properties, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("mine@gmail.com", "minepass");
}
});
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yours@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("theirs@yahoo.com"));
message.setSubject("Send meessage");
message.setText("Email received");
Transport.send(message);
System.out.println("Sent");
}
catch(MessagingException e){
throw new RuntimeException(e);
}
}
}
這是輸出日志:
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp
at SendEmail.main(SendEmail.java:40)
Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/answer/14257 hg3sm8230925pbb.52 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.main(SendEmail.java:35)
如果您使用 gmail 帳戶發送電子郵件 (SMTP),請確保您在應用程序中擁有正確的電子郵件密碼,並啟用此設置以允許您的 gmail 帳戶使用安全性較低的應用程序。
允許安全應用-->去管理賬戶設置-->左側導航點擊安全-->然后啟用不太安全的應用。
確保您已啟用從外部和未經認證的應用程序連接到您的 gmail 帳戶。 有關啟用安全性較低的應用程序以連接到您的 Google 帳戶的更多信息,請查看: https ://support.google.com/accounts/answer/6010255?hl=en 這可能是您從一開始就存在的問題,啟用安全性較低的應用程序以連接到您的 gmail 帳戶並重新運行您的應用程序。
如果這無助於嘗試將username
和password
放入屬性中,則無需使用Authenticator
。 這是能夠發送多封電子郵件的方法的工作示例。
注意:它需要javax.mail
庫
方法參數:
from
- 是您的電子郵件地址(來自 gmail)
pass
- 您的密碼
to
- 一組收件人
subject
- 消息標題
body
- 消息正文
public static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.