Email发送邮件使用ical4j将时间同步日历中
1.Maven依赖
<!--邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--ical4j-->
<dependency>
<groupId>org.mnode.ical4j</groupId>
<artifactId>ical4j</artifactId>
<version>1.0.7</version>
</dependency>
2.认证
/**
* 邮箱认证
* @return
*/
private static JavaMailSenderImpl authEmail(){
JavaMailSenderImpl javaMailSender=new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setPort(port);
javaMailSender.setUsername(email);
javaMailSender.setPassword(pwd);
//认证机制
Properties properties=new Properties();
//连接超时
properties.setProperty("mail.smtp.connectiontimeout","10000");
//开启认证
properties.setProperty("mail.smtp.auth","true");
//启动调试
properties.setProperty("mail.debug","true");
//设置端口
properties.setProperty("mail.smtp.port",Integer.toString(465));
//加密方式
System.out.println("启用加密方式");
properties.setProperty("mail.smtp.starttls.enable","true");
properties.setProperty("mail.smtp.starttls.required","true");
//是否设置ssl端口
System.out.println("启用ssl");
properties.setProperty("mail.smtp.socketFactory.port",prot);
properties.setProperty("mail.smtp.socketFactory.fallback","false");
properties.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.enable","true");
javaMailSender.setJavaMailProperties(properties);
return javaMailSender;
}
/**
* 测试连接
* @param args
*/
public static void main(String[] args) {
JavaMailSenderImpl javaMailSender = authEmail();
try {
javaMailSender.testConnection();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("成功");
}
3.发送
private void sendEmail() throws MessagingException, IOException, ValidationException, URISyntaxException {
JavaMailSenderImpl javaMailSender = authEmail();
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
//发送人邮箱
messageHelper.setFrom("sendEmail");
//接收者邮箱
String[] tos={"invit2","invit2"};
messageHelper.setTo(tos);
//标题
messageHelper.setSubject("信息通知:");
//内容
String content = content();
MimeMultipart multipart=new MimeMultipart();
MimeBodyPart bodyPart=new MimeBodyPart();
bodyPart.setContent(content,"text/html;charset=UTF-8");
//bodyPart.setText("会议在15分钟后");
multipart.addBodyPart(bodyPart);
//是否加入日历
if (true){
String calendar = getCalendar();
BodyPart bodyPartCal=new MimeBodyPart();
bodyPartCal.setDataHandler(new DataHandler(
new ByteArrayDataSource(calendar,"text/calendar;method=REQUEST;charset=\"UTF-8\"")));
multipart.addBodyPart(bodyPartCal);
}
message.setContent(multipart);
message.setSentDate(new Date());
//保存
message.saveChanges();
//发送
javaMailSender.send(message);
}
3.日历-创建日程
/**
* 邮箱添加到日历
* @return
*/
private String getCalendar() throws SocketException, ValidationException, URISyntaxException {
//设置时区
TimeZoneRegistry zoneRegistry = TimeZoneRegistryFactory.getInstance().createRegistry();
net.fortuna.ical4j.model.TimeZone timeZone = zoneRegistry.getTimeZone("Asia/Shanghai");
VTimeZone tz = timeZone.getVTimeZone();
//创建日历
net.fortuna.ical4j.model.Calendar calendar=new net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(Version.VERSION_2_0);
// -//Events Calendar//iCal4j 1.0//EN
calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
calendar.getProperties().add(CalScale.GREGORIAN);
calendar.getProperties().add(Method.REQUEST);
//开始时间 接收时间
LocalDateTime currDate = LocalDateTime.now().plusDays(1L).with(LocalTime.MIN);
LocalDateTime start = currDate.with(LocalTime.parse("11:00"));
LocalDateTime end = currDate.with(LocalTime.parse("12:15"));
DateTime startTime =new DateTime(start.toInstant(ZonedDateTime.now().getOffset()).toEpochMilli());
startTime.setTimeZone(timeZone);
DateTime endTime =new DateTime(end.toInstant(ZonedDateTime.now().getOffset()).toEpochMilli());
endTime.setTimeZone(timeZone);
//创建事件
VEvent vEvent=new VEvent(startTime,endTime,"信息通知");
//地址
vEvent.getProperties().add(new Location("测试园区-F4"));
//唯一标识
vEvent.getProperties().add(new Uid(new UidGenerator("iCal4j").generateUid().getValue()));
//时区
vEvent.getProperties().add(tz.getTimeZoneId());
//组织者
Organizer organizer=new Organizer("mailto:email");
organizer.getParameters().add(new Cn("Booked or person"));
vEvent.getProperties().add(organizer);
//邀请者
String[] tos={};
for (int i = 0; i < tos.length; i++) {
Attendee dev = new Attendee(URI.create("mailto:"+tos[i]));
dev.getParameters().add(Role.REQ_PARTICIPANT);
dev.getParameters().add(new Cn("Developer"+i));
vEvent.getProperties().add(dev);
}
// 重复事件
// Recur recur = new Recur(Recur.WEEKLY, Integer.MAX_VALUE);
// recur.getDayList().add(WeekDay.MO);
// recur.getDayList().add(WeekDay.TU);
// recur.getDayList().add(WeekDay.WE);
// recur.getDayList().add(WeekDay.TH);
// recur.getDayList().add(WeekDay.FR);
// RRule rule = new RRule(recur);
// vEvent.getProperties().add(rule);
// 提醒,提前10分钟
VAlarm valarm = new VAlarm(new Dur(0, 0, -10, 0));
valarm.getProperties().add(new Summary("预订信息"));
valarm.getProperties().add(Action.DISPLAY);
valarm.getProperties().add(new Description("会议信息预订一个计划"));
// 将VAlarm加入VEvent
vEvent.getAlarms().add(valarm);
// 添加事件
calendar.getComponents().add(vEvent);
// 验证
calendar.validate();
return calendar.toString();
}