33

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?))

ポート 587 を使用するように指定したにもかかわらず、ポート 25 を使用しようとしているようです。この問題の原因を知っている人はいますか?ポート 587 で smtp サーバーに接続するために telnet を使用しました。ポート 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)

どんな助けでも大歓迎です。前もって感謝します!

-ブライアン

4

3 に答える 3

34

Java では、次のようなことを行います。

Transport transport = session.getTransport("smtps");
transport.connect (smtp_host, smtp_port, smtp_username, smtp_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();    

「smtpS」プロトコルに注意してください。また、最新の JVM では socketFactory プロパティは不要になりましたが、Gmail では「mail.smtps.auth」と「mail.smtps.starttls.enable」を「true」に設定する必要がある場合があります。「mail.smtps.debug」も役に立ちます。

于 2010-01-02T04:11:53.693 に答える
18

完全な解決策を探している人のために、maximdimの答えに基づいて次のコードでこれを機能させました:

import javax.mail.*
import javax.mail.internet.*

private class SMTPAuthenticator extends Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication('email@gmail.com', 'test1234');
    }
}

def  d_email = "email@gmail.com",
        d_uname = "email",
        d_password = "password",
        d_host = "smtp.gmail.com",
        d_port  = "465", //465,587
        m_to = "testepscript@gmail.com",
        m_subject = "Testing",
        m_text = "Hey, this is the testing email."

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 transport = session.getTransport("smtps");
transport.connect(d_host, 465, d_uname, d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
于 2010-01-02T05:12:39.037 に答える
5

この問題に遭遇した他の人に役立つかもしれません: プロパティでポートを設定する場合:

props.put("mail.smtp.port", smtpPort);

..必ず文字列オブジェクトを使用してください。数値 (つまり、Long) オブジェクトを使用すると、このステートメントは効果がないように見えます。

于 2015-09-08T20:01:34.357 に答える