×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Ricardo T
Added: Nov 30, 2016 1:01 PM
Views: 9
Tags: no tags
email
  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3. import java.util.*;
  4.  
  5. public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
  6. {
  7.     boolean debug = false;
  8.  
  9.      //Set the host smtp address
  10.      Properties props = new Properties();
  11.      props.put("mail.smtp.host", "smtp.example.com");
  12.  
  13.     // create some properties and get the default Session
  14.     Session session = Session.getDefaultInstance(props, null);
  15.     session.setDebug(debug);
  16.  
  17.     // create a message
  18.     Message msg = new MimeMessage(session);
  19.  
  20.     // set the from and to address
  21.     InternetAddress addressFrom = new InternetAddress(from);
  22.     msg.setFrom(addressFrom);
  23.  
  24.     InternetAddress[] addressTo = new InternetAddress[recipients.length];
  25.     for (int i = 0; i < recipients.length; i++)
  26.     {
  27.         addressTo[i] = new InternetAddress(recipients[i]);
  28.     }
  29.     msg.setRecipients(Message.RecipientType.TO, addressTo);
  30.    
  31.  
  32.     // Optional : You can also set your custom headers in the Email if you Want
  33.     msg.addHeader("MyHeaderName", "myHeaderValue");
  34.  
  35.     // Setting the Subject and Content Type
  36.     msg.setSubject(subject);
  37.     msg.setContent(message, "text/plain");
  38.     Transport.send(msg);
  39. }