Reputation: 101
I was going through this Link and tried to execute the code.
Summary of the same is as follows:
private static final String SSL_FACTORY = "SendMailDemoSSLSocketFactory"; //SendMailDemoSSLSocketFactory extends SSLSocketFactory
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.submitter", emailUsername);
props.put("mail.smtp.user", emailUsername);
props.put("mail.smtp.host", smtpHostName);
props.put("mail.debug", debug);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.ssl.socketFactory.port", smtpPort);
props.put("mail.smtp.ssl.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "true");
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.setProperty("mail.smtps.ssl.enable", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.ssl.trust", smtpHostName);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailUsername,
emailPassword);
}
});
session.setDebug(Boolean.parseBoolean(debug));
Message msg = new MimeMessage(session);
//set message
SMTPSSLTransport transport = (SMTPSSLTransport) session
.getTransport("smtps");
transport.connect(smtpHostName, new Integer(smtpPort).intValue(),
emailUsername, emailPassword);
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
With the same I was able to estb successful connection but authentication was unsuccessful. I am not able to find where is the code going wrong.
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.7.0_25\jre\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.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,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: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,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: successfully loaded file: C:\Program Files\Java\jdk1.7.0_25\jre\lib\javamail.address.map
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 smtp.gmail.com ESMTP xv9sm19052844pbc.2 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO test
250-smtp.gmail.com at your service, [182.64.100.170]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: use8bit false
MAIL FROM: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 xv9sm19052844pbc.2 - gsmtp com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 xv9sm19052844pbc.2 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at edu.bhavesh.mail.smtp.ssl.SendMailDemo.sendSSLMessage(SendMailDemo.java:135)
at edu.bhavesh.mail.smtp.ssl.SendMailDemo.main(SendMailDemo.java:41)
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/answer/14257 xv9sm19052844pbc.2 - gsmtp QUIT
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at edu.bhavesh.mail.smtp.ssl.SendMailDemo.sendSSLMessage(SendMailDemo.java:135)
at edu.bhavesh.mail.smtp.ssl.SendMailDemo.main(SendMailDemo.java:41)
221 2.0.0 closing connection xv9sm19052844pbc.2 - gsmtp
Upvotes: 4
Views: 1200
Reputation: 11513
It is commendable that you are trying to accomplish this on a low level API, but really you shouldn't attempt this anymore in this day and age (not even when this question was originally asked). Not only are the Jakarta/Java Mail properties finicky, factories confusing, authorization verbose and documentation vague, you would likely have difficulty forming a correct MimeMessage for a more complex email that works properly and that too in all possible clients.
Have a look at the following alternative using open source Simple Java Mail. I have converted your example for this:
Email email = EmailBuilder.startingBlank()
.from(..)
.to(..)
.withSubject(..)
(..)
.buildEmail();
MailerBuilder
.withSMTPServer(smtpHostName, smtpPort, emailUsername, emailPassword)
.withTransportStrategy(TransportStrategy.SMTP_TLS) // or .SMTPS
.withDebugLogging(Boolean.parseBoolean(debug))
.buildMailer()
.sendMail(email);
This uses all the right properties and produces the correct MimeMessage structure matching your needs.
There are other alternatives such as Apache Commons Email which are an improvement over the low level API you are using now, but are not as robust, correct or performant as Simple Java Mail has become over the many years of development (still active).
Upvotes: 0
Reputation: 794
This is only properties needed for gmail smtp, i am working with it: Using jars in my classpath:
commons-io-2.4.jar
String sendingHost = "smtp.gmail.com";
int sendingPort = 465;
Properties props = new Properties();
props.put("mail.smtp.host", sendingHost);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", sendingPort + "");
/**ADD THIS**/
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.SSL", "true");
props.put("mail.smtp.ehlo", "true");
/** **/
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email.getFromAccount().getEmail(), email.getFromAccount().getPassword());
}
});
Upvotes: 0