Java 发送会议邀请到 Outlook
Java 发送会议邀请到 Outlook
系统:Win10
IDE:IntelliJ IDEA 2017.3.7
JDK:1.8.0_121
Outlook:Microsoft Office 2016
1.发件服务器配置
服务器名称:SMTP
服务器地址:smtp.126.com
SSL协议端口:465
2.发送邮件代码
需要导入mail.jar包,点击下载
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
public class ToExchangeMail {
protected void sendEmail() {
try {
Properties props = new Properties();
try {
// 使用SSL协议发送邮件
// smtp的相关配置
props.put("mail.smtp.host", "smtp.126.com"); // smtp地址
props.put("mail.smtp.port", "465"); // smtp端口号
props.put("mail.transport.protocol", "smtps"); // 邮件端口名
props.put("mail.smtp.auth", "true"); // 是否需要身份验证
props.put("mail.smtp.ssl.enable", "true"); // SSL加密
} catch (Exception e) {
e.printStackTrace();
}
String fromEmail = "***@126.com"; // 发送邮箱
String toEmail = "***@test.com"; // 接收邮箱
Session session;
final String username = "***";//发送人的邮箱用户名(这里一定要是邮箱的用户名,不是xxx@126.com)
final String password = "***";//发送人的邮箱密码
class EmailAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
Authenticator authenticator = new EmailAuthenticator();
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("Test标题"); // 会议标题
StringBuffer buffer = new StringBuffer();
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); // 数据库的时间格式
DateFormat format2 = new SimpleDateFormat("yyyyMMdd:HHmmss"); // 发送到outlook的时间格式
String beginTime = format2.format(format1.parse("2021-04-16 16:00")); // 会议开始时间
beginTime = beginTime.replace(":", "T");
String endTime = format2.format(format1.parse("2021-04-16 16:30")); // 会议结束时间
endTime = endTime.replace(":", "T");
String uuid = UUID.randomUUID().toString(); // 如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。 如果传同一ID不同内容的会议会修改上一个会议
System.out.println("UUID:" + uuid);
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:"+toEmail+"\n"
+ "ORGANIZER:MAILTO:"+toEmail+"\n"
+ "DTSTART:"
+ beginTime+"\n"
+ "DTEND:"
+ endTime+"\n"
+ "UID:"+uuid+"\n"
+ "CATEGORIES:"+"邮件类别"+"\n" // 类别
+ "DESCRIPTION:"+"会议地点"+"\n\n" // 地点
+ "SUMMARY:Test meeting request\n" + "PRIORITY:5\n"
+ "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
+ "TRIGGER:-PT15M\n" + "ACTION:DISPLAY\n"
+ "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR");
BodyPart messageBodyPart = new MimeBodyPart();
// 测试下来如果不这么转换的话,会以纯文本的形式发送过去,
// 如果没有method=REQUEST;charset=\"UTF-8\",outlook会议附件的形式存在,而不是直接打开就是一个会议请求
messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(buffer.toString(),
"text/calendar;method=REQUEST;charset=\"UTF-8\"")));
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("发送成功");
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.测试代码
在同级包下创建测试类,运行
public class SendMailMain {
public static void main(String[] args) {
ToExchangeMail mail = new ToExchangeMail();
mail.sendEmail();
}
}
运行结果
进入Outlook可以看到该条会议邀请已经发送成功