Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.41 KB | None | 0 0
  1. package cl.group2.util;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.Authenticator;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import java.util.Properties;
  8.  
  9. public class Mail {
  10.     private String mail;
  11.     private Session session;
  12.  
  13.     Mail(String mail, String password) {
  14.         this.mail = mail;
  15.  
  16.         Properties props = new Properties();
  17.         props.put("mail.smtp.auth", "true");
  18.         props.put("mail.smtp.starttls.enable", "true");
  19.         props.put("mail.smtp.host", "smtp.gmail.com");
  20.         props.put("mail.smtp.port", "587");
  21.  
  22.         session = Session.getDefaultInstance(props, new Authenticator() {
  23.             @Override
  24.             protected PasswordAuthentication getPasswordAuthentication() {
  25.                 return new PasswordAuthentication(mail, password);
  26.             }
  27.         });
  28.     }
  29.  
  30.     public boolean sendMail(String to, String subject, String body) {
  31.         try {
  32.             MimeMessage message = new MimeMessage(session);
  33.  
  34.             message.setFrom(new InternetAddress(mail));
  35.             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  36.             message.setSubject(subject);
  37.             message.setText(body);
  38.  
  39.             Transport.send(message);
  40.  
  41.             return true;
  42.         } catch (MessagingException e) {
  43.             e.printStackTrace();
  44.  
  45.             return false;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement