Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package com.Morkaz.skMorkaz.main;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.Authenticator;
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. public class EmailSender {
  14. private final String username;
  15. private final String password;
  16. private Properties mailProps;
  17.  
  18. public EmailSender(){
  19. this.username = ConfigLoader.emailUsername;
  20. this.password = ConfigLoader.emailPassword;
  21.  
  22. this.mailProps = new Properties();
  23. this.mailProps.put("mail.smtp.auth", ConfigLoader.emailPasswordRequired);
  24. this.mailProps.put("mail.smtp.starttls.enable", ConfigLoader.emailTLSRequired);
  25. this.mailProps.put("mail.smtp.host", ConfigLoader.emailServerHost);
  26. this.mailProps.put("mail.smtp.port", ConfigLoader.emailPort);
  27. }
  28.  
  29. public boolean send(String subject, String text, String address){
  30. boolean sent = false;
  31. try{
  32. Message message = new MimeMessage(Session.getInstance(this.mailProps,
  33. new Authenticator(){
  34. protected PasswordAuthentication getPasswordAuthentication(){
  35. return new PasswordAuthentication(EmailSender.this.username, EmailSender.this.password);
  36. }
  37. }));
  38. message.setFrom(new InternetAddress(this.username));
  39. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address));
  40. message.setSubject(subject);
  41. message.setText(text);
  42. Transport.send(message);
  43. sent = true;
  44. } catch (MessagingException e){
  45. e.printStackTrace();
  46. sent = false;
  47. }
  48. return sent;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement