Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                          

» View all related tutorials
Related Tags: java c api class mail method ip new read using this message ai config show if ie example add javamail

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

Am getting ConnectException in the above class. unable to send mail, Please assist me. I guess its because of Id and password of mail server is missing.

java.net.ConnectException: Connection timed out (errno:238)

Posted by MeetGaurav on Wednesday, 04.8.09 @ 12:56pm | #86637

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.