Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. import java.util.Properties;
  2. import javax.mail.Message;
  3. import javax.mail.PasswordAuthentication;
  4. import javax.mail.Session;
  5. import javax.mail.Transport;
  6. import javax.mail.internet.InternetAddress;
  7. import javax.mail.internet.MimeMessage;
  8.  
  9. public class Notif{
  10.  
  11.    private static Message msg;
  12.    private InternetAddress fromAdd;
  13.    private static final String PASS = "***********";
  14.    private static final String FROM = "***************";
  15.    private static final String[] TO = {"**************"};
  16.  
  17.    public static void main(String[] args){
  18.       Notif n = new Notif();
  19.       n.mail(TO, FROM, "test test hai thur");
  20.    }
  21.  
  22.    public void mail(String[] recipients, String from, String message){
  23.       msg = new MimeMessage(getSession());
  24.       try{
  25.          fromAdd = new InternetAddress(from);
  26.          msg.setFrom(fromAdd);
  27.          InternetAddress[] to = new InternetAddress[recipients.length];
  28.          int i = 0;
  29.          for(String r : recipients){
  30.             to[i] = new InternetAddress(r);
  31.             i++;
  32.          }
  33.          msg.setRecipients(Message.RecipientType.TO, to);
  34.          msg.setSubject("null");
  35.          msg.setContent(message, "text/plain");
  36.          Transport.send(msg);
  37.       }
  38.       catch(Exception e){
  39.          e.printStackTrace();
  40.       }
  41.    }
  42.  
  43.    private Session getSession(){
  44.       Authenticator authenticator = new Authenticator();
  45.  
  46.       Properties properties = new Properties();
  47.       properties.setProperty("mail.smtp.submitter", FROM);
  48.       properties.setProperty("mail.smtp.auth", "true");
  49.  
  50.       properties.setProperty("mail.smtp.host", "mail.gmx.com");
  51.       properties.setProperty("mail.smtp.port", "25");
  52.  
  53.       return Session.getInstance(properties, authenticator);
  54.    }
  55.  
  56.    private class Authenticator extends javax.mail.Authenticator{
  57.       private PasswordAuthentication authentication;
  58.  
  59.       public Authenticator(){
  60.          String username = FROM;
  61.          String password = PASS;
  62.          authentication = new PasswordAuthentication(username, password);
  63.       }
  64.  
  65.       protected PasswordAuthentication getPasswordAuthentication(){
  66.          return authentication;
  67.       }
  68.    }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement