Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.mail.DefaultAuthenticator;
- import org.apache.commons.mail.EmailAttachment;
- import org.apache.commons.mail.HtmlEmail;
- public class Mailer
- {
- public void sendMessage()
- {
- String smtpServerName = "smtp.gmail.com";
- int smtpServerPort = 587;
- String smtpServerUser = "yourusername@gmail.com";
- String smtpServerPassword = "yourpassword";
- String[] recipients = {"rec1@gmail.com", "rec2@domain.com"};
- String sender="somesender@google.pl";
- HtmlEmail email = new HtmlEmail();
- email.setHostName(smtpServerName);
- email.setSmtpPort(smtpServerPort);
- email.setCharset("UTF-8");
- if (smtpServerUser != null && smtpServerPassword != null)
- {
- javax.mail.Authenticator auth = new DefaultAuthenticator(smtpServerUser, smtpServerPassword);
- email.setAuthenticator(auth);
- email.setTLS(true);
- }
- try
- {
- for (int i = 0; i < recipients.length; i++)
- {
- email.addTo(recipients[i]);
- }
- email.setFrom(sender);
- email.setSubject("Some Message");
- StringBuffer msg = new StringBuffer();
- msg.append("<html><body>");
- msg.append("<b> Mail, </b> ");
- msg.append("</body></html>");
- email.setHtmlMsg(msg.toString());
- email.send();
- }
- catch (Throwable t)
- {
- throw new RuntimeException(t);
- }
- }
- public static void main(String args[])
- {
- Mailer sm = new Mailer ();
- sm.sendMessage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement