Guest User

Untitled

a guest
Oct 6th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. package javaapplication6;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.*;
  5. import javax.mail.internet.*;
  6. class Mailer{
  7. public static void send(String from,String password,String to,String sub,String msg){
  8. //Get properties object
  9.  
  10. Properties props = new Properties();
  11. props.put("mail.smtp.host", "smtp.gmail.com");
  12. props.put("mail.smtp.socketFactory.port", "465");
  13. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  14. props.put("mail.smtp.auth", "true");
  15. props.put("mail.smtp.port", "465");
  16. //get Session
  17. Session session = Session.getDefaultInstance(props,
  18. new javax.mail.Authenticator() {
  19. protected PasswordAuthentication getPasswordAuthentication() {
  20. return new PasswordAuthentication("XXXX@gmail.com","XXXXXXXXX");
  21. }
  22. });
  23. //compose message
  24. try {
  25. MimeMessage message = new MimeMessage(session);
  26. message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  27. message.setSubject(sub);
  28. message.setText(msg);
  29. //send message
  30. Transport.send(message);
  31. System.out.println("message sent successfully");
  32. } catch (MessagingException e) {throw new RuntimeException(e);}
  33.  
  34. }
  35. }
  36. public class SendEmail{
  37. public static void main(String[] args) {
  38. //from,password,to,subject,message
  39. Mailer.send("XXXX@gmail.com","XXXXXXXXX","itkrishcommerce@gmail.com","hello javatpoint","How r u?");
  40. //change from, password and to
  41. }
  42. }
Add Comment
Please, Sign In to add comment