最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring+quartz實(shí)現(xiàn)定時(shí)發(fā)送郵件功能實(shí)例

 更新時(shí)間:2017年03月14日 11:26:59   作者:逍遙衛(wèi)子  
spring提供的定時(shí)發(fā)送郵件功能一直深受廣大web開(kāi)發(fā)者的喜愛(ài),這篇文章主要介紹了Spring+quartz實(shí)現(xiàn)定時(shí)發(fā)送郵件功能實(shí)例,有興趣的可以了解一下。

在做具體的系統(tǒng)管理時(shí),往往會(huì)有每隔一段時(shí)間發(fā)給用戶一些郵件的業(yè)務(wù),現(xiàn)在參考的網(wǎng)上的大部分代碼,寫(xiě)下了我自己的代碼。

在ApplicationContext.xml的內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<bean id="testQuartz" class="com.zhuyun.net.AttachmentEmailUtil"></bean>
 
 <!-- bean觸發(fā)方法配置 -->
 <bean name="quartzBean"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!-- bean名字 -->
  <property name="targetObject" ref="testQuartz" /><!--目標(biāo)對(duì)象-->
  <!-- bean方法 -->
  <property name="targetMethod"><!--目標(biāo)方法-->
   <value>sendEmail</value>
  </property>
  <property name="concurrent"><!--配置為false不允許任務(wù)并發(fā)執(zhí)行-->
   <value>false</value>
  </property>
 </bean>
  
  <!-- bean觸發(fā)時(shí)間配置,指定具體的時(shí)間執(zhí)行 --> 
  <bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> 
   <!-- 觸發(fā)bean配置 --> 
   <property name="jobDetail" ref="quartzBean" /> 
   <!-- 觸發(fā)時(shí)間配置 --> 
   <property name="cronExpression"> 
    <value>0 0 10 1 * ?</value> 
   </property> 
  </bean> 
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="default" autowire="default">
 <property name="triggers">
  <list>
   <ref local="quartzTrigger" />
  </list>
 </property>
 <property name="autoStartup" value="true" />
</bean>

</beans>

至于方法如下:

package com.zhuyun.net;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;



public class AttachmentEmailUtil {

 private String host = ""; // smtp服務(wù)器
 private String from = ""; // 發(fā)件人地址
 private String to = ""; // 收件人地址
 private String affix = ""; // 附件地址
 private String affixName = ""; // 附件名稱
 private String user = ""; // 用戶名
 private String pwd = ""; // 密碼
 private String subject = ""; // 郵件標(biāo)題

 public void setAddress(String from, String to, String subject) {
  this.from = from;
  this.to = to;
  this.subject = subject;
 }

 public void setAffix(String affix, String affixName) {
  this.affix = affix;
  this.affixName = affixName;
 }

 public void send(String host, String user, String pwd) {
  this.host = host;
  this.user = user;
  this.pwd = pwd;

  Properties props = new Properties();

  // 設(shè)置發(fā)送郵件的郵件服務(wù)器的屬性(這里使用網(wǎng)易的smtp服務(wù)器)
  props.put("mail.smtp.host", host);
  // 需要經(jīng)過(guò)授權(quán),也就是有戶名和密碼的校驗(yàn),這樣才能通過(guò)驗(yàn)證
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", 465);
  props.put("mail.smtp.ssl.enable", true);
  // 用剛剛設(shè)置好的props對(duì)象構(gòu)建一個(gè)session
  Session session = Session.getDefaultInstance(props);

  // 有了這句便可以在發(fā)送郵件的過(guò)程中在console處顯示過(guò)程信息,供調(diào)試使
  // 用(你可以在控制臺(tái)(console)上看到發(fā)送郵件的過(guò)程)
  session.setDebug(true);

  // 用session為參數(shù)定義消息對(duì)象
  MimeMessage message = new MimeMessage(session);
  try {
   // 加載發(fā)件人地址
   message.setFrom(new InternetAddress(from));
   // 加載收件人地址
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   // 加載標(biāo)題
   message.setSubject(subject);

   // 向multipart對(duì)象中添加郵件的各個(gè)部分內(nèi)容,包括文本內(nèi)容和附件
   Multipart multipart = new MimeMultipart();

   // 設(shè)置郵件的文本內(nèi)容
   BodyPart contentPart = new MimeBodyPart();
   contentPart.setText("第二種方法···");
   multipart.addBodyPart(contentPart);
   // 添加附件
   BodyPart messageBodyPart = new MimeBodyPart();
   DataSource source = new FileDataSource(affix);
   // 添加附件的內(nèi)容
   messageBodyPart.setDataHandler(new DataHandler(source));
   // 添加附件的標(biāo)題
   // 這里很重要,通過(guò)下面的Base64編碼的轉(zhuǎn)換可以保證你的中文附件標(biāo)題名在發(fā)送時(shí)不會(huì)變成亂碼
   sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
   messageBodyPart.setFileName("=?GBK?B?"
     + enc.encode(affixName.getBytes()) + "?=");
   multipart.addBodyPart(messageBodyPart);

   // 將multipart對(duì)象放到message中
   message.setContent(multipart);
   // 保存郵件
   message.saveChanges();
   // 發(fā)送郵件
   Transport transport = session.getTransport("smtp");
   // 連接服務(wù)器的郵箱
   transport.connect(host, user, pwd);
   // 把郵件發(fā)送出去
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public void sendEmail() {
  File file = new File("/root/EmailFile/content.csv");
  if (!file.exists()) {
   try {
    file.createNewFile();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  String pushInfoContent = "成功了";

  FileWriter writer = null;
  try {
   // 打開(kāi)一個(gè)寫(xiě)文件器,構(gòu)造函數(shù)中的第二個(gè)參數(shù)true表示以追加形式寫(xiě)文件
   writer = new FileWriter(file, true);
   writer.write(pushInfoContent + "\r\n");
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    if (writer != null) {
     writer.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

  AttachmentEmailUtil cn = new AttachmentEmailUtil();
  // 設(shè)置發(fā)件人地址、收件人地址和郵件標(biāo)題
  cn.setAddress("發(fā)件人地址", "收件人地址", "一個(gè)帶附件的JavaMail郵件(標(biāo)題)");
  // 設(shè)置要發(fā)送附件的位置和標(biāo)題
  cn.setAffix("附件的位置", "附件的文件名");
  // 設(shè)置smtp服務(wù)器以及郵箱的帳號(hào)和密碼
  cn.send("smtp.qq.com", "帳號(hào)", "密碼");

  if (file.exists()) {
   file.delete();
  }
 }
}

大概就是這樣子了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java性能優(yōu)化之關(guān)于大對(duì)象復(fù)用的目標(biāo)和注意點(diǎn)

    Java性能優(yōu)化之關(guān)于大對(duì)象復(fù)用的目標(biāo)和注意點(diǎn)

    這篇文章主要介紹了Java性能優(yōu)化之關(guān)于大對(duì)象復(fù)用的目標(biāo)和注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • SpringBoot使用MockMvc進(jìn)行單元測(cè)試的實(shí)例代碼

    SpringBoot使用MockMvc進(jìn)行單元測(cè)試的實(shí)例代碼

    在Spring Boot應(yīng)用程序中,使用MockMvc進(jìn)行單元測(cè)試是一種有效的方式,可以驗(yàn)證控制器的行為和API的正確性,在這篇博客中,我們將介紹如何使用MockMvc對(duì)用戶控制器進(jìn)行測(cè)試,感興趣的朋友可以參考下
    2024-01-01
  • Spring Security實(shí)現(xiàn)多次登錄失敗后賬戶鎖定功能

    Spring Security實(shí)現(xiàn)多次登錄失敗后賬戶鎖定功能

    當(dāng)用戶多次登錄失敗的時(shí)候,我們應(yīng)該將賬戶鎖定,等待一定的時(shí)間之后才能再次進(jìn)行登錄操作。今天小編給大家分享Spring Security實(shí)現(xiàn)多次登錄失敗后賬戶鎖定功能,感興趣的朋友一起看看吧
    2019-11-11
  • MyBatis在Mapper中傳遞多個(gè)參數(shù)的四種方法詳解

    MyBatis在Mapper中傳遞多個(gè)參數(shù)的四種方法詳解

    這篇文章主要介紹了MyBatis在Mapper中傳遞多個(gè)參數(shù)的四種方法,文章通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,,需要的朋友可以參考下
    2023-09-09
  • SpringBoot實(shí)現(xiàn)redis緩存菜單列表

    SpringBoot實(shí)現(xiàn)redis緩存菜單列表

    本文主要介紹了SpringBoot實(shí)現(xiàn)redis緩存菜單列表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Mybatis 緩存原理及失效情況解析

    Mybatis 緩存原理及失效情況解析

    這篇文章主要介紹了Mybatis 緩存原理及失效情況解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問(wèn)題

    自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問(wèn)題

    這篇文章主要介紹了自主配置數(shù)據(jù)源,mybatis/plus不打印sql日志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java 類在 Tomcat 中是如何加載的(過(guò)程分析)

    Java 類在 Tomcat 中是如何加載的(過(guò)程分析)

    這篇文章主要介紹了Java 類在 Tomcat 中是如何加載的過(guò)程分析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 深入理解java1.8之supplier

    深入理解java1.8之supplier

    這篇文章主要介紹了深入理解java1.8之supplier,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Spring?AOP?的實(shí)現(xiàn)和切點(diǎn)表達(dá)式的實(shí)現(xiàn)方式

    Spring?AOP?的實(shí)現(xiàn)和切點(diǎn)表達(dá)式的實(shí)現(xiàn)方式

    本文給大家介紹了Spring?AOP的基本概念、通知類型、切點(diǎn)表達(dá)式和切面優(yōu)先級(jí),并通過(guò)示例代碼展示了如何實(shí)現(xiàn)這些功能,感興趣的朋友跟隨小編一起看看吧
    2024-12-12

最新評(píng)論

庆安县| 周至县| 新竹县| 蓬安县| 东乡| 万全县| 禹州市| 特克斯县| 马鞍山市| 重庆市| 乌恰县| 平阳县| 商河县| 沈阳市| 安龙县| 彝良县| 上栗县| 迁西县| 塔城市| 阿拉尔市| 金沙县| 鄄城县| 江西省| 石阡县| 蓬安县| 承德县| 凤翔县| 丰县| 壤塘县| 南城县| 饶阳县| 新乡县| 苍南县| 长岛县| 江北区| 定边县| 日照市| 蓝田县| 乌鲁木齐县| 武鸣县| 独山县|