Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.security.Provider;
  2. import java.util.*;
  3. import javax.*;
  4. import javax.mail.Message;
  5. import javax.mail.Session;
  6. import javax.mail.Transport;
  7. import javax.mail.internet.InternetAddress;
  8. import javax.mail.internet.MimeMessage;
  9.  
  10.  
  11. public class EmailSend extends TimerTask {
  12.        
  13.     public static void main(String[] args) {
  14.         Timer t = new Timer();
  15.         t.schedule(new EmailSend(), 0, 10000);
  16.     }
  17.  
  18.     @Override
  19.     public void run() {
  20.    
  21.         try {
  22.             String host = "smtp.gmail.com";
  23.             String user = "NAZWA_USERA";
  24.             String pass = "some_pass";
  25.             String to = "odbiorca@gmail.com";
  26.             String from = "javatest95@gmail.com";
  27.             String subject = "Test wysyłania maila.";
  28.             String messageText = "";
  29.             boolean sessionDebug = false;
  30.  
  31.             Properties props = System.getProperties();
  32.  
  33.             props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
  34.             props.put("mail.smtp.starttls.enable", "true");
  35.             props.put("mail.smtp.host", host);
  36.             props.put("mail.smtp.port", "587");
  37.             props.put("mail.smtp.auth", "true");
  38.             props.put("mail.smtp.starttls.required", "true");
  39.  
  40.             //java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  41.             Session mailSession = Session.getDefaultInstance(props, null);
  42.             mailSession.setDebug(sessionDebug);
  43.             Message msg = new MimeMessage(mailSession);
  44.             msg.setFrom(new InternetAddress(from));
  45.             InternetAddress[] address = { new InternetAddress(to) };
  46.             msg.setRecipients(Message.RecipientType.TO, address);
  47.             msg.setSubject(subject);
  48.             msg.setSentDate(new Date());
  49.             msg.setText(messageText);
  50.  
  51.             Transport transport = mailSession.getTransport("smtp");
  52.             transport.connect(host, user, pass);
  53.             transport.sendMessage(msg, msg.getAllRecipients());
  54.             transport.close();
  55.             System.out.println("message send successfully");
  56.         } catch (Exception e) {
  57.             System.out.println(e);
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement