一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring+quartz實現定時發送郵件功能實例

Spring+quartz實現定時發送郵件功能實例

2020-08-28 10:33逍遙衛子 Java教程

spring提供的定時發送郵件功能一直深受廣大web開發者的喜愛,這篇文章主要介紹了Spring+quartz實現定時發送郵件功能實例,有興趣的可以了解一下。

在做具體的系統管理時,往往會有每隔一段時間發給用戶一些郵件的業務,現在參考的網上的大部分代碼,寫下了我自己的代碼。

在ApplicationContext.xml的內容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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觸發方法配置 -->
 <bean name="quartzBean"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!-- bean名字 -->
  <property name="targetObject" ref="testQuartz" /><!--目標對象-->
  <!-- bean方法 -->
  <property name="targetMethod"><!--目標方法-->
   <value>sendEmail</value>
  </property>
  <property name="concurrent"><!--配置為false不允許任務并發執行-->
   <value>false</value>
  </property>
 </bean>
  
  <!-- bean觸發時間配置,指定具體的時間執行 -->
  <bean id="quartzTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
   <!-- 觸發bean配置 -->
   <property name="jobDetail" ref="quartzBean" />
   <!-- 觸發時間配置 -->
   <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>

至于方法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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服務器
 private String from = ""; // 發件人地址
 private String to = ""; // 收件人地址
 private String affix = ""; // 附件地址
 private String affixName = ""; // 附件名稱
 private String user = ""; // 用戶名
 private String pwd = ""; // 密碼
 private String subject = ""; // 郵件標題
 
 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();
 
  // 設置發送郵件的郵件服務器的屬性(這里使用網易的smtp服務器)
  props.put("mail.smtp.host", host);
  // 需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", 465);
  props.put("mail.smtp.ssl.enable", true);
  // 用剛剛設置好的props對象構建一個session
  Session session = Session.getDefaultInstance(props);
 
  // 有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使
  // 用(你可以在控制臺(console)上看到發送郵件的過程)
  session.setDebug(true);
 
  // 用session為參數定義消息對象
  MimeMessage message = new MimeMessage(session);
  try {
   // 加載發件人地址
   message.setFrom(new InternetAddress(from));
   // 加載收件人地址
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   // 加載標題
   message.setSubject(subject);
 
   // 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
   Multipart multipart = new MimeMultipart();
 
   // 設置郵件的文本內容
   BodyPart contentPart = new MimeBodyPart();
   contentPart.setText("第二種方法···");
   multipart.addBodyPart(contentPart);
   // 添加附件
   BodyPart messageBodyPart = new MimeBodyPart();
   DataSource source = new FileDataSource(affix);
   // 添加附件的內容
   messageBodyPart.setDataHandler(new DataHandler(source));
   // 添加附件的標題
   // 這里很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼
   sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
   messageBodyPart.setFileName("=?GBK?B?"
     + enc.encode(affixName.getBytes()) + "?=");
   multipart.addBodyPart(messageBodyPart);
 
   // 將multipart對象放到message中
   message.setContent(multipart);
   // 保存郵件
   message.saveChanges();
   // 發送郵件
   Transport transport = session.getTransport("smtp");
   // 連接服務器的郵箱
   transport.connect(host, user, pwd);
   // 把郵件發送出去
   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 {
   // 打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
   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();
  // 設置發件人地址、收件人地址和郵件標題
  cn.setAddress("發件人地址", "收件人地址", "一個帶附件的JavaMail郵件(標題)");
  // 設置要發送附件的位置和標題
  cn.setAffix("附件的位置", "附件的文件名");
  // 設置smtp服務器以及郵箱的帳號和密碼
  cn.send("smtp.qq.com", "帳號", "密碼");
 
  if (file.exists()) {
   file.delete();
  }
 }
}

大概就是這樣子了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/xhw123xhw/p/5641316.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美free激情野战hd | www.av免费| 国产精品久久久久aaaa | 东京道一本热大交乱 | 亚洲狠狠婷婷综合久久久久网站 | 国产精品永久免费视频 | 欧美激情 亚洲 | 调教全程肉动画片在线观看 | 无遮掩60分钟从头啪到尾 | 暖暖免费高清完整版观看日本 | 日本最新伦中文字幕 | 四虎精品成人免费影视 | 国产成人性毛片aaww | 国产高清免费在线 | 美女脱了内裤打开腿让人桶网站o | 韩日理论片| 免费理伦片在线观看全网站 | 久久99re2在线视频精品 | 免费一级黄 | 欧美成人福利视频 | 99re在线视频观看 | 和直男装修工在工地啪 | 成年人视频免费在线播放 | 国产a一级毛片午夜剧院 | 国产亚洲人成网站在线观看不卡 | 久久黄色录像 | 色图大全 | 999久久久| 极品手交handjobtattoo | 欧美精品v日韩精品v国产精品 | 久久黄色免费 | 亚洲视频免费在线观看 | 91国语自产拍在线观看 | 日韩大片免费看 | 色噜噜亚洲男人的天堂www | 水多多凹凸福利视频导航 | 4hu四虎永久网址 | 91夜夜操| 亚洲国产精品无码中文在线 | 美女被的在线网站91 | 爽好舒服快想要免费看 |