Not able to send Email using Amazon SES in Java

前端 未结 2 1553
难免孤独
难免孤独 2021-01-24 02:18

I am using below mentioned code to send Email.

public static void send(String email, String subject, String body) {
    try {
        fromEmail = \"abc.@xyz.com\         


        
相关标签:
2条回答
  • 2021-01-24 02:46

    This is a Sample code to send Email using Amazon SES. Initially when you create an Amazon SES account, the account will be in Sandbox mode which allows you to send only 200 emails. For Switching this to production mode, you need to "Request" for expanding email limit. Please go through the documentation.

    Pre requisites: You need to have Amazon SES account activated. verify email addressses (to and from) or domain if you are currently in Sandbox mode. Under "SMTP settings" in Navigation bar, you can generate your SMTP credentials. This will include a smtp username and password. You can download a csv file containing this details as well.

    sending email using Java :

    public class AmazonSESExample {
    
    
    static final String FROM = "your from email address";
    static final String FROMNAME = "From name";
    
    // Replace recipient@example.com with a "To" address. If your account
    // is still in the sandbox, this address must be verified.
    static final String TO = "receiver email address";
    
    // Replace smtp_username with your Amazon SES SMTP user name.
    static final String SMTP_USERNAME = "username generated under smtp settings";
    
    // Replace smtp_password with your Amazon SES SMTP password.
    static final String SMTP_PASSWORD = "password generated under smtp settings";
    
    // Amazon SES SMTP host name. This example uses the US West (Oregon) region.
    // See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
    // for more information.
    static final String HOST = "email-smtp.us-east-1.amazonaws.com";
    
    // The port you will connect to on the Amazon SES SMTP endpoint.
    static final int PORT = 25;
    
    static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
    
    static final String BODY = String.join(
            System.getProperty("line.separator"),
            "<h1>Amazon SES SMTP Email Test</h1>",
            "<p>This email was sent with Amazon SES using the ",
            "<a href='https://github.com/javaee/javamail'>Javamail Package</a>",
            " for <a href='https://www.java.com'>Java</a>."
    );
    
    public static void main(String[] args) throws Exception {
    
        // Create a Properties object to contain connection configuration information.
        Properties props = System.getProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
    
        // Create a Session object to represent a mail session with the specified properties.
        Session session = Session.getDefaultInstance(props);
    
        // Create a message with the specified information.
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM, FROMNAME));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY, "text/html");
    
    
        // Create a transport.
        Transport transport = session.getTransport();
    
        // Send the message.
        try {
            System.out.println("Sending...");
    
            // Connect to Amazon SES using the SMTP username and password you specified above.
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
    
            // Send the email.
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Email sent!");
        } catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        } finally {
            // Close and terminate the connection.
            transport.close();
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-24 02:51

    The credentials you are providing is incorrect. You are giving IAM username and password. Instead you have to provide the access_key_id and access_key_id.

    AWSCredentials credentials = new BasicAWSCredentials(access_key_id, secret_access_key)
    

    See: Providing AWS Credentials in the AWS SDK for Java

    0 讨论(0)
提交回复
热议问题