首页 > 代码库 > mail 发送邮件

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:‘商品编号‘,width:100},
              {field:‘name‘,title:‘商品名称‘,width:100},
              {field:‘storenum‘,title:‘库存数量‘,width:100},
              {field:‘outnum‘,title:‘待发货数量‘,width: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="http://www.mamicode.com/ui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/ui/themes/icon.css">
<script type="text/javascript" src="http://www.mamicode.com/ui/jquery.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/ui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/ui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/ui/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="http://www.mamicode.com/js/storealert.js"></script>

</head>
<body>
	
<table id="grid"></table>
</body>

  

mail 发送邮件