Advertisement
Guest User

Untitled

a guest
Aug 29th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package com.javabp.jmailer;
  2.  
  3.  
  4. import java.util.Properties;
  5.  
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13. public class JMailer {
  14. public static void main(String[] args)
  15. {
  16. /***** CHANGE THESE FOUR VARIABLE VALUES TO REFLECT YOUR ENVIRONMENT ******/
  17. String user = "user"; // Newly created user on JAMES Server
  18. String password = "password"; // user password
  19.  
  20. String fromAddress = "user@example.com"; // newlycreateduser@localhost
  21. String toAddress = "myaccount@gmail.com";
  22.  
  23.  
  24. // Create a mail session
  25. Properties properties = new Properties();
  26. properties.put("mail.transport.protocol", "smtp");
  27. properties.put("mail.smtp.host", "example.com");
  28. properties.put("mail.smtp.port", "25");
  29. properties.put("mail.smtp.username", user);
  30. properties.put("mail.smtp.password", password);
  31. Session session = Session.getDefaultInstance(properties, null);
  32.  
  33. try
  34. {
  35. Message message = new MimeMessage(session);
  36. message.setFrom(new InternetAddress(fromAddress));
  37. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
  38.  
  39. message.setSubject("Email From my Own Server");
  40. message.setText("Test Mail sent from My Apache James Server!!");
  41. Transport.send(message);
  42.  
  43. System.out.println("Email sent successfully");
  44. }
  45. catch (MessagingException e)
  46. {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement