首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >必须先发出STARTTLS命令。使用Java和Google Apps发送电子邮件

必须先发出STARTTLS命令。使用Java和Google Apps发送电子邮件
EN

Stack Overflow用户
提问于 2008-12-22 11:57:57
回答 5查看 95.5K关注 0票数 20

我正在尝试使用Bill the Lizard's code通过谷歌应用程序发送电子邮件。我得到了这个错误:

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的代码包含下一行,这似乎与错误有关:

   props.put("mail.smtp.starttls.enable","true");

然而,这并没有什么帮助。

下面是我的import语句:

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;

有人知道这个错误吗?

EN

回答 5

Stack Overflow用户

发布于 2012-04-21 20:12:31

设置以下标签。看起来不错。

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"); 
票数 17
EN

Stack Overflow用户

发布于 2010-02-02 21:11:53

它是java mail API的版本。我正面临着这个问题,我刚刚将java mail API更新到了1.4.3,它对我来说工作得很好!

谢谢!

票数 8
EN

Stack Overflow用户

发布于 2008-12-22 12:36:51

我认为这与使用SMTPS而不是SMTP进行邮件传输有关。这是一个不同的版本,仿照JavaMail FAQ on accessing Gmail。请注意,为了清楚起见,我省略了所有更精细的异常处理。

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();
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/386083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档