![](/img/trans.png)
[英]Java Send Mail : javax.mail.AuthenticationFailedException
[英]java mail javax.mail.AuthenticationFailedException
嗨,我已經編寫了使用Java郵件從服務器發送郵件的代碼
這是我的代碼
Properties pros = new Properties();
pros.put("mail.smtp.host", "my Ip Adress");
pros.put("mail.smtp.auth", "true");
pros.put("mail.smtp.port", "25");
Session mailSession = Session.getDefaultInstance(pros,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("emailId","password");
}
});
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("sendingAddress"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recieverAddress"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
return "SUCCESS";
我在代碼中提供的所有詳細信息都是正確的,我的意思是屬性設置,我已經檢查了它。 但不幸的是,它顯示了一些錯誤消息,例如身份驗證失敗
錯誤消息是這樣的
javax.mail.AuthenticationFailedException
有人對此有任何想法嗎?
我正在為我的項目使用struts2框架。
使用此代碼..! 您正在使用從郵件服務器獲取身份驗證的傳統方法
import java.util.*;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
public void Mail(String to[], String subject, String content_message)
throws MessagingException {
boolean debug = false;
try {
String host = "smtp.gmail.com";
String from = "yourmail@gmail.com";
String pass = "yourpwd";
Properties props = System.getProperties();
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, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setContent(content_message, "text/html; charset=\"UTF-8\"");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception e) {
System.out.println("Unable to connect");
}
}
在需要發送郵件時調用構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.