Email邮件发送设置 工具开发整理(网易邮箱、Mailgun为例) 上篇

               Email邮件设置   工具开发整理(网易邮箱、Mailgun为例) 上篇

说明:Email邮件 国内国外设置 以网易邮箱和Mailgun为例,并开发工具类。

一、国内 网易邮箱设置

 1.设置STMP 服务

2.获取授权密码 

   授权密码为邮件stmp 服务密码 非网易邮箱密码

3.邮件代码示例

  public static void main(String[] args) {
        try {
            String emailFrom = "";//发件人邮箱
            String emailPwd = "";//密码
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.163.com");// 设置服务器地址
            props.put("mail.store.protocol", "smtp");// 设置协议
            props.put("mail.smtp.port", 25);// 设置端口
            props.put("mail.smtp.auth", true);

            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailFrom, emailPwd);
                }
            };
            Session session = Session.getDefaultInstance(props, authenticator);

            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] address = { new InternetAddress("****@qq.com") };//收件人邮箱
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("这是标题");
            msg.setSentDate(new Date());
            msg.setContent("<a href='http://www.baidu.com'>请点击激活</a>", "text/html;charset=utf-8");
            Transport.send(msg);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

二、国外 Mailgun 邮件设置

1.注册 Mailgun 增加子域

        Mailgun  邮件发送需要域名解析,需要有自己域名。

2.获取解析内容

3.解析域名(阿里云为例)

4.获取发件人邮箱和密码

5.代码示例


    public static void main(String[] args) {
        try {
            String emailFrom = "";//发件人邮箱
            String emailPwd = "";//密码
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.mailgun.org");// 设置服务器地址
            props.put("mail.store.protocol", "smtp");// 设置协议
            props.put("mail.smtp.port", 25);// 设置端口
            props.put("mail.smtp.auth", true);

            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailFrom, emailPwd);
                }
            };
            Session session = Session.getDefaultInstance(props, authenticator);

            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] address = { new InternetAddress("****@qq.com") };//收件人邮箱
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("这是标题");
            msg.setSentDate(new Date());
            msg.setContent("<a href='http://www.baidu.com'>请点击激活</a>", "text/html;charset=utf-8");
            Transport.send(msg);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

  注:邮件工具类 参考https://blog.csdn.net/qq_37184877/article/details/100512095

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要抓取网易邮箱邮件设置邮件的时间范围,你可以使用JavaMail API来实现。 首先,你需要连接到网易邮箱的SMTP服务器,然后使用IMAP协议获取邮件。下面是一个简单的示代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.search.*; public class EmailFetcher { public static void main(String[] args) throws Exception { // 邮箱账号和密码 String username = "your_email@163.com"; String password = "your_password"; // 连接到网易邮箱服务器 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imaps"); props.setProperty("mail.imaps.host", "imap.163.com"); props.setProperty("mail.imaps.port", "993"); Session session = Session.getInstance(props, null); Store store = session.getStore(); store.connect(username, password); // 获取邮件 Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); // 设置时间范围 SearchTerm searchTerm = new ReceivedDateTerm(ComparisonTerm.GE, new Date(2021, 8, 1)); // 执行搜索 Message[] messages = inbox.search(searchTerm); // 输出邮件信息 for (Message message : messages) { System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); System.out.println("Sent date: " + message.getSentDate()); System.out.println(); } // 关闭连接 inbox.close(false); store.close(); } } ``` 在上面的代码中,我们连接到网易邮箱的IMAP服务器,并使用`ReceivedDateTerm`来设置时间范围,只获取2021年8月1日以后收到的邮件。然后使用`inbox.search`方法搜索符合条件的邮件。 需要注意的是,上面的代码只是一个简单的示,实际使用中还需要处理异常、关闭连接等操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值