java mail 端口号_使用Javamail连接到Gmail smtp服务器将忽略指定的端口并尝试使用25...

我试图使用javamail在groovy脚本通过gmail发送一封电子邮件。我已经看了很多地方在网上,一直无法得到它的工作到目前为止。我在运行我的脚本时遇到的错误是:

DEBUG SMTP: useEhlo true, useAuth false

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false

Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?))

似乎是尝试使用端口25,即使我已经指定它应该使用端口587.有谁知道可能是什么导致这个问题,我使用telnet连接到端口587的smtp服务器,和thunderbird使用端口587与STARTTLS安全,并能够成功地使用smtp服务器发送邮件。这告诉我,它不是一个阻塞的端口或连接问题。这里是我用来尝试和发送电子邮件的代码:

import javax.mail.*

import javax.mail.internet.*

private class SMTPAuthenticator extends Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication('email@gmail.com', 'password');

}

}

def d_email = "email@gmail.com",

d_password = "password",

d_host = "smtp.gmail.com",

d_port = "587", //465,587

m_to = "email@gmail.com",

m_subject = "Testing",

m_text = "This is a test."

def props = new Properties()

props.put("mail.smtp.user", d_email)

props.put("mail.smtp.host", d_host)

props.put("mail.smtp.port", d_port)

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

props.put("mail.smtp.debug", "true");

props.put("mail.smtp.auth", "true")

props.put("mail.smtp.socketFactory.port", d_port)

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")

props.put("mail.smtp.socketFactory.fallback", "false")

def auth = new SMTPAuthenticator()

def session = Session.getInstance(props, auth)

session.setDebug(true);

def msg = new MimeMessage(session)

msg.setText(m_text)

msg.setSubject(m_subject)

msg.setFrom(new InternetAddress(d_email))

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to))

Transport.send(msg)

任何帮助将不胜感激。提前致谢!

-Bryan

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值