Java Mail SMTPClient Example

This Example shows you how to create a SMTP Client using javamail api. A client create new message by using Message subclass.

Java Mail SMTPClient Example


This Example shows you how to create a SMTP Client using javamail api. A client create new message by using Message subclass. It sets attributes like recipient address and the subject, and inserts the contentinto the Message object, and inserts the content into  the Message object. Finally, it sends the Message by invoking the Transport.send()  method. The Transport class models the transport agent that routes a message to its destination addresses. This class provides methods that send a message to a list of  recipients. Invoking the Transport.send() method with a Message object identifies the appropriate transport based on its destination addresses.

SMTPClient .java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*; 

public class SMTPClient { 

  public static void main(String args[]) throws Exception { 

  String host = "";
String from = "[email protected]";
String to = "[email protected]";

  // Get system properties
Properties properties = System.getProperties(); 

  // Setup mail server
properties.setProperty("", host); 

  // Get the default Session object.
Session session = Session.getDefaultInstance(properties); 

  // Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session); 

  // Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.
message.setFrom(new InternetAddress(from));

  // Add the given addresses to the specified recipient type.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

  // Set the "Subject" header field.

  // Sets the given String as this part's content,
// with a MIME type of "text/plain".
message.setText("Hi ......"); 

  // Send message
  System.out.println("Message Send.....");


Message Send.....

Download code

Share on Google+Share on Google+

Java Mail SMTPClient Example

Posted on: December 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Mail SMTPClient Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 23, 2011
relay access denied error?

hi im triing to send email with the client but after it connect so server is says: "you must autenthicate before sending email". how to do it? or can you please tell me a server where no authentication needed? i guess thats why i cant send email and get relay access denied error when executing rcpt to: "some email adress"... thank you for your help
Debangshu Das
February 5, 2012
SMTP Client

Very useful. Helps the beginners to send mail through SMTP. Here we are getting the session but do we need to close the session (which is abscent).
August 28, 2012
SMTP With authentication

I have created an local email server and could send / receive email though it.looking for an java class which is use to send mail with authentication details.the above example is a straight forward connection without any authentication.Just let me know how do i pass username and pwd in the same example.
October 9, 2012

we test