Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Morkaz.skMorkaz.main;
- 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;
- public class EmailSender {
- private final String username;
- private final String password;
- private Properties mailProps;
- public EmailSender(){
- this.username = ConfigLoader.emailUsername;
- this.password = ConfigLoader.emailPassword;
- this.mailProps = new Properties();
- this.mailProps.put("mail.smtp.auth", ConfigLoader.emailPasswordRequired);
- this.mailProps.put("mail.smtp.starttls.enable", ConfigLoader.emailTLSRequired);
- this.mailProps.put("mail.smtp.host", ConfigLoader.emailServerHost);
- this.mailProps.put("mail.smtp.port", ConfigLoader.emailPort);
- }
- public boolean send(String subject, String text, String address){
- boolean sent = false;
- try{
- Message message = new MimeMessage(Session.getInstance(this.mailProps,
- new Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication(){
- return new PasswordAuthentication(EmailSender.this.username, EmailSender.this.password);
- }
- }));
- message.setFrom(new InternetAddress(this.username));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address));
- message.setSubject(subject);
- message.setText(text);
- Transport.send(message);
- sent = true;
- } catch (MessagingException e){
- e.printStackTrace();
- sent = false;
- }
- return sent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement