SMTP Mail Through Java with Example
SMTP Mail Through Java with Example
Two mail through java with example are here to show you how to use JavaMail API method to send an email via Gmail SMTP server, using both TLS and SSL connection.
below details are required for sending an email.
1 2 3 4 5 |
Outgoing Mail (SMTP) Server requires TLS or SSL: smtp.gmail.com (use authentication) Use Authentication: Yes Port for TLS/STARTTLS: 587 Port for SSL: 465 |
To run this example, you need two libraries – javaee.jar and mail.jar
1. JavaMail –SSL(Through GMAIL)
To send an Mail Through Java and SMTP server using SSL connection please find below Example .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.Thatsjavainfo; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; public class thatsjavainfoMailSSL { public static void main(String[] args) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("thatsjavainfo","password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@gmail.com")); message.setSubject("Testing Subject"); message.setText("Dear one," + "\n\n This is system mail from thatsjavainfo SSL!"); Transport.send(message); System.out.println("SSL Mail Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } } |
2. JavaMail –TLS(Through GMAIL)
To send an Mail Through Java and SMTP server using TLS connection please find below Example .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.thatsjavainfo; import java.util.Properties; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; public class ThatsjavainfoTLSMAIL{ public static void main(String[] args) { final String username = "Thatsjavainfo@gmail.com"; final String password = "password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from-email@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-email@gmail.com")); message.setSubject("Testing Subject"); message.setText("Dear One," + "\n\n I thatsjavainfo TLS system mailer!"); Transport.send(message); System.out.println("TLS Mail Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } } |
Pingback: Your documents written perfectly()