Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 KB | None | 0 0
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.activation.*;
  5.  
  6. import java.util.Properties;
  7.  
  8. import javax.mail.Message;
  9. import javax.mail.MessagingException;
  10. import javax.mail.PasswordAuthentication;
  11. import javax.mail.Session;
  12. import javax.mail.Transport;
  13. import javax.mail.internet.InternetAddress;
  14. import javax.mail.internet.MimeMessage;
  15.  
  16. public class Main {
  17.     public static void main(String[] args) {
  18.         // Recipient's email ID needs to be mentioned.
  19.         String to = "zharikovigor97@gmail.com";//change accordingly
  20.  
  21.         // Sender's email ID needs to be mentioned
  22.         String from = "akirusg@gmail.com";//change accordingly
  23.         final String username = "akirusg@gmail.com";//change accordingly
  24.         final String password = "****";//change accordingly
  25.  
  26.         // Assuming you are sending email through relay.jangosmtp.net
  27.         String host = "smtp.gmail.com";
  28.  
  29.         Properties props = new Properties();
  30.         props.put("mail.smtp.auth", "true");
  31.         props.put("mail.smtp.starttls.enable", "true");
  32.         props.put("mail.smtp.host", host);
  33.         props.put("mail.smtp.port", "587");
  34.  
  35.         // Get the Session object.
  36.         Session session = Session.getInstance(props,
  37.                 new javax.mail.Authenticator() {
  38.                     protected PasswordAuthentication getPasswordAuthentication() {
  39.                         return new PasswordAuthentication(username, password);
  40.                     }
  41.                 });
  42.  
  43.         try {
  44.             // Create a default MimeMessage object.
  45.             Message message = new MimeMessage(session);
  46.  
  47.             // Set From: header field of the header.
  48.             message.setFrom(new InternetAddress(from));
  49.  
  50.             // Set To: header field of the header.
  51.             message.setRecipients(Message.RecipientType.TO,
  52.                     InternetAddress.parse(to));
  53.  
  54.             Calendar calender = Calendar.getInstance();
  55.             calender.roll(Calendar.YEAR, false);
  56.             calender.roll(Calendar.MONTH, false);
  57.  
  58.             message.setSentDate(calender.getTime());
  59.  
  60.             // Set Subject: header field
  61.             message.setSubject("Testing Subject 4");
  62.  
  63.             // Now set the actual message
  64.             message.setText("Hello, this is sample for to check send "
  65.                     + "email using JavaMailAPI ");
  66.  
  67.             // Send message
  68.             Transport.send(message);
  69.  
  70.             System.out.println("Sent message successfully....");
  71.  
  72.         } catch (MessagingException e) {
  73.             throw new RuntimeException(e);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement