Friday 11, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 8 September 2013

Sending Simple Email Using Java Mail API


In this tutorials we are going to see how to send the simple email using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for sending email, for that add latest version of mail.jar into your build path, I used javamail-1.4.7 version.Follow the simple steps for create simple email application using java
Create Java project called EmailSender in your eclipse IDE and then create package called com.javatutorialscorner.email under EmailSender project then create Java class called EmailSender.java under com.javatutorialscorner.email package.
EmailSender.java
package com.javatutorialscorner.email;

import java.util.Properties;

import javax.mail.Authenticator;
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 EmailSender {

/**
* @param args
*/
public static void main(String[] args) {
String TO = "toemail@gmail.com";
String host = "smtp.gmail.com";
String port = "465";
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties mailConfig = new Properties();
mailConfig.put("mail.smtp.host", host);
mailConfig.put("mail.smtp.socketFactory.port", port);
mailConfig.put("mail.smtp.socketFactory.class", SSL_FACTORY);
mailConfig.put("mail.smtp.auth", "true");
mailConfig.put("mail.smtp.port", port);

Session session = Session.getDefaultInstance(mailConfig,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("fromeail@gmail.com","password");
}});

try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
message.setSubject("Java Tutorials Corner - Email Sender");
message.setText("Simple Email sent by Java Tutorials Corner");
Transport.send(message);
System.out.println("Email sent successfully");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

}

}

In above program smtp.gmail.com is gmail SMTP server, you can add your SMTP server if you own. The javax.net.ssl.SSLSocketFactory used for SSL support, mail.smtp.auth is to set authentication required by mail server for sending email.The PasswordAuthentication class is used to authenticate user name and password. The Session object used to store information like host,port,user name,password. The getDefaultInstance() method used to get session object.MimeMessage used to set subject ,message and add recipient and The Transport class send() method is used to send MIME message

Now you can run the application and receive the mail.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Sending HTML Email using Java Mail APIIn this tutorials we are going to see how to send the HTML email message using Java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package use… Read More
  • Sending Simple Email Using Java Mail APIIn this tutorials we are going to see how to send the simple email using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for … Read More
  • Email Receiver using Java Mail APIIn this tutorials we are going to see how to receive the email using java program.I used gmail SMTP server for receive email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for rece… Read More
  • Java Mail API - Introduction In this tutorials we are going to see introduction about Java Mail API.The Java Mail API used to send email programmatically.Java Mail API is platform and protocol independent API for sending and receiving email programmat… Read More
  • Email Receiver with AttachmentIn this tutorials we are going to see how to receive the email with attachment using java program.I used gmail SMTP server for receive email, you can use any SMTP server(provided by your host provider).The javax.mail.*; packa… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Sending Simple Email Using Java Mail API Rating: 5 Reviewed By: eHowToNow