• mail 发送邮件


    1.定时发送邮件

    思路:1.添加依赖2.发送邮件的类3.配置文件4. 业务层调用发送邮件 5.action将数据带回前端 6.js编写

    1.添加依赖

    <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.4.4</version>
            </dependency>

     2.发送邮件的类

    //2.发送邮件的类
    public class MailUtil {
    	private JavaMailSender sender; //内置对象
    	private String from;//发件人
    	public void sendMail(String to, String subject, String text) throws Exception{
    		//邮件
    		MimeMessage message = sender.createMimeMessage();
    		//邮件工具
    		MimeMessageHelper helper = new MimeMessageHelper(message);
    		//发件人
    		helper.setFrom(from);
    		//收件人
    		helper.setTo(to);
    		//标题
    		helper.setSubject(subject);
    		//邮件内容
    		helper.setText(text);
    		//发送邮件
    		sender.send(message);
    	}
    	public void setSender(JavaMailSender sender) {
    		this.sender = sender;
    	}
    	public void setFrom(String from) {
    		this.from = from;
    	}
    }
    

    3.配置文件

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="protocol" value="smtp"/>
            <property name="host" value="smtp.zoho.com.cn"/>
            <property name="port" value="465" />
            <property name="username" value="fadali@zoho.com.cn"/>
            <property name="password" value="bB123456"/>
            <property name="javaMailProperties">
                <props>
                    <!-- 验证发件人与邮箱所有人是否一致 -->
                    <prop key="mail.smtp.auth">true</prop>
                    <!-- 发送服务器: smtp.zoho.com.cn, 端口: 465, SSL -->
                    <prop key="mail.smtp.ssl.enable">true</prop>
                </props>
            </property>
        </bean>
        <bean id="mailUtil" class="cn.itcast.erp.util.MailUtil">
            <!-- 调用发送邮件的配置 -->
            <property name="sender" ref="mailSender"></property>
            <!-- 收件人 -->
            <property name="from" value="fadali@zoho.com.cn" />
        </bean>
    </beans>

     4.业务层调用发送邮件

    /**
    	 * 点击按钮,发送邮件
    	 */
    	private String to;//收件人
    	private String subject;//邮件标题
    	private String text;//邮件内容
    	private IStoredetailDao storedetailDao;
    	@Override
    	public void sendStorealertMail() throws Exception {
    		//查询是否有需要预警的数据
    		List<Storealert> storealertList = storedetailDao.getStorealertList();
    		if(null != storealertList && storealertList.size() > 0){
    			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    			
    			mailUtil.sendMail(to, subject.replace("[time]", df.format(new Date())), 
    					text.replace("[count]", storealertList.size() + ""));
    		}
    	}
    

    4.1配置文件 

    <bean id="storedetailBiz" class="cn.itcast.erp.biz.impl.StoredetailBiz">
            <property name="storedetailDao" ref="storedetailDao"></property>
            <property name="goodsDao" ref="goodsDao"></property>
            <property name="storeDao" ref="storeDao"></property>
            <property name="mailUtil" ref="mailUtil"></property>
            <property name="to" value="erik2010163@163.com"></property>
            <property name="subject" value="库存预警_时间:[time]"></property>
            <property name="text" value="亲!有[count]种商品已经库存不足,请登陆ERP系统查看"></property>
        </bean>

    5.action将数据带回前端

    /**
         * 发送库存预警邮件
         */
        public void sendStorealertMail(){
            try {
                storedetailBiz.sendStorealertMail();
                ajaxReturn(true,"库存预警邮件发送成功");
            } catch (Exception e) {
                e.printStackTrace();
                ajaxReturn(false,"库存预警邮件发送失败");
            }
        }

     6.js编写,数据 表格

    $(function(){
        //加载表格数据
        $('#grid').datagrid({
            url:'storedetail_storealertList',
            columns:[[
                  {field:'uuid',title:'商品编号',100},
                  {field:'name',title:'商品名称',100},
                  {field:'storenum',title:'库存数量',100},
                  {field:'outnum',title:'待发货数量',100}
            ]],
            singleSelect: true,
            pagination: true,
            toolbar:[
                {
                    text:'发送预警邮件',
                    iconCls:'icon-alert',
                    handler:function(){
                        $.ajax({
                            url:'storedetail_sendStorealertMail',
                            dataType:'json',//把服务器响应回来的数据转换成json对象
                            type:'post',
                            success:function(rtn){
                                $.messager.alert('提示',rtn.message,'info');
                            }
                        });
                    }
                }         
            ]
        });
    });

     7.html页面

    <link rel="stylesheet" type="text/css"
    	href="ui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="ui/themes/icon.css">
    <script type="text/javascript" src="ui/jquery.min.js"></script>
    <script type="text/javascript" src="ui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="ui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="ui/jquery.serializejson.min.js"></script>
    <script type="text/javascript" src="js/storealert.js"></script>
    
    </head>
    <body>
    	
    <table id="grid"></table>
    </body>

      

  • 相关阅读:
    记录锁
    linux多线程同步pthread_cond_XXX条件变量的理解
    设置创建文件掩码遇到的问题
    函数的可重入和线程安全
    sigemptyset,sigfillset,sigaddset,sigdelset,sigismember,sigprocmask,sigpendmask作用
    嵌入式中断服务函数的一些特点
    linux alarm函数解除read write等函数的阻塞
    孤儿进程,僵死进程
    标准IO的缓冲问题
    《实用技巧》——让你的网站变成响应式的3个简单步骤
  • 原文地址:https://www.cnblogs.com/liushisaonian/p/7091595.html
Copyright © 2020-2023  润新知