Home Javamail Sending message using Java Mail
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Sending message using Java Mail

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

Sending message using Java Mail

      

This Example shows you how to send a message using javamail api. A client create new message by using Message subclass. It sets attributes like recipient address and the subject, and inserts the content into 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.

SendMail.java

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

public class SendMail {

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

  String host = "192.168.10.205";
 
String from = "test@localhost";
 
String to = "test@localhost";

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

  // Setup mail server
  
properties.setProperty("mail.smtp.host", 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.
 
message.setSubject("hi..!");

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

  // Send message
 
Transport.send(message);

   System.out.println("Message Send.....");
 
}
}

Output:

Message Send.....

Download code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 15, 2008

Ask Questions?    Discuss: Sending message using Java Mail   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sandeep
May 30, 2011
Sending message using java mail

Sir,when i run this code.I'm getting the following exception Exception in thread "main" javax.mail.MessagingException: could not connect to SMTP host:124.123.89.30, Port No:25; caused by java.net.SocketException: Network is unreachable: connect
Kishan Khatanhar
February 25, 2013
How to install SMTP in local system?

For the Java code above to work, it should have SMTP server setup on host (192.168.10.205 in your case). I want to send a mail using Java with host value as "localhost". Can you please tell me how to setup SMTP server on Windows 7 professional?
kishan Khatanhar
February 25, 2013
How to setup SMTP server on localhost?

I want to send mail using Java with host value as "localhost". I suppose you have SMTP server setup on 192.168.10.205 server. Can you please tell me how to setup SMTP server on localhost?
santosh
January 14, 2012
I got error

I executed the above code but i got error saying that connection is refused? How to solve it.
rahul
March 11, 2012
error message

Could not connect to SMTP host error i am getting . please help
Prashant
July 7, 2012
error

Exception in thread "main" java.lang.NoClassDefFound Error: SendMail
Amit
July 8, 2013
Query

sir i want to make this mailing application in netbeans ide but but i m not able to make, can u please explain me how to make this application in netbeans
DMCA.com