Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaMail smtp properties (for STARTTLS)

Tags:

JavaMail specifies a bunch of properties that can be set to configure an SMTP connection. To use STARTTLS it is necessary to set the following property

mail.smtp.starttls.enable=true 

Where do I specify the username/password to use the smtp service? Is it enough to specify the:

mail.smtp.user=me mail.smtp.password=secret 

Or do I have to explicitely login using the:

transport.connect(server, userName, password) 

Yes, I already tried to do this and it seems that it is necessary to connect using transport.connect(..). But if yes, what are the mail.smtp.user & pass properties for? Are they not enough to use smtp with starttls?

like image 419
paweloque Avatar asked Apr 08 '11 07:04

paweloque


People also ask

What is mail SMTP Starttls enable?

smtp. starttls. enable, to "true". When set, if the server supports the STARTTLS command, it will be used after making the connection and before sending any login information.

What is SMTP socketFactory port?

mail.smtp.ssl.socketFactory.port. int. Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used. mail.smtp.ssl.protocols.

How can I get SMTP server response using JavaMail?

SMTPTransport t = (SMTPTransport)session. getTransport("smtps"); t. send(message); String response = t. getLastServerResponse(); boolean s = t.

What is mail SMTP AUTH?

SMTP Authentication, often abbreviated SMTP AUTH, is an extension of the Simple Mail Transfer Protocol (SMTP) whereby a client may log in using any authentication mechanism supported by the server. It is mainly used by submission servers, where authentication is mandatory.


1 Answers

Here is my sendEmail method which is using GMail smtp (JavaMail) with STARTTLS

public void sendEmail(String body, String subject, String recipient) throws MessagingException,             UnsupportedEncodingException {         Properties mailProps = new Properties();         mailProps.put("mail.smtp.from", from);         mailProps.put("mail.smtp.host", smtpHost);         mailProps.put("mail.smtp.port", port);         mailProps.put("mail.smtp.auth", true);         mailProps.put("mail.smtp.socketFactory.port", port);         mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");         mailProps.put("mail.smtp.socketFactory.fallback", "false");         mailProps.put("mail.smtp.starttls.enable", "true");          Session mailSession = Session.getDefaultInstance(mailProps, new Authenticator() {              @Override             protected PasswordAuthentication getPasswordAuthentication() {                 return new PasswordAuthentication(login, password);             }          });          MimeMessage message = new MimeMessage(mailSession);         message.setFrom(new InternetAddress(from));         String[] emails = { recipient };         InternetAddress dests[] = new InternetAddress[emails.length];         for (int i = 0; i < emails.length; i++) {             dests[i] = new InternetAddress(emails[i].trim().toLowerCase());         }         message.setRecipients(Message.RecipientType.TO, dests);         message.setSubject(subject, "UTF-8");         Multipart mp = new MimeMultipart();         MimeBodyPart mbp = new MimeBodyPart();         mbp.setContent(body, "text/html;charset=utf-8");         mp.addBodyPart(mbp);         message.setContent(mp);         message.setSentDate(new java.util.Date());          Transport.send(message);     } 
like image 139
Yuri.Bulkin Avatar answered Oct 16 '22 16:10

Yuri.Bulkin