java mail tls,通过JavaMail发送到Gmail时的TLS问题

博主在尝试使用JavaMail通过Gmail服务器发送SMTP邮件时遇到530 5.7.0错误,问题在于缺少STARTTLS启用设置。解决方案是添加配置属性"mail.smtp.starttls.enable"并设置为"true"。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值