Tuesday, July 17, 2018

Send mail using Java Application

Using this code we can send an email using gmail server

Note: Please follow the given guidelines before using this code
1. Attach the mail.jar with your project using the build-path concept
2. Create a java file as SendMail .java and copy the code inside this
3. Make sure your internet connection is on   


<< Code for SendMail.java file >>

import java.io.File;
import java.util.Properties;

// it is mandatory to import these packages in order to send an email
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

class BabyAuthenticator extends Authenticator
{
@Override
protected PasswordAuthentication 
getPasswordAuthentication() 
{
// create object of PasswordAuthentication class
PasswordAuthentication pa = new 
PasswordAuthentication
("provide email-id of yourself","provide password of yourself");

// return the object of PasswordAuthentication class
return pa;
}
}

public class SendMail 
{
public static void main(String[] args) 
{
try 
{
// create object of properties class to provide the information of
// email server (jo hamari email ko send karega)
Properties p = new Properties();

p.put("mail.smtp.host","smtp.gmail.com");
p.put("mail.smtp.port","587");
p.put("mail.smtp.starttls.enable","true");
p.put("mail.smtp.auth","true");
p.put("mail.smtp.ssl.trust", "smtp.gmail.com");

// create object of BabyAuthenticator 
BabyAuthenticator auth = new BabyAuthenticator();

// create object of session
// the email will be send in the context of session object
Session session = Session.getInstance(p, auth);

// create object of MimeMessage to denote the actual message to be sent
MimeMessage message = new MimeMessage(session);

// provide the email id of sender
message.setFrom
(new InternetAddress("provide your email over here"));

// specify the list persons who will receive this email
// we can create many objects of InternetAddress class to specify
// many receivers
InternetAddress receiver1 = new InternetAddress
("provide the email id of receiver one");
InternetAddress receiver2 = new InternetAddress
("provide the email id of receiver two");

// create array of InternetAddress to store many receivers at a single palce
InternetAddress[] rcvrs = {receiver1,receiver2};

// specify the the type of Recipients
// type means the email will be sent as TO or CC or BCC
message.addRecipients(RecipientType.TO,rcvrs);

// provide the subject of email
message.setSubject("<< provide the subject of mail over here >>");

// create object of MimeBodyPart to denote the body parts of mail 
// note: mail body may contain TEXT as well as ATTACHMENTS
MimeBodyPart part1 = new MimeBodyPart();

// associate some text to the body part 
// the text will be shown at Recipient in italic and the color will be blue 
part1.setContent("<i style='color : blue'>"+ "<< please type the text of email over here >>"+"</i>","text/html");

// create object of MimeBodyPart to denote the body parts of mail
MimeBodyPart part2 = new MimeBodyPart();

// this variable will store the path of file which will be attached to this
// email (as an attachment)
// this file must be available inside your computer (else exception)
String filepath = "<< provide any valid path of file >>";

// create object of File to denote the file path
File file = new File(filepath);

// attach the file with the body part of email
// here we are passing object of File class inside the method
part2.attachFile(file);

// create object of MimeBodyPart to store all the body parts of email
// as a single unit
MimeMultipart allParts = new MimeMultipart();

// associate all the body parts of email inside object of MimeMultipart class 
allParts.addBodyPart(part1);
allParts.addBodyPart(part2);

// store object of MimeMultipart inside the object of MimeMessage
message.setContent(allParts);

// send the object of MimeMessage (bole toh actual message) to the mail server
Transport.send(message);

// show message on console
System.out.println("Mail has been sent to the mail server...");

catch (Exception e) 
{
System.out.println("Some error has occured, and error is "+e);
}

} // end of main method

} // end of class

4 comments: