相关疑难解决方法(0)

使用Javamail连接到Gmail smtp服务器会忽略指定的端口并尝试使用25

我正在尝试在groovy脚本中使用javamail通过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?))
Run Code Online (Sandbox Code Playgroud)

它似乎尝试使用端口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", …
Run Code Online (Sandbox Code Playgroud)

java groovy gmail smtp jakarta-mail

32
推荐指数
3
解决办法
11万
查看次数

Javamail API - 如何将setFrom更改为您想要的任何内容?

如何将setFrom()方法更改为我想要的任何方法?我可以通过我的gmail accoutn发送电子邮件并更改setFrom文本,但它显示我username的电子邮件.我也试过使用我的雅虎帐户,我收到了身份验证错误.

我想更改发件人地址.代码如下:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "username@gmail.com";
        final String password = "password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            }
        );

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail

7
推荐指数
1
解决办法
8857
查看次数

标签 统计

jakarta-mail ×2

java ×2

gmail ×1

groovy ×1

smtp ×1