• Java 超简单实现发送邮件(可动态控制发送人数)


    发送邮件的实现

    需要事先引入以下几个架包,最重要的架包是jodd-3.7这个

    以上架包下载地址:http://pan.baidu.com/s/1kVs7Tyv  提取密码:h22x

    新建一个Util类,其中emails.txt 是用来动态配置需要发送邮件的发送对象

    package quartz;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     *
     * @author DONG
     */
    public class Util{
        public static final String format = "HH:mm";
        public static final SimpleDateFormat sdf = new SimpleDateFormat(format);
        public static String content = "以下电桩断网已超过1小时" +"【"+ sdf.format(new Date())+"】";//发送邮件内容
        public static Date lastSend = null;
        
        public static List getEmailList(){
            return getList("emails.txt");
        }
        
        public static List getList(String fileName){
            try{
                InputStream is = Util.class.getResourceAsStream(fileName);
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                List list = new ArrayList();
                String line = "";
                while( (line = br.readLine()) != null ){
                    if(!"".equals(line.trim())) list.add(line);
                }
                br.close();
                isr.close();
                is.close();
                return list;
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

    以上代码可直接copy复用

    接下来就是发送邮件了

    public void run() {
    
                List<String> emails = Util.getEmailList();//获取邮件发送对象的集合
                if (emails.isEmpty()) {
                    System.out.println("no email receiver");
                    return;
                }
                String from = "********@sina.com";//用户名,登录邮箱的账号
                String psw = "**********";//密码
                String[] to = emails.toArray(new String[0]);
                Email email = Email.create()
                        .from(from).to(to)
                        .subject("电桩断网超时提醒")//邮件主题
                        .addText(Util.content);//邮件内容
    
                SmtpServer smtpServer = SmtpServer.create("smtp.sina.com")//调用新浪邮箱服务器
                        .authenticateWith(from, psw);
    
                SendMailSession session = smtpServer.createSession();
                session.open();
                session.sendMail(email);//执行发送
                session.close();
    
                System.out.println("--email send success. receivers: " + Arrays.deepToString(emails.toArray()));
    
            }

    在需要发送邮件的地方调用run方法即可。以上就是一个超简易的发送邮件示例,亲测有效

     下一篇,将补充如何自定义添加邮件内容

  • 相关阅读:
    【DWT笔记】基于小波变换的降噪技术
    【DWT笔记】傅里叶变换与小波变换
    Matlab命令——目录操作(windows&Linux)
    【DCT笔记】DCT变换、DCT反变换、分块DCT变换
    JSOI2018 防御网络
    NOI2018 屠龙勇士
    CRT&EXCRT学习笔记
    CF662C Binary Table
    HNOI2017 礼物
    ZJOI2014 力
  • 原文地址:https://www.cnblogs.com/zhangxiaoyong/p/5473498.html
Copyright © 2020-2023  润新知