通过 JavaMail 发送到 gmail 时的 TLS 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6512430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TLS issue when sending to gmail through JavaMail
提问by Brian
Turns out that JavaMail is a bit more frustrating than I thought it would be. I've looked at several examples online on how to send a simple SMTP email through Gmail's servers (but not through SSL). After trying several different examples of code, I keep concluding to the same example exception when I call transport.connect()
. I keep getting this stack trace:
事实证明,JavaMail 比我想象的更令人沮丧。我在网上查看了几个关于如何通过 Gmail 的服务器(但不是通过 SSL)发送简单的 SMTP 电子邮件的示例。在尝试了几个不同的代码示例之后,当我调用transport.connect()
. 我不断收到此堆栈跟踪:
Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at SendEmail.main(SendEmail.java:47)
Can someone please tell me what I should add or do to fix this?
有人可以告诉我我应该添加什么或做什么来解决这个问题吗?
Here is my code:
这是我的代码:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.host", "smtp.gmail.com");
props.put("mail.user", "[email protected]");
props.put("mail.password", "blah");
props.put("mail.port", "587");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("This is a test");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
采纳答案by Rob Harrop
You need to enable STARTTLS
. Add one more property to your configuration:
您需要启用STARTTLS
. 在您的配置中再添加一项属性:
props.put("mail.smtp.starttls.enable", "true");
回答by pooja
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class CopyOfSendMail {
private static String SMPT_HOSTNAME = "my smtp port no";
private static String USERNAME = "root";
private static String PASSWORD = "root";
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", SMPT_HOSTNAME);
props.put("mail.from","[email protected]");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"[email protected]");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}
}
}
回答by pkm1986
Here Mail Sender which is using MSN-SMTP service
这里使用 MSN-SMTP 服务的邮件发件人
My host is smtp.live.comand port is 587.
我的主机是smtp.live.com,端口是 587。
As given in official doc of Java Mail, here you can get more info about best Java Mail mechanism to send and receive mails.
正如Java Mail 的官方文档中给出的那样,在这里您可以获得有关发送和接收邮件的最佳 Java 邮件机制的更多信息。
Properties of Mail Client are:
邮件客户端的属性是:
Properties mailProps = new Properties();
mailProps.put("mail.smtp.user",mailID);
mailProps.put("mail.smtp.host",host);
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.smtp.port",port);
mailProps.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getInstance(mailProps,null);
Sending mechanism is :
发送机制是:
SMTPTransport t=(SMTPTransport)mailSession.getTransport("smtp");
System.out.println(" Taking protocol! ");
t.connect(host, mailID, password);
System.out.println(" Connection Successfull! ");
t.sendMessage(mimMessage,mimMessage.getAllRecipients());
Note:
Code is running well on local Indian Server.But this is not responding at Azur Congo: Both are Linux server.
注意:
代码在本地印度服务器上运行良好。但这在Azur Congo没有响应:两者都是 Linux 服务器。
Error is:
错误是:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
Even if system property set manually:
即使手动设置系统属性:
java -Dmail.smtp.starttls.enable=true SendAMai
also @Rob Harrop and @Brian 's points are ensured
还确保@Rob Harrop 和@Brian 的分数
if you're on Linux ensure that you have libnss3 and openssl installed