发送电子邮件在AWS

时间:2016-08-23 12:48:28

标签: java amazon-web-services

我正在尝试通过AWS发送电子邮件。我已经离开Sandbox并且获得了US-WEST-2的限制增加。我已经验证了我的"来自"电子邮件地址,  (Constants.FROM_EMAIL)。

但它仍在寻找验证。

我得到的错误是,

com.sun.mail.smtp.SMTPSendFailedException:554邮件被拒绝:未验证电子邮件地址。以下身份未通过检查地区US-WEST-2:abc@gmail.com

我使用下面的代码。

    static final String SMTP_USERNAME = "my user name";
static final String SMTP_PASSWORD = "my password";

// Amazon SES SMTP host name. 
static final String HOST = "email-smtp.us-west-2.amazonaws.com";    

static final int PORT = 25;


/* (non-Javadoc)
 * @see com.email.AWSPostman#createProperties()
 */
public Properties createProperties() {
    Properties props = System.getProperties();
    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtp.port", PORT); 

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");

    return props;
}

/* (non-Javadoc)
 * @see com.email.AWSPostman#createSession(java.util.Properties)
 */
public Session createSession(Properties props) {
    Session session = Session.getDefaultInstance(props);

    return session;
}

/* (non-Javadoc)
 * @see com.email.AWSPostman#createTransport(javax.mail.Session)
 */
public Transport connectTransport(Session session) throws MessagingException {
    Transport transport = session.getTransport();
    transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
    return transport;
}

/* (non-Javadoc)
 * @see com.email.AWSPostman#closeTransport(javax.mail.Session)
 */
public void closeTransport(Transport transport) throws MessagingException {
    transport.close();
}

public Message createMessage(Session session, String to, String subject, String body) throws AddressException, MessagingException {
    // Create a message with the specified information. 
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(Constants.FROM_EMAIL));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setSubject(subject);
    msg.setContent(body,"text/plain");

    return msg;
}

public void sendMessage(Transport transport, Message message) throws MessagingException {
    // Send the email.
    transport.sendMessage(message, message.getAllRecipients());
}

感谢有人可以提供帮助。

0 个答案:

没有答案