搜档网
当前位置:搜档网 › 【黑马程序员】追女神神器,Java实现QQ邮箱每天定时发送邮件,学会Java可以做什么?

【黑马程序员】追女神神器,Java实现QQ邮箱每天定时发送邮件,学会Java可以做什么?

【黑马程序员】追女神神器,Java实现QQ邮箱每天定时发送

邮件,学会Java可以做什么?

任务类的代码:

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import java.util.Calendar;

import java.text.SimpleDateFormat;

import java.util.Date;

import https://www.sodocs.net/doc/e22028011.html,ng.InterruptedException;

import java.util.Random;

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.*;

public class MailJob implements Job

{

public void execute(JobExecutionContext context)

throws JobExecutionException {

//收件人,标题和文本内容

String to = "#######@https://www.sodocs.net/doc/e22028011.html,";//填写你要发给谁

String title = createTitle();

String text = createText();

//设置属性

Properties props = new Properties();

//QQ邮箱发件的服务器和端口

props.put("mail.smtp.host", "https://www.sodocs.net/doc/e22028011.html,");

props.put("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.socketFactory.class", "https://www.sodocs.net/doc/e22028011.html,.ssl.SSLSocketFactory");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.port", "25");

Session session = Session.getDefaultInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

//填写你的qq邮箱用户名和密码

return new PasswordAuthentication("*******@https://www.sodocs.net/doc/e22028011.html,", "###***%%%");

}

});

MimeMessage message = new MimeMessage(session);

//这里用flag来标记是否发件成功(有时候会连不上服务器),

//如果没有,继续发送,直到发送成功为止。

int flag = 0;

{

try {

//设置发件人,收件人,主题和文本内容,并发送

message.setFrom(new InternetAddress("*******@https://www.sodocs.net/doc/e22028011.html,"));//填写你自己的qq邮箱,和上面相同

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject(title);

message.setText(text);

System.out.println("Preparing sending mail...");

System.out.println(text);

Transport.send(message);

flag = 1;

System.out.println("message sent successfully");

} catch(Exception e) {

flag = 0;

}

} while(flag == 0);

}

//下面的两个方法,用来随机组合标题和文本内容。文本内容由四部分随机组合。

//产生标题

public String createTitle() {

String[] titles = {"love", "I love you", "Miss you", "My baby"};

Random randT = new Random(System.currentTimeMillis());

String title = titles[randT.nextInt(titles.length)];

return title;

}

//产生文本内容,文本内容由四部分随机组合得到。

public String createText() {

//名字纯属虚构,如有雷同(肯定会有),纯属巧合。

String[] parts1 = {"晓静,你好。", "晓静,你还好吗?", "晓静,你那边天气怎么样?"};

String[] parts2 = {

"距离上次见面,我感觉已经好长时间了。",

"流去的时间磨不去我对你的爱。",

"我仍然记得你在天安门前的那一抹笑容。"

};

String[] parts3 = {"今天,我大胆地追求你。",

"我不怕大胆地对你说,我爱你。",

"此刻,月亮代表我的心。"

};

String[] parts4 = {

"未来,我的心依旧属于你。",

"好想在未来陪你一起慢慢变老,当然在我心中你不会老。"

};

Random randT = new Random(System.currentTimeMillis());

String text = parts1[randT.nextInt(parts1.length)]

+ parts2[randT.nextInt(parts2.length)]

+ parts3[randT.nextInt(parts3.length)]

+ parts4[randT.nextInt(parts4.length)];

return text;

}

}

触发器的代码:

import org.quartz.CronScheduleBuilder;

import org.quartz.JobBuilder;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.Trigger;

import org.quartz.TriggerBuilder;

import org.quartz.impl.StdSchedulerFactory;

import java.util.Random;

public class CronTriggerExample

{

public static void main( String[] args ) throws Exception

{

//创建工作对象

JobDetail job = JobBuilder.newJob(MailJob.class)

.withIdentity("dummyJobName", "group1").build();

//为了立即测试,可以使用下面的代码,每隔5秒钟执行一次

//int secDelta = 5;

//Trigger trigger = TriggerBuilder

// .newTrigger()

// .withIdentity("dummyTriggerName", "group1")

// .withSchedule(

// CronScheduleBuilder.cronSchedule("0/" + secDelta + " * * * * ?"))

// .build();

//在每天早上的9点多(不超过3分钟)执行

Random rand = new Random(System.currentTimeMillis());

int secDelta = rand.nextInt(60 * 3);

//创建触发器对象

Trigger trigger = TriggerBuilder

.newTrigger()

.withIdentity("dummyTriggerName", "group1")

.withSchedule(

CronScheduleBuilder.cronSchedule(secDelta + " 0 9 ? * SUN-SAT")) .build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

scheduler.start();

//将触发器与工作关联起来

scheduler.scheduleJob(job, trigger);

}

}

发邮件依赖的包:activation.jar,mail.jar

quartz下载地址:https://www.sodocs.net/doc/e22028011.html,/downloads/

将发邮件依赖的包和quartz下载得到的lib路径下的jar包全部放在mylib路径下,mylib 路径与java文件位于同一个目录。编译和运行时,可以使用命令:

set classpath=mylib/*;.;

javac CronTriggerExample.java

java CronTriggerExample

相关主题