How to send mail using java




To send mail using java using we use SMTP(Simple Mail Transport Protocol) service provider for sending Gmail to another Gmail account. There are many SMTP providers in the market like Amazon SES, SendGrid etc but these are the paid, so we need free and best SMTP provider which is "smtp.gmail.com".

Requirement 



If someone uses maven to develop the java mail sending application then use the following dependency to add these jar files otherwise click the above click to download the corresponding file.

Dependency
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>

copy paste below code to the IDE and make some change like sender Gmail and password, receiver Gmail ID and messaging text.

program code:


import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class App {

   public static void main(String [] args) {    
        final String username = "sender gmail Id";
       final String password = "gmail password";

       Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
       prop.put("mail.smtp.port", "587");
       prop.put("mail.smtp.auth", "true");
       prop.put("mail.smtp.starttls.enable", "true"); //TLS

       Session session = Session.getInstance(prop,
               new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication(username, password);
                   }
               });

       try {

           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress("sender gmail id"));
           message.setRecipients(
                   Message.RecipientType.TO,
                   InternetAddress.parse("Receiver gmail id")
           );
           message.setSubject("Testing mail sender");
           message.setText("Here we send mail"
                   + "\n\n using java!");

           Transport.send(message);

           System.out.println("Done");

       } catch (MessagingException e) {
           e.printStackTrace();
       }
   }

explanation of code:

  • Two varicable username and password use to store sender Gmail id and thier password.
  • Create properties class and put value in the form of key value pair.
  • prop.put("mail.smtp.host", "smtp.gmail.com")->means use SMTP server as gmail smtp server
  •  prop.put("mail.smtp.port", "587")->
  • SMTP send mail in two formate i,e, SSL and TLS. here port-587 use for TLS encodding scheme.
  • prop.put("mail.smtp.auth", "true")->use for gmail authentication using username id and password
  • prop.put("mail.smtp.starttls.enable", "true") -> which type of encodding use
  • Session.getSession() method use to create a session for sending mail using take username and password and return a session.
  • Then MimeMessage is use to encrypt the message before sending.
  •  Transport.send() method use to send the message.




Post a Comment

Previous Post Next Post

Recent Posts