【发布时间】:2013-12-24 09:12:20
【问题描述】:
这个问题已经从一个相当模糊的问题演变为一个更明确的问题。代码来自example by mkyong。至少根据谷歌it's a bad password,我遵循了他们的登录建议。当然,输入正确密码时我得到相同的结果,下面的密码是一个虚拟密码。无论密码如何,它都是相同的输出。
输出:
BUILD SUCCESSFUL
Total time: 1 second
-- listing properties --
mail.smtp.gmail=smtp.gmail.com
mail.smtps.quitwait=true
mail.smtp.username=hawat.thufir@gmail.com
mail.smtp.starttls.enable=true
mail.smtp.connectiontimeout=2000
mail.smtp.user=hawat.thufir
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.password=password
nntp.host=nntp://localhost/
mail.smtp.socketFactory.port=465
mail.smtp.timeout=2000
mail.imap.port=993
mail.imap.timeout=5000
mail.smtp.port=587
mail.smtp.auth=true
mail.imap.host=imap.gmail.com
mail.nntp.newsrc.file=/home/thufir/.newsrc
mail.imap.connectiontimeout=5000
mail.smtp.host=smtp.gmail.com
========message follows==========
Exception in thread "main" java.lang.RuntimeException: javax.mail.SendFailedException: Send failed;
nested exception is:
javax.mail.SendFailedException: 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 g6sm55120721pat.2 - gsmtp
at net.bounceme.dur.nntp.SendTLS.sendMessage(SendTLS.java:61)
at net.bounceme.dur.nntp.SendTLS.<init>(SendTLS.java:23)
at net.bounceme.dur.nntp.SendTLS.main(SendTLS.java:34)
Caused by: javax.mail.SendFailedException: Send failed;
nested exception is:
javax.mail.SendFailedException: 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 g6sm55120721pat.2 - gsmtp
at javax.mail.Transport.doSend(Transport.java:231)
at javax.mail.Transport.send(Transport.java:75)
at net.bounceme.dur.nntp.SendTLS.sendMessage(SendTLS.java:57)
... 2 more
thufir@dur:~/NetBeansProjects/MailFromNNTP$
代码:
public class SendTLS {
public SendTLS() {
try {
sendMessage();
} catch (NoSuchProviderException ex) {
Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SendTLS.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
new SendTLS();
}
private void sendMessage() throws NoSuchProviderException, MessagingException, IOException {
Properties props = PropertiesReader.getProps();
props.list(System.out);
System.out.println("\n========message follows==========\n");
final String username = props.getProperty("mail.smtp.username");
final String password = props.getProperty("mail.smtp.password");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from-email@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-email@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
【问题讨论】:
-
端口是整数(而不是字符串?只是猜测:P)?还可以尝试用 Session.getInstance(props) 替换 getDefaultInstance。另外,如果这没有帮助。尝试使用道具“mail.smtp.connectiontimeout”和“mail.smtp.timeout”并设置限制以获取堆栈跟踪。通常是防火墙/端口/网络问题
-
第 58 行是连接:
transport.connect(host, port, user, password);,顺便说一下,端口是一个 int。我当然尝试使用正确的密码。 ` -
我知道这是经过几次修改。很奇怪,将属性文件缩减到最低限度实际上将错误引入
SendTLS。 -
我不知道这个,我最后一次尝试,你使用的是两步登录吗?
-
不,只是常规登录。我打算明天再试一次,不过不是 tls。
标签: java email smtp gmail jakarta-mail