[英]553 sorry, Authentication failed or timed out. Please do get messages first to authenticate yourself.(#4.4.3)
我正在尝试使用Java Mail API发送邮件,但是由于发生以下错误,所以我无法发送邮件
553 sorry, Authentication failed or timed out. Please do get messages first to authenticate yourself.(#4.4.3)
我无法理解问题出在哪里,或者rediff是否有任何特殊设置? 我在Google上进行了搜索,但未找到解决方案。请帮助我进行纠正
这是我的代码
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class TestMail {
public static void main(String[] args) throws Exception{
new TestMail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.rediffmailpro.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", 587);
// props.put("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
// uncomment for debugging infos to stdout
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setContent("This is a test", "text/plain");
message.setFrom(new InternetAddress("xxxxxxxxxxx"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("xxxxxxxxxxxxxxxxxx@gmail.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
//Transport.send(message);
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "xxxxxxxxx";
String password = "xxxxxxxx";
return new PasswordAuthentication(username, password);
}
}
}
为了防止用来发送SPAM,大多数SMTP服务器在尝试使用它们中继邮件(即,将邮件发送到其他服务器)时都需要某种身份验证。
连接到服务器时可以提供用户名和密码。 其他服务器-似乎就是这种情况-要求您首先使用POP3或IMAP(必须在其中发送身份验证)连接它们。 他们记得您的IP地址已成功用于身份验证连接,并在此后的一定时间内允许SMTP中继请求。
检查这种情况的一种方法是使用您的邮件程序来获取您的邮件,然后在那之后立即从同一台计算机(或至少通过同一Internet连接)运行您的程序以发送邮件。
如果这可行,则您必须在发送邮件之前在应用程序中建立POP3连接,或者与邮件提供商联系,是否还有其他方法可以验证SMTP连接。
我通过更改解决了这个错误
props.put("mail.smtp.port", 587);
至
props.put("mail.smtp.port", "587");
在JavaMail API的规范中,指定所有属性必须设置为String。这是链接https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary。 HTML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.