系统相关
首页 > 系统相关> > 无法在Linux中使用JavaMail API发送电子邮件

无法在Linux中使用JavaMail API发送电子邮件

作者:互联网

这是我的代码,它可以在Windows计算机上运行,​​但不能在Linux上运行…
它没有抛出任何异常..在Linux上

public void alert(String recipient, String subject , String error){

    final String username = customize.getString("alertSenderEmail");
    final String password = customize.getString("alertSenderPassword");

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "false");
    props.put("mail.smtp.host", "smtp1.qualitykiosk.com");
    props.put("mail.smtp.port", "25");

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

    session.setDebug(true);

    String[] receivers = recipient.split(","); 
    for (String receiver : receivers) {

            try {


                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(username));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(receiver));
                message.setSubject(subject);
                message.setText(error);

                Transport.send(message);

                System.out.println("Done");

            } catch (MessagingException e) {
                //throw new RuntimeException(e);
            }
    }

}

错误-:

DEBUG: setDebug: JavaMail version 1.4ea
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 "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25

EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname
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 "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25

EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname
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 "smtp1.qualitykiosk.com", port 25, isSSL false
220 ESMTP ESMTP
DEBUG SMTP: connected to host "smtp1.qualitykiosk.com", port: 25

EHLO
501 Syntax: EHLO hostname
HELO
501 Syntax: HELO hostname

解决方法:

如果Java / JavaMail无法检测到您当前的主机名,则会发生这种情况.使用属性mail.smtp.localhost显式指定EHLO / HELO的主机名.

https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html开始:

mail.smtp.localhost String
Local host name used in the SMTP HELO or EHLO command. Defaults to InetAddress.getLocalHost().getHostName(). Should not normally need to be set if your JDK and your name service are configured properly.

作为更长期的解决方案,您可能想找出为什么InetAddress.getLocalHost().getHostName()(显然)没有返回主机名.

标签:javamail,linux,java
来源: https://codeday.me/bug/20191120/2040653.html