Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sunrise.infrastructure.server;
- import java.util.ArrayList;
- import java.util.List;
- import javax.mail.Authenticator;
- import javax.mail.PasswordAuthentication;
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
- public class Sender {
- private String username;
- private String password;
- private Properties props;
- public List<String> EmailNames = new ArrayList<String>();
- public List<String> EmailMessages = new ArrayList<String>();
- public Sender(String username, String password) {
- this.username = username;
- this.password = password;
- props = new Properties();
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.port", "465");
- }
- public void send(String subject, String text, String fromEmail, String toEmail){
- Session session = Session.getDefaultInstance(props, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- //от кого
- message.setFrom(new InternetAddress(username));
- //кому
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
- //тема сообщения
- message.setSubject(subject);
- //текст
- message.setText(text);
- //отправляем сообщение
- Transport.send(message);
- SR_Server.log("E-Mail send message to: "+toEmail+", title: "+subject+", text: "+text);
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement