Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mcviral.dev.plugins.servermanager.util;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- //import javax.mail.internet.MimeMessage.RecipientType;
- public class Email {
- private String smtpServer;
- private String username;
- private String password;
- private String to;
- private String subject;
- private String body;
- public Email withSMTPServer(String smtpServer)
- {
- this.smtpServer = smtpServer;
- return this;
- }
- public Email withUsername(String username)
- {
- this.username = username;
- return this;
- }
- public Email withPassword(String password)
- {
- this.password = password;
- return this;
- }
- public Email withTo(String to)
- {
- this.to = to;
- return this;
- }
- public Email withSubject(String subject)
- {
- this.subject = subject;
- return this;
- }
- public Email withBody(String body)
- {
- this.body = body;
- return this;
- }
- public void send()
- {
- new Thread(new Runnable()
- {
- public void run()
- {
- try{
- Properties props = new Properties();
- props.put("mail.smtp.host", Email.this.smtpServer);
- //props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.port", "587");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.auth", "true");
- //props.put("mail.smtp.port", "465");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(
- props,
- new Authenticator()
- {
- protected PasswordAuthentication getPasswordAuthentication()
- {
- return new PasswordAuthentication(Email.this.username, Email.this.password);
- }
- });
- Message message = new MimeMessage(session);
- message.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(Email.this.to));
- message.setSubject(Email.this.subject);
- message.setText(Email.this.body);
- Transport.send(message);
- }
- catch (MessagingException e)
- {
- e.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement