Advertisement
Guest User

Untitled

a guest
May 13th, 2017
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import javax.mail.Message;
  2. import javax.mail.PasswordAuthentication;
  3. import javax.mail.Session;
  4. import javax.mail.Transport;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import java.util.Properties;
  8.  
  9. public class EmailSender {
  10.  
  11.     public static void main (String[] args) {
  12.         EmailSender es1 = new EmailSender();
  13.         es1.emailSender("kapa@gmaiil.com", "kapa", "asdadsadasda");
  14.  
  15.     }
  16.  
  17.  
  18.     public String emailSender (String email, String subject, String content) {
  19.  
  20.         final String username = "emailsendTM@gmail.com";
  21.         final String password = "tm123456";
  22.         final String toEmail = "agelos.tsal@gmail.com";
  23.         content = "Sender's email: " + email + "\n" + "\n" + "\n" + content;
  24.  
  25.         try {
  26.  
  27.             Properties properties = new Properties();
  28.             properties.put("mail.smtp.auth", true);
  29.             properties.put("mail.smtp.starttls.enable", "true");
  30.             properties.put("mail.smtp.host", "smtp.gmail.com");
  31.             properties.put("mail.smtp.port", "587");
  32.             properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
  33.  
  34.            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  35.  
  36.             System.out.print("done1\n");
  37.  
  38.             Session mailsession = Session.getInstance(properties,
  39.                     new javax.mail.Authenticator(){
  40.                         protected PasswordAuthentication getPasswordAuthentication(){
  41.                             return  new PasswordAuthentication(username, password);
  42.                         }
  43.                     });
  44.  
  45.             System.out.print("done2\n");
  46.  
  47. //            mailsession.setDebug(true);
  48.  
  49.             Message mailMessage = new MimeMessage(mailsession);
  50.  
  51.             mailMessage.setFrom(new InternetAddress(username));
  52.             mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
  53.             mailMessage.setText(content);
  54.             mailMessage.setSubject(subject);
  55.  
  56.             System.out.print("done3\n");
  57.  
  58.             Transport transport = mailsession.getTransport("smtp");
  59.             System.out.print("done4\n");
  60.  
  61.             transport.connect("smtp.gmail.com", username, password);
  62.             System.out.print("done5\n");
  63.             transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
  64.             System.out.print("done6\n");
  65.             Transport.send(mailMessage);
  66.  
  67.             System.out.print("done");
  68.  
  69.  
  70.         }catch(Exception e) {
  71.  
  72.             System.out.print(e + "\n" + "ddddddddddddddddddddddddddddddddddddd");
  73.  
  74.         }
  75.  
  76.         return "";
  77.  
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement