Sending Email through Gmail Server

We can send email by utilizing the SMTP server of gmail. It is great on the off chance that you are don't have any SMTP server and solid. Here we will figure out how to send email through gmail server by SSL [Secured Socket Layer]. SSL is fundamentally utilized for security in the event that you are sending email through gmail server.

For better comprehension of this case, take in the progressions of sending email utilizing JavaMail API first.

For sending the email utilizing JavaMail API, you have to stack the two jug records:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.

Impair the antivirus e.g. avast and so forth in light of the fact that it may interfere with your code to send messages.

Illustration of Sending Email through Gmail Server with SSL

import java.util.Properties;  
import javax.mail.*;  
import javax.mail.internet.*;  
public class SendMailSSL {  
 public static void main(String[] args) {  
 String to="";//change in like manner   
  //Get the session object  
  Properties props = new Properties();  
  props.put("", "");  
  props.put("mail.smtp.socketFactory.port", "465");  
  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("","password");//change accordingly  
  //compose message  
  try {  
   MimeMessage message = new MimeMessage(session);  
   message.setFrom(new InternetAddress(""));//change in like manner 
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
   //send message  
   System.out.println("message sent successfully");  
  } catch (MessagingException e) {throw new RuntimeException(e);}  

As should be obvious in the above illustration, userid and secret word should be validated. As, this system shows, you can send email effortlessly yet change the username and watchword likewise. How about we perceive how to run it by and by basic method:

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac
run by c:\> java SendMailSSL