【问题标题】:Javamail fails but with no exceptions or debug errorJavamail 失败但没有异常或调试错误
【发布时间】:2013-10-24 21:56:42
【问题描述】:

使用下面的代码,我无法发送电子邮件。没有抛出异常,但没有发送电子邮件。该代码是从运行在 Apache Tomcat 中的 servlet 中调用的。

代码主要来自http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ 的在线示例。 调试输出位于代码下方。

  public static boolean sendEmail(String to, final String from, String subject, String emailMessage) {
    final String username = from;
    final String password = MyUtilities.getSystemPWD(from);
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", MyConfiguration.smtpServer);
    props.put("mail.smtp.port", MyConfiguration.smtpPort);
    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 InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setText(emailMessage);
        Transport.send(message);

        System.out.println("Done");
    } catch (MessagingException me) {
        MyLogger.log("MyUtilities.sendEmail: Messaging error", me);
        Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", me);
        System.out.println("MyUtilities.sendEmail: Messaging error");
        return false;
    } catch (Exception ex) {
        MyLogger.log("MyUtilities.sendEmail: Messaging error", ex);
        Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", ex);
        return false;
    }
    return true;
}

调试输出

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.mysite.com", port 587, isSSL false
220 smtp.mysite.net InterWorx-CP SMTP Server ESMTP
DEBUG SMTP: connected to host "mail.mysite.com", port: 587

EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-STARTTLS
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
220 ready for tls
EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<notifications@myaddress.com>
250 ok
RCPT TO:<xxxxxxxxxx@hotmail.com>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   xxxxxxx@hotmail.com
DATA
354 go ahead
From: notifications@myaddress.com
To: xxxxxx@hotmail.com
Message-ID: <256511087.2.1381941482464.JavaMail.xxxxx@xxxx-iMac.local>
Subject: 
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit    
<html></html>
.
250 ok 1381941483 qp 29906
QUIT
221 smtp.mysite.net InterWorx-CP SMTP Server
Done 

【问题讨论】:

  • 如果版主可以看到这一点,当我尝试将 SSL 标记更改为 tls 时,它会变回 ssl....
  • 刚刚尝试将标签从 ssl 更改为 tls。不变

标签: java email ssl smtp jakarta-mail


【解决方案1】:

从调试输出中可以清楚地看出,您的邮件服务器正在接受来自您的应用程序的消息。因此,您的程序或配置没有问题。如果邮件服务器从不发送邮件,您需要查看邮件服务器日志文件。在此之前,请查看收件人的垃圾邮件文件夹。

【讨论】:

    【解决方案2】:

    我面临(几乎)同样的问题,这是由于一些额外的/不需要的罐子,请参考 Send mail not working in Spring/Maven/Java 6 environment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2013-05-23
      • 2020-10-02
      • 2022-08-22
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多