网上找了很多关于javamail发送会议请求的文章,终于成功了,没有使用ical4j,发送的outlook直接打开就是一个会议请求,而不是以附件的形式存在。具体的注意点在代码中以注释的形式标明。
该方法只能给outlook发送,foxmail不行,有待研究!
public class Email {
private static String EMAIL_CONFIG = "email_config.properties";
private Properties emailProp = new Properties();
public Email() {
InputStream is = getClass().getResourceAsStream("/"+EMAIL_CONFIG);
try {
emailProp.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class EmailAuthenticator extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
String userId = emailProp.getProperty("userId", "");
String password = emailProp.getProperty("password", "");
return new PasswordAuthentication(userId, password);
}
}
public void send() throws Exception {
try {
String fromEmail = emailProp.getProperty("fromEmail", "");
String toEmail=emailProp.getProperty("toEmail", "");
Properties props = new Properties();
try {
props.put("mail.smtp.port", "587");
props.put("mail.smtp.host", "smtp.gmail.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");
} catch (Exception e) {
e.printStackTrace();
}
Session session;
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("Outlook Meeting Request Using JavaMail");
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:"+toEmail+"\n"
+ "ORGANIZER:MAILTO:"+toEmail+"\n"
+ "DTSTART:20120302T060000Z\n"
+ "DTEND:20120302T070000Z\n"
+ "LOCATION:Conference room\n"
+ "UID:"+UUID.randomUUID().toString()+"\n"//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
+ "CATEGORIES:SuccessCentral Reminder\n"
+ "DESCRIPTION:This the description of the meeting.<br>asd;flkjasdpfi\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);
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
try {
Email email = new Email();
email.send();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
}