Monday, 17 November 2014

Send sample Mail ( Java Code )

//Sending Mail

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
 
public class SendEmail 

 public static void main(String [] args){ 
      String to = "abcd@gmail.com";//change accordingly 
      String from = "sample@gmail.com";//change accordingly 
      String host = "localhost";//or IP address 
 
     //Get the session object 
      Properties properties = System.getProperties(); 
      properties.setProperty("mail.smtp.host", host); 
      Session session = Session.getDefaultInstance(properties); 
 
     //compose the message 
      try{ 
         MimeMessage message = new MimeMessage(session); 
         message.setFrom(new InternetAddress(from)); 
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
         message.setSubject("Ping"); 
         message.setText("Hello, Happy Birthday to u  "); 
 
         // Send message 
         Transport.send(message); 
         System.out.println("message sent successfully...."); 
 
      }catch (MessagingException mex) {mex.printStackTrace();} 
   } 
}

1 comment:

  1. Hello p8Programmer,
    can you help me with this.
    https://stackoverflow.com/questions/55754955/forward-mimemessage-object-messages-as-an-regular-forward-format-instead-of-an-a

    ReplyDelete