How to Send an Email Using JAVA?
Pre-requisities required to send an Email using Java program
----------------------------------------------
1) Internet Connection is required to connect the Mail Host
2 ) mail.jar file (Need to add this jar file for buildpath)
3) Mail Host
4) From Mail address
5) To mail address
6) Authentication of mail sender details(like username & password).
Program
------------
package com.mail;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendingMail {
public static void main(String[] args) throws AddressException, MessagingException {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com"); // Here GMAIL Mail host has been used
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sendermailid", "senderpassword"); // Here
//validating the sender details like username,password)
}
});
MimeMessage message = new MimeMessage(session); //Passing the session details
message.setFrom(new InternetAddress("sendermailid")); //From address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("receivermailid")); // To
//address
message.setSubject("Tesing the Mail Functionality"); //Mail Subject
message.setText("Welcome to Mail Inbox...Thanking for using this code!!"); //Mail Content
message.saveChanges();
Transport.send(message); //Sending the mail using MIME
System.out.println("Mail has been sent successfullly to your Mail box. Kindly check.....!");
}
}
Console Output
----------------
Mail has been sent successfullly to your Mail box. Kindly check.....!
Note : Kindly Let me know if anybody doesn't understand (or) if facing issues while running this program.
Pre-requisities required to send an Email using Java program
----------------------------------------------
1) Internet Connection is required to connect the Mail Host
2 ) mail.jar file (Need to add this jar file for buildpath)
3) Mail Host
4) From Mail address
5) To mail address
6) Authentication of mail sender details(like username & password).
Program
------------
package com.mail;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendingMail {
public static void main(String[] args) throws AddressException, MessagingException {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.gmail.com"); // Here GMAIL Mail host has been used
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sendermailid", "senderpassword"); // Here
//validating the sender details like username,password)
}
});
MimeMessage message = new MimeMessage(session); //Passing the session details
message.setFrom(new InternetAddress("sendermailid")); //From address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("receivermailid")); // To
//address
message.setSubject("Tesing the Mail Functionality"); //Mail Subject
message.setText("Welcome to Mail Inbox...Thanking for using this code!!"); //Mail Content
message.saveChanges();
Transport.send(message); //Sending the mail using MIME
System.out.println("Mail has been sent successfullly to your Mail box. Kindly check.....!");
}
}
Console Output
----------------
Mail has been sent successfullly to your Mail box. Kindly check.....!
Note : Kindly Let me know if anybody doesn't understand (or) if facing issues while running this program.
The information you posted here is useful to make my career better. Thanks for sharing such a informative post. keep updates...
ReplyDeleteRegards..
Salesforce Training in Chennai
Thanks for sharing this information. Salesforce is a cloud based CRM. Nowadays most of the multinational companies used this CRM for managing their customers. To know more details call 9841746595.
ReplyDeleteRegards..
Salesforce Training
ice post. PHP is one of the server side scripting language mainly used for designing website. So learning PHP Training in Chennai is really useful to make a better career.
ReplyDeleteRegards..
HTML5 Training in Chennai
Hi, I am Victoria from Chennai. I am technology freak. I did Hadoop Training in Chennai at FITA. This is useful for me to make a bright career in IT field.
ReplyDeleteHi friends,This is Johnson from Chennai.Thanks for sharing this informative blog. I did Unix certification course in Chennai at Fita academy. This is really useful for me to make a bright career.
ReplyDeleteRegards..
Unix Course in Chennai
ReplyDeleteAppreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
Cloud Training in Chennai
Software Testing Training in Chennai
Tableau Training in Chennai
QlikView Training in Chennai
Microstrategy Training in Chennai
That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
ReplyDeletefire and safety course in chennai
It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog! I suppose for now I’ll settle for book-marking and adding your RSS feed to my Google account. fire and safety course in chennai
ReplyDelete