javajakarta-mailsimple-java-mail

How do I set the localhost name in EHLO (How to set mail.smtp.localhost?)


I use Simple Java Mail library (http://www.simplejavamail.org) in my program and I want to set a localhost name that should be sent in the EHLO when I send email. I tried to do it like this:

Properties props = new Properties();
props.put("mail.smtp.sendpartial", "true");
props.put("mail.smtp.host", "smtp.mail.ru");
props.put("mail.smtp.localhost", "mail.ru");

Mailer mailer = MailerBuilder
        .withSMTPServer(server, port, login, password)
        .withTransportStrategy(TransportStrategy.SMTPS)
        .withSessionTimeout(10 * 1000)
        .clearEmailAddressCriteria()
        .withProperties(props)
        .withDebugLogging(true)
        .buildMailer();

As you can see in the code I should have "mail.ru" in the EHLO now, but it doesn't happen, I keep having my computer's name. I see the following in the log:

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.ru", port 465, isSSL true
220 smtp46.i.mail.ru ESMTP ready (Looking for Mail for your domain? Visit https://biz.mail.ru)
DEBUG SMTP: connected to host "smtp.mail.ru", port: 465

EHLO MyComputersName
250-smtp46.i.mail.ru
250-SIZE 73400320
250-8BITMIME
250-PIPELINING
250 AUTH PLAIN LOGIN XOAUTH2
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2"
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded

What am I doing wrong? How to set mail.smtp.localhost?


Solution

  • If SSL is enabled, try either mail.smtps.localhost or mail.smtps.localaddress. otherwise, mail.smtp.localhost or mail.smtp.localaddress can be used.

    In you case TransportStrategy.SMTPS is used, which means SSL is enabled explicitly, so mail.smtp.localhost for plain connection will NOT work.

    Below is my reference [ paste from SMTPTransport class file ] :

    public synchronized String getLocalHost() {
            if(this.localHostName == null || this.localHostName.length() <= 0) {
                ***this.localHostName = this.session.getProperty("mail." + this.name + ".localhost");***
            }
    
            if(this.localHostName == null || this.localHostName.length() <= 0) {
                ***this.localHostName = this.session.getProperty("mail." + this.name + ".localaddress");***
            }
    
            InetAddress localHost;
            try {
                if(this.localHostName == null || this.localHostName.length() <= 0) {
                    localHost = InetAddress.getLocalHost();
                    this.localHostName = localHost.getCanonicalHostName();
                    if(this.localHostName == null) {
                        this.localHostName = "[" + localHost.getHostAddress() + "]";
                    }
                }
            } catch (UnknownHostException var2) {
                ;
            }
    
            if((this.localHostName == null || this.localHostName.length() <= 0) && this.serverSocket != null && this.serverSocket.isBound()) {
                localHost = this.serverSocket.getLocalAddress();
                this.localHostName = localHost.getCanonicalHostName();
                if(this.localHostName == null) {
                    this.localHostName = "[" + localHost.getHostAddress() + "]";
                }
            }
    
            return this.localHostName;
    }