Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class Notif{
- private static Message msg;
- private InternetAddress fromAdd;
- private static final String PASS = "***********";
- private static final String FROM = "***************";
- private static final String[] TO = {"**************"};
- public static void main(String[] args){
- Notif n = new Notif();
- n.mail(TO, FROM, "test test hai thur");
- }
- public void mail(String[] recipients, String from, String message){
- msg = new MimeMessage(getSession());
- try{
- fromAdd = new InternetAddress(from);
- msg.setFrom(fromAdd);
- InternetAddress[] to = new InternetAddress[recipients.length];
- int i = 0;
- for(String r : recipients){
- to[i] = new InternetAddress(r);
- i++;
- }
- msg.setRecipients(Message.RecipientType.TO, to);
- msg.setSubject("null");
- msg.setContent(message, "text/plain");
- Transport.send(msg);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- private Session getSession(){
- Authenticator authenticator = new Authenticator();
- Properties properties = new Properties();
- properties.setProperty("mail.smtp.submitter", FROM);
- properties.setProperty("mail.smtp.auth", "true");
- properties.setProperty("mail.smtp.host", "mail.gmx.com");
- properties.setProperty("mail.smtp.port", "25");
- return Session.getInstance(properties, authenticator);
- }
- private class Authenticator extends javax.mail.Authenticator{
- private PasswordAuthentication authentication;
- public Authenticator(){
- String username = FROM;
- String password = PASS;
- authentication = new PasswordAuthentication(username, password);
- }
- protected PasswordAuthentication getPasswordAuthentication(){
- return authentication;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement