java代码邮件回复 java发送邮件,内容换行-成都快上网建站

java代码邮件回复 java发送邮件,内容换行

java 实现发送会议邀请邮件。以及去读取人员回复情况,是接受还是拒绝。

你想直接有人把代码给你写好??

砚山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

没问题,可以把项目包出来嘛。2W块我帮你搞定

怎样用JavaMail实现邮件的自动回复和定时发送

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 //收件人,标题和文本内容

17 String to = "#######@126点抗 ";//填写你要发给谁

18 String title = createTitle();

19 String text = createText();

20 //设置属性

21 Properties props = new Properties();

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

23 props.put("mail.smtp.host", "smtp.qq点抗 ");

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

25 props.put("mail.smtp.socketFactory.class", "javax点虐 .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 //填写你的qq邮箱用户名和密码

32 return new PasswordAuthentication("*******@qq点抗 ", "###***%%%");

33 }

34 });

35 MimeMessage message = new MimeMessage(session);

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

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

38 int flag = 0;

39 {

40 try {

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

42 message.setFrom(new InternetAddress("*******@qq点抗 "));//填写你自己的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 //下面的两个方法,用来随机组合标题和文本内容。文本内容由四部分随机组合。

57 //产生标题

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 //产生文本内容,文本内容由四部分随机组合得到。

65 public String createText() {

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

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

68 String[] parts2 = {

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

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

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

72 };

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

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

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

76 };

77 String[] parts4 = {

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

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

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 }

复制代码

触发器的代码:

复制代码

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 //创建工作对象

14 JobDetail job = JobBuilder.newJob(MailJob.class)

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

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

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点多(不超过3分钟)执行

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

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

27 //创建触发器对象

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 //将触发器与工作关联起来

38 scheduler.scheduleJob(job, trigger);

39 }

40 }

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

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

set classpath=mylib/*;.;

javac CronTriggerExample.java

java CronTriggerExample

用java写一个邮件发送代码

public boolean mainto()

{

boolean flag = true;

//建立邮件会话

Properties pro = new Properties();

pro.put("mail.smtp.host","smtp.qq点抗 ");//存储发送邮件的服务器

pro.put("mail.smtp.auth","true"); //通过服务器验证

Session s =Session.getInstance(pro); //根据属性新建一个邮件会话

//s.setDebug(true);

//由邮件会话新建一个消息对象

MimeMessage message = new MimeMessage(s);

//设置邮件

InternetAddress fromAddr = null;

InternetAddress toAddr = null;

try

{

fromAddr = new InternetAddress(451144426+"@qq点抗 "); //邮件发送地址

message.setFrom(fromAddr); //设置发送地址

toAddr = new InternetAddress("12345367@qq点抗 "); //邮件接收地址

message.setRecipient(Message.RecipientType.TO, toAddr); //设置接收地址

message.setSubject(title); //设置邮件标题

message.setText(content); //设置邮件正文

message.setSentDate(new Date()); //设置邮件日期

message.saveChanges(); //保存邮件更改信息

Transport transport = s.getTransport("smtp");

transport.connect("smtp.qq点抗 ", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码

transport.sendMessage(message, message.getAllRecipients()); //发送邮件

transport.close();//关闭

}

catch (Exception e)

{

e.printStackTrace();

flag = false;//发送失败

}

return flag;

}

这是一个javaMail的邮件发送代码,需要一个mail.jar

javamail 回复邮件代码

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点抗 ";//smtp主机名。

private static String from = "XXXXXX@163点抗 ";//发送方邮件地址

private static String pw = "XXXXXX";//发送方邮件密码。

private static String to = "XXXXXX@163点抗 "; //接收方邮件地址

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);//填写用户名及密码

sendMailSession = Session.getInstance(props,new Authenticator(){

public PasswordAuthentication getPasswordAuthentication() //邮件安全认证。

{

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);

}

}*/

}


网站标题:java代码邮件回复 java发送邮件,内容换行
文章源于:http://kswjz.com/article/ddseedj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流