I try to send email using javamail to Zimbra server but always receive error Client host rejected: Access denied
This is my java code:
public class TestMail {
public static void main(String[] args) throws MessagingException {
// provide recipient's email ID
String to = "other_email@domain.com";
// provide sender's email ID
String from = "admin@domain.com";
// provide username
final String username = "admin@domain.com";
// provide password
final String password = "password-here";
// provide host address
String host = "mail.domain.com";
// configure SMTP server details
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
// setting starttls
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// create the Session object
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// create a MimeMessage object
Message message = new MimeMessage(session);
// set From email field
message.setFrom(new InternetAddress(from));
// set To email field
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// set email subject field
message.setSubject("Here comes Jakarta Mail!");
// set the content of the email message
message.setText("Just discovered that Jakarta Mail is fun and easy to use");
// send the email message
// Transport.send(message);
Transport tr = session.getTransport("smtp");
tr.connect(host, username, password);
message.saveChanges();
tr.sendMessage(message, message.getAllRecipients());
tr.close();
System.out.println("Email Message Sent Successfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
I think error because it seems java code don't send AUTH PLAIN but I don't know why, maybe I forget configure properties?
But with nodejs code then email sent success with AUTH PLAIN
DEBUG LOG:
DEBUG: Jakarta Mail version 1.6.5
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: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.domain.com", port 587, isSSL false
220 mail.domain.com ESMTP Postfix
DEBUG SMTP: connected to host "mail.domain.com", port: 587
EHLO Admin
502 5.5.2 Error: command not recognized
HELO Admin
250-mail.domain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: use8bit false
MAIL FROM:<admin@mitc.vn>
250 mail.domain.com
RCPT TO:<other_email@domain.com>
250 2.1.0 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: other_email@domain.com
DATA
554 5.7.1 <unknown[xxx.xxx.xx.xx]>: Client host rejected: Access denied
DEBUG SMTP: got response code 554, with response: 554 5.7.1 <unknown[xxx.xxx.xx.xx]>: Client host rejected: Access denied
RSET
554 5.5.1 Error: no valid recipients
DEBUG SMTP: MessagingException while sending, THROW:
com.sun.mail.smtp.SMTPSendFailedException: 554 5.7.1 <unknown[xxx.xxx.xx.xx]>: Client host rejected: Access denied
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
at com.sun.mail.smtp.SMTPTransport.data(SMTPTransport.java:2080)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1300)
at test2.TestMail.main(TestMail.java:74)