你想直接有人把代碼給你寫(xiě)好??
硯山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話(huà)聯(lián)系或者加微信:13518219792(備注:SSL證書(shū)合作)期待與您的合作!
沒(méi)問(wèn)題,可以把項(xiàng)目包出來(lái)嘛。2W塊我?guī)湍愀愣?/p>
1 import org.quartz.Job;
2 import org.quartz.JobExecutionContext;
3 import org.quartz.JobExecutionException;
4 import java.util.Calendar;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.lang.InterruptedException;
8 import java.util.Random;
9 import java.util.Properties;
10 import javax.mail.*;
11 import javax.mail.internet.*;
12 public class MailJob implements Job
13 {
14 public void execute(JobExecutionContext context)
15 throws JobExecutionException {
16 //收件人,標(biāo)題和文本內(nèi)容
17 String to = "#######@126點(diǎn)抗 ";//填寫(xiě)你要發(fā)給誰(shuí)
18 String title = createTitle();
19 String text = createText();
20 //設(shè)置屬性
21 Properties props = new Properties();
22 //QQ郵箱發(fā)件的服務(wù)器和端口
23 props.put("mail.smtp.host", "smtp.qq點(diǎn)抗 ");
24 props.put("mail.smtp.socketFactory.port", "465");
25 props.put("mail.smtp.socketFactory.class", "javax點(diǎn)虐 .ssl.SSLSocketFactory");
26 props.put("mail.smtp.auth", "true");
27 props.put("mail.smtp.port", "25");
28 Session session = Session.getDefaultInstance(props,
29 new javax.mail.Authenticator() {
30 protected PasswordAuthentication getPasswordAuthentication() {
31 //填寫(xiě)你的qq郵箱用戶(hù)名和密碼
32 return new PasswordAuthentication("*******@qq點(diǎn)抗 ", "###***%%%");
33 }
34 });
35 MimeMessage message = new MimeMessage(session);
36 //這里用flag來(lái)標(biāo)記是否發(fā)件成功(有時(shí)候會(huì)連不上服務(wù)器),
37 //如果沒(méi)有,繼續(xù)發(fā)送,直到發(fā)送成功為止。
38 int flag = 0;
39 {
40 try {
41 //設(shè)置發(fā)件人,收件人,主題和文本內(nèi)容,并發(fā)送
42 message.setFrom(new InternetAddress("*******@qq點(diǎn)抗 "));//填寫(xiě)你自己的qq郵箱,和上面相同
43 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
44 message.setSubject(title);
45 message.setText(text);
46 System.out.println("Preparing sending mail...");
47 System.out.println(text);
48 Transport.send(message);
49 flag = 1;
50 System.out.println("message sent successfully");
51 } catch(Exception e) {
52 flag = 0;
53 }
54 } while(flag == 0);
55 }
56 //下面的兩個(gè)方法,用來(lái)隨機(jī)組合標(biāo)題和文本內(nèi)容。文本內(nèi)容由四部分隨機(jī)組合。
57 //產(chǎn)生標(biāo)題
58 public String createTitle() {
59 String[] titles = {"love", "I love you", "Miss you", "My baby"};
60 Random randT = new Random(System.currentTimeMillis());
61 String title = titles[randT.nextInt(titles.length)];
62 return title;
63 }
64 //產(chǎn)生文本內(nèi)容,文本內(nèi)容由四部分隨機(jī)組合得到。
65 public String createText() {
66 //名字純屬虛構(gòu),如有雷同(肯定會(huì)有),純屬巧合。
67 String[] parts1 = {"曉靜,你好。", "曉靜,你還好嗎?", "曉靜,你那邊天氣怎么樣?"};
68 String[] parts2 = {
69 "距離上次見(jiàn)面,我感覺(jué)已經(jīng)好長(zhǎng)時(shí)間了。",
70 "流去的時(shí)間磨不去我對(duì)你的愛(ài)。",
71 "我仍然記得你在天安門(mén)前的那一抹笑容。"
72 };
73 String[] parts3 = {"今天,我大膽地追求你。",
74 "我不怕大膽地對(duì)你說(shuō),我愛(ài)你。",
75 "此刻,月亮代表我的心。"
76 };
77 String[] parts4 = {
78 "未來(lái),我的心依舊屬于你。",
79 "好想在未來(lái)陪你一起慢慢變老,當(dāng)然在我心中你不會(huì)老。"
80 };
81 Random randT = new Random(System.currentTimeMillis());
82 String text = parts1[randT.nextInt(parts1.length)]
83 + parts2[randT.nextInt(parts2.length)]
84 + parts3[randT.nextInt(parts3.length)]
85 + parts4[randT.nextInt(parts4.length)];
86 return text;
87 }
88
89 }
復(fù)制代碼
觸發(fā)器的代碼:
復(fù)制代碼
1 import org.quartz.CronScheduleBuilder;
2 import org.quartz.JobBuilder;
3 import org.quartz.JobDetail;
4 import org.quartz.Scheduler;
5 import org.quartz.Trigger;
6 import org.quartz.TriggerBuilder;
7 import org.quartz.impl.StdSchedulerFactory;
8 import java.util.Random;
9 public class CronTriggerExample
10 {
11 public static void main( String[] args ) throws Exception
12 {
13 //創(chuàng)建工作對(duì)象
14 JobDetail job = JobBuilder.newJob(MailJob.class)
15 .withIdentity("dummyJobName", "group1").build();
16 //為了立即測(cè)試,可以使用下面的代碼,每隔5秒鐘執(zhí)行一次
17 //int secDelta = 5;
18 //Trigger trigger = TriggerBuilder
19 // .newTrigger()
20 // .withIdentity("dummyTriggerName", "group1")
21 // .withSchedule(
22 // CronScheduleBuilder.cronSchedule("0/" + secDelta + " * * * * ?"))
23 // .build();
24 //在每天早上的9點(diǎn)多(不超過(guò)3分鐘)執(zhí)行
25 Random rand = new Random(System.currentTimeMillis());
26 int secDelta = rand.nextInt(60 * 3);
27 //創(chuàng)建觸發(fā)器對(duì)象
28 Trigger trigger = TriggerBuilder
29 .newTrigger()
30 .withIdentity("dummyTriggerName", "group1")
31 .withSchedule(
32 CronScheduleBuilder.cronSchedule(secDelta + " 0 9 ? * SUN-SAT"))
33 .build();
34
35 Scheduler scheduler = new StdSchedulerFactory().getScheduler();
36 scheduler.start();
37 //將觸發(fā)器與工作關(guān)聯(lián)起來(lái)
38 scheduler.scheduleJob(job, trigger);
39 }
40 }
發(fā)郵件依賴(lài)的包:activation.jar,mail.jar
將發(fā)郵件依賴(lài)的包和quartz下載得到的lib路徑下的jar包全部放在mylib路徑下,mylib路徑與java文件位于同一個(gè)目錄。編譯和運(yùn)行時(shí),可以使用命令:
set classpath=mylib/*;.;
javac CronTriggerExample.java
java CronTriggerExample
public boolean mainto()
{
boolean flag = true;
//建立郵件會(huì)話(huà)
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq點(diǎn)抗 ");//存儲(chǔ)發(fā)送郵件的服務(wù)器
pro.put("mail.smtp.auth","true"); //通過(guò)服務(wù)器驗(yàn)證
Session s =Session.getInstance(pro); //根據(jù)屬性新建一個(gè)郵件會(huì)話(huà)
//s.setDebug(true);
//由郵件會(huì)話(huà)新建一個(gè)消息對(duì)象
MimeMessage message = new MimeMessage(s);
//設(shè)置郵件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
try
{
fromAddr = new InternetAddress(451144426+"@qq點(diǎn)抗 "); //郵件發(fā)送地址
message.setFrom(fromAddr); //設(shè)置發(fā)送地址
toAddr = new InternetAddress("12345367@qq點(diǎn)抗 "); //郵件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //設(shè)置接收地址
message.setSubject(title); //設(shè)置郵件標(biāo)題
message.setText(content); //設(shè)置郵件正文
message.setSentDate(new Date()); //設(shè)置郵件日期
message.saveChanges(); //保存郵件更改信息
Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq點(diǎn)抗 ", "451144426", "密碼"); //服務(wù)器地址,郵箱賬號(hào),郵箱密碼
transport.sendMessage(message, message.getAllRecipients()); //發(fā)送郵件
transport.close();//關(guān)閉
}
catch (Exception e)
{
e.printStackTrace();
flag = false;//發(fā)送失敗
}
return flag;
}
這是一個(gè)javaMail的郵件發(fā)送代碼,需要一個(gè)mail.jar
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
* pTitle: java mail/p
* pDescription: /p
* pCopyright: Copyright (c) 2004/p
* pCompany: /p
* @author petehero
* @version 1.0
*/
class SendMail
{
/**
* Logger for this class
*/
private static String server = "smtp.163點(diǎn)抗 ";//smtp主機(jī)名。
private static String from = "XXXXXX@163點(diǎn)抗 ";//發(fā)送方郵件地址
private static String pw = "XXXXXX";//發(fā)送方郵件密碼。
private static String to = "XXXXXX@163點(diǎn)抗 "; //接收方郵件地址
static String subject="hello";
/*BodyPart bp = new MimeBodyPart();
Multipart mp = new MimeMultipart();
DataSource source = new FileDataSource("a.jpg");//附件
*/
static String mailContent="你好!\n\n"+"\t郵件正文test\n\n";//郵件正文
public static void send()
{
try
{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",server);
props.put("mail.smtp.user",from);
props.put("mail.smtp.password",pw);
//PopupAuthenticator popA=new PopupAuthenticator();
//PasswordAuthentication pop = popA.performCheck(from, pw);
//sendMailSession = Session.getInstance(props,popA);//填寫(xiě)用戶(hù)名及密碼
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() //郵件安全認(rèn)證。
{
return new PasswordAuthentication(from, pw);
}
});
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
/*
bp.setText("bptext");
mp.addBodyPart(bp);
bp = new MimeBodyPart();
bp.setDataHandler(new DataHandler(source));
bp.setFileName("141.txt");
mp.addBodyPart(bp);
newMessage.setContent(mp);
*/
mailContent+=new Date().toLocaleString();
newMessage.setText(mailContent);
transport = sendMailSession.getTransport("smtp");
//System.out.println("Sending...");
//transport
Transport.send(newMessage);
//Transport.send(newMessage );
//System.out.println(server+from+pw+to);
}
catch (MessagingException ex)
{
}
}
// public static void main(String[] args)
// {
//
//// for(int i=0;iargs.length;i++)
//// {
//// System.out.println(args[i]);
//// }
//
// from=args[0].trim();
////
// pw=args[1].trim();
////
// SendMail sml = new SendMail();
// sml.send();
//// String a=SendMail.mailSend("hello","world");
//// System.out.println(a);
// }
static String mailSend(String t_to,String t_subject,String t_text)
{ String state="";
to=t_to;
subject=t_subject;
mailContent=t_text+"\n\n";
SendMail sml = new SendMail();
sml.send();
state="Successful";
return state;
}
/* public class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}*/
}