必须首先发出 STARTTLS 命令。使用 Java 和 Google Apps 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/386083/
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
Must issue a STARTTLS command first. Sending email with Java and Google Apps
提问by Sergio del Amo
I am trying to use Bill the Lizard's codeto send an email using Google Apps. I am getting this error:
我正在尝试使用Bill the Lizard 的代码通过 Google Apps 发送电子邮件。我收到此错误:
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81)
at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44)
Bill's code contains the next line, which seems related to the error:
比尔的代码包含下一行,这似乎与错误有关:
props.put("mail.smtp.starttls.enable","true");
However, it does not help.
但是,它没有帮助。
These are my import statements:
这些是我的导入语句:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
Does anyone know about this error?
有谁知道这个错误?
回答by Zach Scrivena
I think it's got to do with using SMTPS instead of SMTP for mail transport. Here's a different version, modeled after the JavaMail FAQ on accessing Gmail. Note that I have left out all of the finer level exception handling for clarity.
我认为这与使用 SMTPS 而不是 SMTP 进行邮件传输有关。这是一个不同的版本,以访问 Gmail的JavaMail FAQ为模型。请注意,为清楚起见,我省略了所有更精细级别的异常处理。
private static void send(
final String username,
final String password,
final String recipients,
final String subject,
final String body)
throws Exception
{
final Session session = Session.getInstance(System.getProperties(), null);
final Message msg = new MimeMessage(session);
final String senderEmail = username.contains("@") ? username : (username + "@gmail.com");
msg.setFrom(new InternetAddress(senderEmail));
final Address[] recipientAddresses = InternetAddress.parse(recipients);
msg.setRecipients(Message.RecipientType.TO, recipientAddresses);
msg.setSentDate(new Date());
msg.setSubject(subject);
msg.setText(body);
final Transport transport = session.getTransport("smtps");
transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password);
transport.sendMessage(msg, recipientAddresses);
transport.close();
}
回答by Sergio del Amo
I found the problem. Previously i was using j2ee.jar to import javax.mail.
我发现了问题。以前我使用 j2ee.jar 导入 javax.mail。
I removed j2ee.jar from the classpath and downloaded JavaMail1.4.1 and put into my classpath two jars, smtp.jar and mailapi.jar. I use now smtpsinstead smtp
我从类路径中删除了 j2ee.jar 并下载了JavaMail1.4.1 并将两个 jars、smtp.jar 和 mailapi.jar 放入我的类路径中。我现在使用smtps而不是smtp
Transport transport = session.getTransport("smtps");
Now Bill the Lizard's code works.
现在比尔蜥蜴的代码有效。
回答by Cleber
It is the version of java mail API. I was facing this issue and I just updated the java mail API to 1.4.3 It works fine for me!
它是java邮件API的版本。我遇到了这个问题,我刚刚将 java 邮件 API 更新到 1.4.3 它对我来说很好用!
Thanks!
谢谢!
回答by Rupal K
Set the following tags. It will work.
设置以下标签。它会起作用。
props.put("mail.smtp.user","username");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.EnableSSL.enable","true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
回答by csheets
This was driving me crazy and so just wanted to add what worked for me. I had to update my version of JavaMail (1.4.5) for this to work - not sure what version was being used before.
这让我发疯,所以只想添加对我有用的东西。我必须更新我的 JavaMail (1.4.5) 版本才能使其工作 - 不确定之前使用的是哪个版本。
Once I updated to new version of JavaMail, the following code worked for me (can uncomment the debug lines to get additional debugging info - port was 587
and host was smtp.gmail.com
):
更新到新版本的 JavaMail 后,以下代码对我有用(可以取消对调试行的注释以获取其他调试信息 - port was587
和 host was smtp.gmail.com
):
public void sendMailWithAuth(String host, String user, String password,
String port, List<String> toList, String htmlBody,
String subject) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.user",user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
//props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.EnableSSL.enable","true");
Session session = Session.getInstance(props, null);
//session.setDebug(true);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
// To get the array of addresses
for (String to: toList) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
message.setSubject(subject);
message.setContent(htmlBody, "text/html");
Transport transport = session.getTransport("smtp");
try {
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
} finally {
transport.close();
}
}
回答by Balchandar Reddy
try this:
尝试这个:
turn on: Allow less secure apps for your gmail account
开启:允许您的 Gmail 帐户使用安全性较低的应用