Translate

Sending email in Java Code

  1. import java.util.*;  
  2. import javax.mail.*;  
  3. import javax.mail.internet.*;  
  4. import javax.activation.*;  
  5.   
  6. public class SendEmail  
  7. {  
  8.  public static void main(String [] args){  
  9.       String to = "sonoojaiswal1988@gmail.com";//change accordingly  
  10.       String from = "sonoojaiswal1987@gmail.com";change accordingly  
  11.       String host = "localhost";//or IP address  
  12.   
  13.      //Get the session object  
  14.       Properties properties = System.getProperties();  
  15.       properties.setProperty("mail.smtp.host", host);  
  16.       Session session = Session.getDefaultInstance(properties);  
  17.   
  18.      //compose the message  
  19.       try{  
  20.          MimeMessage message = new MimeMessage(session);  
  21.          message.setFrom(new InternetAddress(from));  
  22.          message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
  23.          message.setSubject("Ping");  
  24.          message.setText("Hello, this is example of sending email  ");  
  25.   
  26.          // Send message  
  27.          Transport.send(message);  
  28.          System.out.println("message sent successfully....");  
  29.   
  30.       }catch (MessagingException mex) {mex.printStackTrace();}  
  31.    }  
  32. }  
Newest
Previous
Next Post »

Contact Form

Name

Email *

Message *