public class Test1 {
public static void main(String[] args) {
sendMeetingInvitationEmail();
}
private static Properties props;
private static Session session;
public static void sendMeetingInvitationEmail() {
try {
props = new Properties();
//发件人
String fromEmail = props.getProperty("fromEmail", "XXX@outlook.com");
//收件人(面试官)
String toEmail = props.getProperty("toEmail", "XXX@outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.host", "smtp.office365.com");
//当前smtp host设为可信任 否则抛出javax.mail.MessagingException: Could not convert socket to TLS
props.put("mail.smtp.ssl.trust", "smtp.office365.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl", "true");
//开启debug调试,控制台会打印相关信息
props.put("mail.debug", "true");
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱账号
String userId = props.getProperty("userId", "XXX@outlook.com");
//发件人邮箱密码(qq、163等邮箱用的是授权码,outlook是密码)
String password = props.getProperty("password", "XXXXXXXX");
return new PasswordAuthentication(userId, password);
}
};
session = Session.getInstance(props, authenticator);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
//标题
message.setSubject("XXX公司诚邀应聘");
//面试开始时间
String startTime = getUtc("2019-09-04 14:00");
//面试结束时间
String endTime = getUtc("2019-09-04 15:00");
StringBuffer buffer = new StringBuffer();
buffer.append("BEGIN:VCALENDAR\n"
+ "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n"
+ "VERSION:2.0\n"
+ "METHOD:REQUEST\n"
+ "BEGIN:VEVENT\n"
//参会者
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:你和应聘者\n"
//组织者
//+ "ORGANIZER:MAILTO:张三\n"
+ "DTSTART:" + startTime + "\n"
+ "DTEND:" + endTime + "\n"
//面试地点
+ "LOCATION:会议室01\n"
//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
+ "UID:" + UUID.randomUUID().toString() + "\n"
+ "CATEGORIES:\n"
//会议描述
//+ "DESCRIPTION:Stay Hungry.<br>Stay Foolish.\n\n"
+ "SUMMARY:面试邀请\n" + "PRIORITY:5\n"
+ "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
//提前10分钟提醒
+ "TRIGGER:-PT10M\n" + "ACTION:DISPLAY\n"
+ "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(buffer.toString(),
"text/calendar;method=REQUEST;charset=\"UTF-8\"")));
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
//String emailText = getHtmlContent(sendEmailApi.getTemplateContent(tempValue),tempMap);
//文本类型正文
mimeBodyPart.setText("尊敬的张三:\r您好!\r特邀您...");
//html类型正文
//mimeBodyPart.setContent(emailText,"text/html;charset=UTF-8");
//添加正文
multipart.addBodyPart(mimeBodyPart);
//添加日历
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport.send(message);
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 转utc时间
*
* @param str
* @return
*/
private static String getUtc(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
long millionSeconds = 0;
try {
millionSeconds = sdf.parse(str).getTime();
} catch (ParseException e1) {
e1.printStackTrace();
}
//utc时间差8小时
long currentTime = millionSeconds - 8 * 60 * 60 * 1000;
Date date = new Date(currentTime);
//格式化日期
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = "";
nowTime = df.format(date);
//转换utc时间
String utcTime = nowTime.replace("-", "").replace(" ", "T").replace(":", "");
return utcTime;
}
}
Java 发送邮件添加日历提醒,添加附件,添加文本
最新推荐文章于 2024-09-01 12:02:11 发布