Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. /*
  2. * This is Java code to send email using Java Mail API
  3. */
  4. import java.util.Date;
  5. import java.util.Properties;
  6.  
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeMessage;
  14.  
  15. public class SendEmailExample2 {
  16.  
  17. public static void main(String[] args) {
  18.  
  19.  
  20. sendEmail("from@gmail.com", "your password","to@outlook.com");
  21.  
  22.  
  23. }
  24.  
  25. public static void sendEmail(String from,String pwd, String to ) {
  26.  
  27.  
  28. final String user = from;
  29. final String password = pwd;
  30. String host = "smtp.gmail.com";
  31.  
  32. Properties props = new Properties();
  33. props.put("mail.smtp.host", host);
  34.  
  35. props.put("mail.smtp.auth", "true");
  36. props.put("mail.smtp.starttls.enable", "true");
  37. props.put("mail.smtp.port", "587");
  38.  
  39. Session session = Session.getInstance(props,
  40. new javax.mail.Authenticator() {
  41. protected PasswordAuthentication getPasswordAuthentication() {
  42. return new PasswordAuthentication(user, password);
  43. }
  44. });
  45.  
  46. try {
  47. MimeMessage message = new MimeMessage(session);
  48. message.setFrom(new InternetAddress(user));
  49. message.addRecipient(Message.RecipientType.TO, new InternetAddress(
  50. to));
  51. message.setSubject("YaHOOOOOOoo");
  52. message.setText("Sending email using JavaMail API Part 2"+ new Date());
  53.  
  54. // send the message
  55. Transport.send(message);
  56.  
  57. System.out.println("message sent successfully...");
  58.  
  59. }
  60.  
  61. catch (com.sun.mail.smtp.SMTPSendFailedException e) {
  62. e.printStackTrace();
  63. } catch (MessagingException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement