Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package com.sunrise.infrastructure.server;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.mail.Authenticator;
  5. import javax.mail.PasswordAuthentication;
  6. import javax.mail.*;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9. import java.util.Properties;
  10.  
  11. public class Sender {
  12.  
  13.     private String username;
  14.     private String password;
  15.     private Properties props;
  16.    
  17.     public List<String> EmailNames = new ArrayList<String>();
  18.     public List<String> EmailMessages = new ArrayList<String>();
  19.  
  20.     public Sender(String username, String password) {
  21.         this.username = username;
  22.         this.password = password;
  23.  
  24.         props = new Properties();
  25.         props.put("mail.smtp.host", "smtp.gmail.com");
  26.         props.put("mail.smtp.socketFactory.port", "465");
  27.         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  28.         props.put("mail.smtp.auth", "true");
  29.         props.put("mail.smtp.port", "465");
  30.     }
  31.  
  32.     public void send(String subject, String text, String fromEmail, String toEmail){
  33.         Session session = Session.getDefaultInstance(props, new Authenticator() {
  34.             protected PasswordAuthentication getPasswordAuthentication() {
  35.                 return new PasswordAuthentication(username, password);
  36.             }
  37.         });
  38.  
  39.         try {
  40.             Message message = new MimeMessage(session);
  41.             //от кого
  42.             message.setFrom(new InternetAddress(username));
  43.             //кому
  44.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
  45.             //тема сообщения
  46.             message.setSubject(subject);
  47.             //текст
  48.             message.setText(text);
  49.  
  50.             //отправляем сообщение
  51.             Transport.send(message);
  52.             SR_Server.log("E-Mail send message to: "+toEmail+", title: "+subject+", text: "+text);
  53.         } catch (MessagingException e) {
  54.             throw new RuntimeException(e);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement