Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. package thirdparty_services;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.internet.InternetAddress;
  5. import javax.mail.internet.MimeMessage;
  6. import java.util.Properties;
  7.  
  8. public class MailService {
  9. final String username;
  10. final String password;
  11. final Properties props;
  12.  
  13. public MailService(String username, String password) {
  14. this.username = username;
  15. this.password = password;
  16.  
  17. props = new Properties();
  18. props.put("mail.smtp.auth", "true");
  19. props.put("mail.smtp.starttls.enable", "true");
  20. props.put("mail.smtp.host", "smtp.gmail.com");
  21. props.put("mail.smtp.port", "587");
  22. }
  23.  
  24. public void sendMail(String to, String subject, String content) {
  25. Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  26. protected PasswordAuthentication getPasswordAuthentication() {
  27. return new PasswordAuthentication(username, password);
  28. }
  29. });
  30.  
  31. try {
  32.  
  33. Message message = new MimeMessage(session);
  34. message.setFrom(new InternetAddress(username));
  35. message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
  36. message.setSubject(subject);
  37. message.setText(content);
  38.  
  39. Transport.send(message);
  40.  
  41. System.out.println("Email sent.");
  42. } catch (MessagingException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement