Tuesday, 17 December 2013

Send a Simple E-mail using java

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

public class Main {

     * @param args the command line arguments
// File Name SendEmail.java

   public static void main(String [] args)
      // Recipient's email ID  to be mentioned.
   String to = "Admin@gmail.com";

      // Sender's email ID to be mentioned
      String from = "client@gmail.com";

      String host = "localhost";

      Properties properties = System.getProperties();

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

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

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

         // Set From the  header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To the header field of the header.
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject header!");

         // Now set the actual message or post
         message.setText("This is  message");

         // Send message
         System.out.println("Sent message is successfully....");
      }catch (MessagingException mex) {

$ java Main
Sent message is successfully....

No comments:

Post a Comment