Send e-mail with Java using hotmail account - java

I'm doing a web application for send e-mails with a hotmail account. This is my code:
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.user", "mail#hotmail.com");
props.put("mail.smtp.pwd", pass);
props.put("mail.debug", true);
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("othermail#otherserver.com"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("mail#hotmail.com"));
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/html; charset=UTF-8");
mimeMessage.setSentDate(new Date());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect("smtp.live.com", "mail#hotmail.com", pass);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
} catch (MessagingException e) {
ret = false;}
When I use this code in local everything works but when I deploy my application on my hosting server appears the follow trace:
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL false
220 BLU436-SMTP170.smtp.hotmail.com Microsoft ESMTP MAIL Service, Version: 8.0.9200.16384 ready at Thu, 14 Jan 2016 00:04:21 -0800
DEBUG SMTP: connected to host "smtp.live.com", port: 587
EHLO sbi02.namesservers.net
250-BLU436-SMTP170.smtp.hotmail.com Hello [37.187.179.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-TLS
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "TLS", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO sbi02.namesservers.net
250-BLU436-SMTP170.smtp.hotmail.com Hello [37.187.179.160]
250-TURN
250-SIZE 41943040
250-ETRN
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-8bitmime
250-BINARYMIME
250-CHUNKING
250-VRFY
250-AUTH LOGIN PLAIN XOAUTH2
250 OK
DEBUG SMTP: Found extension "TURN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "41943040"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8bitmime", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2"
DEBUG SMTP: Found extension "OK", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
535 5.0.0 Authentication Failed
I am quite sure that the account credentials are correct but the authentication fails.
Also I have to say that when I try to do this I receive an e-mail of microsoft says there has been a fraudulent attempt to start session but when I execute the code in local I don't receive this e-mail.
I looked at the page account information and the securty & Privacity section, see my recent activity appears challenge security about this try of send e-mail. In this information the device and platform is unknow and the ip is the of my hosting.
There any way to do this?

I've only found a possible solution to my problem. I have enabled two-steps verification in the account. When this option is enabled hotmail gives you the option of creating application passwords. This password is the password that I use in my application instead of the real password of the hotmail account. This solution works for gmail accounts too.

Related

exception javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful

I copied code for sending mails with javax.mail from an older project to my new project. The code worked in the old project for an other customizer.
final Properties props = new Properties();
props.put("mail.smtp.host", "mail.mycompany.net");
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.debug", "true");
final class MailAuthenticator extends javax.mail.Authenticator {
#Override
public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
final Session session = Session.getInstance(props, new MailAuthenticator());
final MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(replyMail, replyId));
msg.setReplyTo(InternetAddress.parse(replyMail, false));
msg.setSubject(subject, "UTF-8");
msg.setContent(body, "text/html; charset=utf-8");
msg.setSentDate(java.sql.Date.valueOf(localDate));
toEmails = toEmails.replace(";", ",");
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmails, false));
Transport.send(msg);
But now I get an AuthenticationFailedException
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful
I use javax.mail version 1.5.0. I tried also version 1.6.2 with the same error.
Here is the log file:
DEBUG: Jakarta Mail version 1.6.4.payara-p1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=mail.mycompany.net, user=ann, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.mycompany.net", port 587, isSSL false
220 SMUCMP04A.europe.mycompany.corp Microsoft ESMTP MAIL Service ready at Wed, 15 Feb 2023 15:23:51 +0100
DEBUG SMTP: connected to host "mail.mycompany.net", port: 587
EHLO SWE-mycompany-WS16-09.localdomain
250-SMUCMP04A.europe.mycompany.corp Hello [10.30.85.214]
250-SIZE 20971520
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO SWE-mycompany-WS16-09.localdomain
250-SMUCMP04A.europe.mycompany.corp Hello [10.30.85.214]
250-SIZE 20971520
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-AUTH GSSAPI NTLM LOGIN
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "GSSAPI NTLM LOGIN"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: protocolConnect login, host=mail.mycompany.net, user=myuser#europe.mycompany.corp, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
Does anyone have an idea to solve the problem?
Thank you Ann
Try:
props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.auth.ntlm.domain","YOUR-LOGIN-WINDOMAIN");
I asked my customizer for the user/password combination three times.
And now I asked a fourth time and I get an other password.
With the new password I can send mails with my implementation.
Sorry for the inconvenience. Thank you all for the help.
Many greetings Ann

JavaMail - Client was not authenticated to send anonymous mail during MAIL FROM

I'm trying to use JavaMail to send emails through office365 valid account. However, I am receiving the same exception '530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM'. Following the simple code I am using for testing:
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, USER_PASS);
}
});
try {
final Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(USER_NAME));
message.setFrom(new InternetAddress(USER_NAME));
message.setSubject(subject);
message.setText(messageContent);
message.setSentDate(new Date());
Transport.send(message);
System.out.println("Send OK");
} catch (final MessagingException ex) {
System.out.println(ex.getMessage());
}
Following the debug output:
DEBUG: JavaMail version 1.3
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.address.map (The system cannot find the file specified)
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: SMTPTransport trying to connect to host "smtp.office365.com", port 587
DEBUG SMTP RCVD: 220 xxxxx.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 6 Jun 2019 07:53:32 +0000
DEBUG: SMTPTransport connected to host "smtp.office365.com", port: 587
DEBUG SMTP SENT: EHLO L3343005201
DEBUG SMTP RCVD: 250-xxxxx.outlook.office365.com Hello [92.242.173.14]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP Found extension "SIZE", arg "157286400"
DEBUG SMTP Found extension "PIPELINING", arg ""
DEBUG SMTP Found extension "DSN", arg ""
DEBUG SMTP Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP Found extension "STARTTLS", arg ""
DEBUG SMTP Found extension "8BITMIME", arg ""
DEBUG SMTP Found extension "BINARYMIME", arg ""
DEBUG SMTP Found extension "CHUNKING", arg ""
DEBUG SMTP Found extension "SMTPUTF8", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<sender#domain>
DEBUG SMTP RCVD: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy.outlook.com]
DEBUG SMTP SENT: QUIT
Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy]
From logs it seems the authentication is not working, and the connection port goes to 25 rather than the 587 as specified in the properties.
I tried adding other properties as well, like
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.user", USER_NAME);
props.put("mail.smtp.password", USER_PASS);
props.put("mail.smtp.from", USER_NAME);
in any combination, but no results.
I've also tried to use the updated version of the JavaMail library, but the exception is the same. Am I missing any configuration on the mail account? Or from code?
Thanks in advance
Edited: changing the port from 587 (int) to "587" (string) solved the port issue in the debug port, but the same Client authentication error occurs
JavaMail 1.3 is 17 years old! Where did you find it? Switch to the current release and then fix all these common JavaMail mistakes.

Log4j2 SMTP-Appender "Must issue a STARTTLS command first"

I am trying to send log entries with severity "error" via email in java.
In my log4j2.xml file I have the following SMTP appender:
<SMTP>
name="Mail"
subject="Error Log"
to="receiver#domain.com"
from="sender#domain.com"
smtpHost="SMTP-Host"
smtpPort="587"
smtpPassword="[password]"
smtpUsername="email#domain.com"
smtpDebug="true"
ignoreExceptions="false"
bufferSize="4"
smtpProtocol="smtp">
</SMTP>
But when I run my programm (which consists of a main logging a string) the following error occurs:
DEBUG: setDebug: JavaMail version 1.4.7
13:13:44.469 [main] ERROR Main - Test Error
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "SMTP-Host", port 587, isSSL false
220 SMTP-Host ESMTP Postfix (Debian/GNU)
DEBUG SMTP: connected to host "SMTP-Host", port: 587
EHLO MAXPC
250-SMTP-Host
250-PIPELINING
250-SIZE 52428800
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<sender#domain.com>
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: got response code 530, with response: 530 5.7.0 Must issue a STARTTLS command first
RSET
530 5.7.0 Must issue a STARTTLS command first
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
at Main.main(Main.java:8)
QUIT
221 2.0.0 Bye
2019-03-03 13:13:49,204 main ERROR SmtpManager SMTP:848b9401aab8a35f62b00f0dbdf21fd4 Caught exception while sending e-mail notification.: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
at Main.main(Main.java:8)
2019-03-03 13:13:49,206 main ERROR An exception occurred processing Appender Mail org.apache.logging.log4j.LoggingException: Error occurred while sending email
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:175)
at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
at Main.main(Main.java:8)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
... 18 more
Exception in thread "main" org.apache.logging.log4j.core.appender.AppenderLoggingException: An exception occurred processing Appender Mail
at org.apache.logging.log4j.core.appender.DefaultErrorHandler.error(DefaultErrorHandler.java:75)
at org.apache.logging.log4j.core.config.AppenderControl.handleAppenderError(AppenderControl.java:165)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:158)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:129)
at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:120)
at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:464)
at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:448)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:431)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:406)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146)
at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2170)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2125)
at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2108)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2007)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1866)
at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:299)
at Main.main(Main.java:8)
Caused by: org.apache.logging.log4j.LoggingException: Error occurred while sending email
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:175)
at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:181)
at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:156)
... 16 more
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:257)
at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:172)
... 18 more
As far as I understand the programm can establish a connection to the server but then Must issue a STARTTLS command first this happens. Can someone explain to me what that means and how to fix it?
When I searched for the error I got a few posts with similar problems but not with log4j2 but with java.mail and they didn't helped me.
I tried using smtpPort="465" and smtpProtocol="smtps" but in both cases the connection got refused.
I have no more new ideas and am grateful for everyone who helps me.
I had the same issue and it looks like log4j2 is not capable of handling STARTTLS. Fortunately log4j uses the system properties when creating the mail session. So my fix is using
smtpProtocol="smtp"
for the SMTP appender and setting the system property
mail.smtp.starttls.enable=true
on the command line to activate STARTTLS for the mail session. You could alternatively use
System.setProperty("mail.smtp.starttls.enable", "true")
in your application code.

Postfix and JavaMail

Tried sending JavaMail with Postfix installed on my Ubuntu pc.
It seems as though the mail is sent but it never arrives.
I just installed Postfix and have no idea how to configure it as an smtp server.
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
220 UsProg.com ESMTP Postfix (Ubuntu)
DEBUG SMTP: connected to host "localhost", port: 25
EHLO eitan-pc
250-UsProg.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10240000"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<avicohen974631#gmail.com>
250 2.1.0 Ok
RCPT TO:<eitan.lp#gmail.com>
250 2.1.5 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: *******#gmail.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Sun, 17 May 2015 23:23:26 +0300 (IDT)
From: avicohen974631#gmail.com
To: *********#gmail.com
Message-ID: <2064112929.0.1431894206597.JavaMail.eitan#eitan-pc>
Subject: =?UTF-8?B?16DXmdeh15nXldefINei150g16TXqNeV16fXodeZ?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64
15TXldeT16LXlCDXlteQ16og16DXqdec15fXlCDXk9eo15og16nXqNeqINeQ16DXldeg15nXnteZ
.
250 2.0.0 Ok: queued as 99611E18F1
QUIT
221 2.0.0 Bye
Mail sent to '********#gmail.com' from avicohen974631#gmail.com
Task complete. 1 mails in 0 seconds

Is javax.mail using two smtp sessions for each mail?

So we're debugging some network issues were sending mail takes 5 seconds from telnet to the smptp server. The problem with sending mail with javax.mail is that it takes 10 seconds... We've turned on logging in javax.mail and it looks like it's opening two smtp sessions and the first one doesn't send any data. Could this be because of the 5 second delay? some sort of timeout? or is this how javax.mail works?
Our mail code:
Properties props = new Properties();
props.put("mail.smtp.host", _smtpHost);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message newMessage = new MimeMessage(session);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(sentDate);
newMessage.setContent(content, contenttype);
Transport transport = session.getTransport(SMTP_MAIL);
transport.connect(_smtpHost, _user, _password);
Transport.send(newMessage);
The debug output:
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 false
DEBUG SMTP: trying to connect to host "mail.xxx.xxx", port 25, isSSL false
220 mail.xxx.xxx ESMTP Service (Lotus Domino XXX) ready at Mon, 7 Nov 2011 09:15:00 +0100
DEBUG SMTP: connected to host "mail.xxx.xxx", port: 25
EHLO MYCOMPUTER
250-mail.xxx.xxx Hello MYCOMPUTER ([10.xxx.xxx.xxx]), pleased to meet you
250-HELP
250-VRFY
250-EXPN
250-DSN
250-SIZE 256000000
250-8BITMIME
250 PIPELINING
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "EXPN", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "256000000"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "mail.xxx.xxx", port 25, isSSL false
220 mail.xxx.xxx ESMTP Service (Lotus Domino XXX) ready at Mon, 7 Nov 2011 09:15:05 +0100
DEBUG SMTP: connected to host "mail.xxx.xxx", port: 25
EHLO MYCOMPUTER
250-mail.xxx.xxx Hello MYCOMPUTER ([10.xxx.xxx.xxx]), pleased to meet you
250-HELP
250-VRFY
250-EXPN
250-DSN
250-SIZE 256000000
250-8BITMIME
250 PIPELINING
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "EXPN", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "SIZE", arg "256000000"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<tommy#xxx.xxx>
250 tommy#xxx.xxx... Sender OK
RCPT TO:<tommy#xxx.xxx>
250 tommy#xxx.xxx... Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP: tommy#xxx.xxx
DATA
354 Enter message, end with "." on a line by itself
Date: Mon, 7 Nov 2011 09:14:55 +0100 (CET)
From: tommy#xxx.xxx
To: tommy#xxx.xxx
Message-ID: <145229992.01320653699737.JavaMail.XXX#MYCOMPUTER>
Subject: Test av html-mail
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
sending myself an email...
250 Message accepted for delivery
QUIT
221 xxx.xxx.com SMTP Service closing transmission channel
javax.mail doesn't use any Telnet sessions. It uses POP3 or SMTP sessions.

Categories