當我調用發送方法(設置studentAddress後)在Java中發送電子郵件,我得到這個:嘗試使用Gmail始終會導致用戶名和密碼不被接受
javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 y15sm906936wfd.10
我敢肯定的代碼是正確的,並且100%肯定我輸入的用戶名和密碼的詳細信息是正確的。那麼這是什麼錯誤的Gmail或什麼?
這是我的代碼:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
private String host = "smtp.gmail.com";
private String emailLogin = "[email protected]";
private String pass = "xxx";
private String studentAddress;
private String to;
private Properties props = System.getProperties();
public SendEmail() {
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", emailLogin);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
to = "[email protected]";
}
public void setStudentAddress(String newAddress) {
studentAddress = newAddress;
}
public void send() {
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(emailLogin));
InternetAddress[] studentAddressList = {new InternetAddress(studentAddress)};
message.setReplyTo(studentAddressList);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Test Email");
message.setText("This is a test email!");
Transport transport = session.getTransport("smtps");
transport.connect(host, emailLogin, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException me) {
System.out.println("There has been an email error!");
me.printStackTrace();
}
}
}
任何想法...
要縮小範圍,如果更改爲.getTransport(「smtp」)是否有效? – Serapth 2010-05-14 03:11:42
其實我只是想通了,代碼很好。我使用了一個不同的gmail帳號來與我正在測試的帳號進行比較,並且工作得很完美。因此,出於某種原因,我剛剛在一小時前設置的用於測試此代碼的gmail帳戶無法正常工作?我在其設置中啓用了彈出窗口,所以我不確定爲什麼會出現這種情況。 – Thaeos 2010-05-14 03:24:41