Send Mail using Gmail,Yahoo And Other SMTP With Java

Email Sending Using Java
Email Sending Using Java

I received a tutorial requests from my reader that asked to me how to send mails to web project users. This post explains you how to send mail using SMTP credentials. I had implemented this mail notification system at using SMTP and SASL classes. It’s simple just few lines of configuration changes.

Download Sending Mail Java Code

Step.1 Create New Project

Create New Project
Create New Project

Step.2 Java–> Java Application –>Project Name–>Next–>Finish


Add Jar File For Sending Email
Add Jar File For Sending Email

Step.3 Add Jar File Right Click On Libraries–>Add jar–>Select File Location->Select mail.jar and activation.jar

Jar File Download

Step.4 Insert Code Into Java File

package javaapplication4;

import java.util.Properties;
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;

public class JavaApplication4 {

public static void main(String[] args)

String host=””; //here you write you email host name
final String user=””;//change accordingly user name
final String password=”writepasswordhere”;//change accordingly user pass

String to=””;//change accordingly where you want to send mail

//Get the session object
Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.socketFactory.class”, “”);
props.put(“mail.smtp.port”, “465”);

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);

//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(“Enter Subject”); //change what you want to change subject
message.setText(“here Message What You Want To Send “,”utf-8”, “html”);

//send the message

System.out.println(“message sent successfully…”);

} catch (MessagingException e) {System.out.print(e.getMessage());}



One Comment

  1. sumit said:

    i like this blog very much

    January 3, 2018

Leave a Reply

Your email address will not be published. Required fields are marked *