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

时间:2008-12-22 11:57:57

标签: java email google-apps

我正在尝试使用Bill the Lizard's code使用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的代码包含下一行,它似乎与错误有关:

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

然而,它没有帮助。

这些是我的导入声明:

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;

有人知道这个错误吗?

6 个答案:

答案 0 :(得分:18)

我发现了问题。以前我使用j2ee.jar导入javax.mail。

我从类路径中删除了j2ee.jar并下载了JavaMail 1.4.1并将两个jar,smtp.jar和mailapi.jar放入我的类路径中。我现在使用 smtps 而不是 smtp

Transport transport = session.getTransport("smtps");            

现在比尔蜥蜴的代码有效。

答案 1 :(得分:15)

设置以下标记。它会起作用。

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"); 

答案 2 :(得分:8)

这是java mail API的版本。 我遇到了这个问题,我刚刚将java邮件API更新为1.4.3 它适用于我!

谢谢!

答案 3 :(得分:4)

我认为这与使用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 :(得分:1)

这让我发疯,所以只想添加对我有用的东西。我不得不更新我的JavaMail版本(1.4.5)才能使用 - 不知道之前使用的是什么版本。

一旦我更新到JavaMail的新版本,以下代码对我有用(可以取消注释调试行以获取其他调试信息 - 端口为587,主机为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();
    }
}

答案 5 :(得分:1)

试试这个:

开启:为您的Gmail帐户设置安全性较低的应用

https://myaccount.google.com/lesssecureapps