java 使用 Amazon SES 发送电子邮件

java 使用 Amazon SES发送电子邮件,有三种方式,具体哪三种请移步这里

这里只介绍一种:利用Amazon SES 的 SMTP 服务,javax.mail发送邮件。此方法也可以直接用于163邮箱(需要自己开启邮箱的SMTP服务),亲测有效。

1、先将发送邮件的邮箱在Amazon SES里面验证一下,具体验证方法请参考这里。在使用过程中,发送送邮件返回错误状态码具体参考这里

2、Amazon SES默认是Sandbox模式,需要发送申请production access,要等他们review。

3、代码示例:(代码自行抽取)

package com.iot.task;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;

import org.quartz.*;
import redis.clients.jedis.Jedis;


import javax.mail.*;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.*;
import java.util.Properties;

import java.util.List;

public class PushAlarmNotificationTask implements Job {

    // Replace sender@example.com with your "From" address.
    // This address must be verified with Amazon SES.
    private static final String FROM = "";
    private static final String FROMNAME = "";

    private static final String TO = "";

    // Replace smtp_username with your Amazon SES SMTP user name.
    private static final String SMTP_USERNAME = "";

    // Replace smtp_password with your Amazon SES SMTP password.
    private static final String SMTP_PASSWORD = "";

    // The name of the Configuration Set to use for this message.
    // If you comment out or remove this variable, you will also need to
    // comment out or remove the header below.
    private static final String CONFIGSET = "ConfigSet";

    // Amazon SES SMTP host name. This example uses the 美国西部(俄勒冈) region.
    // See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
    // for more information.
    private static final String HOST = "email-smtp.us-east-1.amazonaws.com";

    // The port you will connect to on the Amazon SES SMTP endpoint.
    private static final int PORT = 587;

    private static final String SUBJECT = "";

    private String BODY = String.join(
            System.getProperty("line.separator"),
            "");

    private String Message = "Hello Email";

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        Transport transport = null;
        try {
            // 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);
            BODY = BODY + Message;
            msg.setContent(BODY, "text/html");

            // Add a configuration set header. Comment or delete the
            // next line if you are not using a configuration set
            //msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);

            // Create a transport.
            transport = session.getTransport();


            // 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());

            sendNum++;
        } catch (UnsupportedEncodingException | MessagingException ex) {
            ex.printStackTrace();
        } finally {
            // Close and terminate the connection.
            try {
                transport.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
            

        
    }

}

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值