通过JavaMail发送到gmail时出现TLS问题

时间:2011-06-28 20:02:58

标签: java email smtp gmail javamail

事实证明,JavaMail比我想象的更令人沮丧。我在网上看了几个关于如何通过Gmail服务器发送简单SMTP电子邮件的例子(但不是通过SSL)。在尝试了几个不同的代码示例之后,当我调用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)

有人可以告诉我应该添加或做些什么来解决这个问题吗?

这是我的代码:

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.host", "smtp.gmail.com");
    props.put("mail.user", "blahblah@gmail.com");
    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("blahblah2@gmail.com"));

    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();

3 个答案:

答案 0 :(得分:13)

您需要启用STARTTLS。在配置中添加一个属性:

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

答案 1 :(得分:5)

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","aaa@gmail.com");
    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,
                          "bbb@gmail.com");
        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);
     }
    }
}

答案 2 :(得分:1)

此处使用MSN-SMTP服务的邮件发件人

我的主机是 smtp.live.com ,端口是587。

official doc of Java Mail中所述,您可以在此处获得有关发送和接收邮件的最佳Java Mail机制的更多信息。

Mail Client的属性是:

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

发送机制是:

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

注意:
代码在本地印度服务器上运行良好。 但这并没有在 Azur Congo 做出回应:两者都是Linux服务器。

错误是:

 com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first

即使手动设置系统属性:

 java -Dmail.smtp.starttls.enable=true SendAMai

@Rob Harrop和@Brian的积分得到了保证

if you're on Linux ensure that you have libnss3 and openssl installed