Guest User

Untitled

a guest
Mar 17th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.io.UnsupportedEncodingException;
  2. import java.util.Properties;
  3.  
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.AddressException;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12.  
  13.  
  14. public class SendMail {
  15.  
  16.     public static void main(String[] args) {
  17.  
  18.         final String username = "your_user_name@gmail.com";
  19.         final String password = "yourpassword";
  20.  
  21.         Properties props = new Properties();
  22.         props.put("mail.smtp.starttls.enable", "true");
  23.         props.put("mail.smtp.auth", "true");
  24.         props.put("mail.smtp.host", "smtp.gmail.com");
  25.         props.put("mail.smtp.port", "587");
  26.  
  27.         Session session = Session.getInstance(props,
  28.           new javax.mail.Authenticator() {
  29.             protected PasswordAuthentication getPasswordAuthentication() {
  30.                 return new PasswordAuthentication(username, password);
  31.             }
  32.           });
  33.  
  34.         try {
  35.  
  36.             Message message = new MimeMessage(session);
  37.             message.setFrom(new InternetAddress("your_user_name@gmail.com"));
  38.             message.setRecipients(Message.RecipientType.TO,
  39.                 InternetAddress.parse("to_email_address@domain.com"));
  40.             message.setSubject("Testing Subject");
  41.             message.setText("Dear Mail Crawler,"
  42.                 + "\n\n No spam to my email, please!");
  43.  
  44.             Transport.send(message);
  45.  
  46.             System.out.println("Done");
  47.  
  48.         } catch (MessagingException e) {
  49.             throw new RuntimeException(e);
  50.         }
  51.     }
  52. }
Add Comment
Please, Sign In to add comment