Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import javax.activation.*;
  6.  
  7. public class SendEmailCheck {
  8.  
  9. public static void main(String [] args) throws Exception {
  10. // Recipient's email ID needs to be mentioned.
  11. String to = "to@gmail.com";//Receipt mail id
  12.  
  13. // Sender's email ID needs to be mentioned
  14. final String from = "from@gmail.com";// Sender email id
  15.  
  16. // Assuming you are sending email from localhost
  17. // String host = "localhost:8080";
  18.  
  19. // Get system properties
  20. //Properties properties = System.getProperties();
  21. Properties props=new Properties();
  22.  
  23. // Setup mail server
  24. // properties.setProperty("mail.smtp.host", host);
  25. props.put("mail.smtp.auth", "true");
  26. //props.put("mail.smtp.starttle.enable", "true");
  27. props.put("mail.smtp.host", "smtp.gmail.com");
  28. props.put("mail.smtp.socketFactory.port", "465");
  29. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  30.  
  31. props.put("mail.smtp.port", "465");
  32. // Get the default Session object.
  33. //Session session = Session.getDefaultInstance(properties);
  34. Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator(){
  35. protected PasswordAuthentication getPasswordAuthentication(){
  36. return new PasswordAuthentication(from,"*****");//*****-sender password
  37. }
  38. });
  39. try {
  40. // Create a default MimeMessage object.
  41. MimeMessage message = new MimeMessage(session);
  42.  
  43. // Set From: header field of the header.
  44. message.setFrom(new InternetAddress(from));
  45.  
  46. // Set To: header field of the header.
  47. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  48.  
  49. // Set Subject: header field
  50. message.setSubject("This is the Subject Line!");
  51.  
  52. // Now set the actual message
  53. // message.setText("This is actual message","text/html");
  54. message.setContent(new String("This is actual message".getBytes(), "iso-8859-1"), "text/html; charset=\"iso-8859-1\"");
  55.  
  56.  
  57. Transport.send(message);
  58. System.out.println("Sent message successfully....");
  59. }catch (Exception mex) {
  60. mex.printStackTrace();
  61. //throw new RuntimeException(mex);
  62.  
  63.  
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement