Advertisement
Guest User

Untitled

a guest
May 30th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1.  
  2. import org.apache.commons.mail.DefaultAuthenticator;
  3. import org.apache.commons.mail.EmailAttachment;
  4. import org.apache.commons.mail.HtmlEmail;
  5.  
  6. public class Mailer
  7. {
  8.     public void sendMessage()
  9.     {
  10.         String smtpServerName = "smtp.gmail.com";
  11.         int smtpServerPort = 587;
  12.         String smtpServerUser = "yourusername@gmail.com";
  13.         String smtpServerPassword = "yourpassword";
  14.        
  15.         String[] recipients = {"rec1@gmail.com", "rec2@domain.com"};
  16.         String sender="somesender@google.pl";
  17.  
  18.         HtmlEmail email = new HtmlEmail();
  19.         email.setHostName(smtpServerName);
  20.         email.setSmtpPort(smtpServerPort);
  21.         email.setCharset("UTF-8");
  22.        
  23.         if (smtpServerUser != null && smtpServerPassword != null)
  24.         {
  25.             javax.mail.Authenticator auth = new DefaultAuthenticator(smtpServerUser, smtpServerPassword);
  26.             email.setAuthenticator(auth);
  27.           email.setTLS(true);
  28.         }
  29.        
  30.         try
  31.         {
  32.             for (int i = 0; i < recipients.length; i++)
  33.             {
  34.                 email.addTo(recipients[i]);
  35.             }
  36.            
  37.             email.setFrom(sender);
  38.             email.setSubject("Some Message");
  39.  
  40.             StringBuffer msg = new StringBuffer();
  41.             msg.append("<html><body>");
  42.             msg.append("<b> Mail, </b> ");
  43.             msg.append("</body></html>");
  44.             email.setHtmlMsg(msg.toString());
  45.            
  46.             email.send();
  47.         }
  48.         catch (Throwable t)
  49.         {
  50.             throw new RuntimeException(t);
  51.         }  
  52.     }
  53.  
  54.     public static void main(String args[])
  55.     {
  56.         Mailer sm = new Mailer ();
  57.         sm.sendMessage();
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement