我试图使用javamail在groovy脚本通过gmail发送一封电子邮件。我已经看了很多地方在网上,一直无法得到它的工作到目前为止。我在运行我的脚本时遇到的错误是:
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "", port 25, isSSL false
Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: , port: 25 (.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@', 'password');
}
}
def d_email = "email@",
d_password = "password",
d_host = "",
d_port = "587", //465,587
m_to = "email@",
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", ".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
如果觉得《java mail 端口号_使用Javamail连接到Gmail smtp服务器将忽略指定的端口并尝试使用25...》对你有帮助,请点赞、收藏,并留下你的观点哦!