通过JavaMail发送到Gmail时的TLS问题 [英] TLS issue when sending to gmail through JavaMail

查看:708
本文介绍了通过JavaMail发送到Gmail时的TLS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事实证明,JavaMail比我想象的更加令人沮丧。我已经在线看了几个关于如何通过Gmail的服务器(但不是通过SSL)发送简单SMTP邮件的例子。在尝试了几个不同的代码示例后,当我调用 transport.connect()时,我会继续总结出同样的例子。我不断得到这个堆栈跟踪:

 线程main中的异常com.sun.mail.smtp.SMTPSendFailedException:530 5.7。 0必须首先发出STARTTLS命令。 com.un.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)

at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
在SendEmail.main(SendEmail.java:47)

有人可以告诉我我应该添加还是修改这个?



这是我的代码:

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

会话mailSession = Session.getDefaultInstance(道具,null);
运输transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject(这是一个测试);
message.setContent(这是一个测试,text / plain);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(blahblah2@gmail.com));

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


解决方案

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

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


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:

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

解决方案

You need to enable STARTTLS. Add one more property to your configuration:

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

这篇关于通过JavaMail发送到Gmail时的TLS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆