Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class AmazonSESSample {
  6.  
  7.     static final String FROM = "SENDER@EXAMPLE.COM";   // Replace with your "From" address. This address must be verified.
  8.     static final String TO = "RECIPIENT@EXAMPLE.COM";  // Replace with a "To" address. If your account is still in the
  9.                                                        // sandbox, this address must be verified.
  10.    
  11.     static final String BODY = "This email was sent through the Amazon SES SMTP interface by using Java.";
  12.     static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
  13.    
  14.     // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
  15.     static final String SMTP_USERNAME = "YOUR_SMTP_USERNAME";  // Replace with your SMTP username.
  16.     static final String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD";  // Replace with your SMTP password.
  17.    
  18.     // Amazon SES SMTP host name. This example uses the US West (Oregon) region.
  19.     static final String HOST = "email-smtp.us-west-2.amazonaws.com";    
  20.    
  21.     // The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use
  22.     // STARTTLS to encrypt the connection.
  23.     static final int PORT = 25;
  24.  
  25.     public static void main(String[] args) throws Exception {
  26.  
  27.         // Create a Properties object to contain connection configuration information.
  28.         Properties props = System.getProperties();
  29.         props.put("mail.transport.protocol", "smtps");
  30.         props.put("mail.smtp.port", PORT);
  31.        
  32.         // Set properties indicating that we want to use STARTTLS to encrypt the connection.
  33.         // The SMTP session will begin on an unencrypted connection, and then the client
  34.         // will issue a STARTTLS command to upgrade to an encrypted connection.
  35.         props.put("mail.smtp.auth", "true");
  36.         props.put("mail.smtp.starttls.enable", "true");
  37.         props.put("mail.smtp.starttls.required", "true");
  38.  
  39.         // Create a Session object to represent a mail session with the specified properties.
  40.         Session session = Session.getDefaultInstance(props);
  41.  
  42.         // Create a message with the specified information.
  43.         MimeMessage msg = new MimeMessage(session);
  44.         msg.setFrom(new InternetAddress(FROM));
  45.         msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
  46.         msg.setSubject(SUBJECT);
  47.         msg.setContent(BODY,"text/plain");
  48.            
  49.         // Create a transport.        
  50.         Transport transport = session.getTransport();
  51.                    
  52.         // Send the message.
  53.         try
  54.         {
  55.             System.out.println("Attempting to send an email through the Amazon SES SMTP interface...");
  56.            
  57.             // Connect to Amazon SES using the SMTP username and password you specified above.
  58.             transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
  59.            
  60.             // Send the email.
  61.             transport.sendMessage(msg, msg.getAllRecipients());
  62.             System.out.println("Email sent!");
  63.         }
  64.         catch (Exception ex) {
  65.             System.out.println("The email was not sent.");
  66.             System.out.println("Error message: " + ex.getMessage());
  67.         }
  68.         finally
  69.         {
  70.             // Close and terminate the connection.
  71.             transport.close();         
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement