1.使用java发送 日程 会议邮件 可以达到安排别人日程预约的目的,代码如下。这个网上教程和多,但是如果已经发送了日程,又需要使用代码进行修改怎么办呢?
2.会议的修改和取消: 首先,发送安排时需要记录生成的UUID,这个是邮件识别日程的关键ID,需要修改的话就再次发送同样ID的邮件就可以了。
3,取消:把邮件里日程部分的METHOD 改为取消 CANCEL 就可以了。
需要注意的地方:日程格式要完全正确,空格都不能有。
附上代码和POM文件
import javax.activation.DataHandler;
import javax.mail.*;
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;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;
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", "25");
props.put("mail.smtp.host", "smtp.126.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.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("24点012会议");
StringBuffer buffer = new StringBuffer();
String uuid=UUID.randomUUID().toString();
uuid="cf2ff464-71ea-4246-a1ec-bacfbdd4c90e";
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"
//+ "METHOD:CANCEL\n"
+ "BEGIN:VEVENT\n"
+ "SEQUENCE:0\n"
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"+toEmail+"\n"
+ "ORGANIZER:MAILTO:"+toEmail+"\n"
+ "DTSTAMP:20181224T085000Z\n"
+ "DTSTART:20181224T085000Z\n"
+ "DTEND:20181224T085500Z\n"
+ "LOCATION:Conference room\n"
+ "UID:"+ uuid+"\n"//如果id相同的话,outlook会认为是同一个会议请求,所以使用uuid。
+ "CATEGORIES:SuccessCentral Reminder\n"
+ "DESCRIPTION:This the description of the meeting\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();
} }
}
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mailSender</groupId>
<artifactId>mailSender</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mnode.ical4j/ical4j -->
<dependency>
<groupId>org.mnode.ical4j</groupId>
<artifactId>ical4j</artifactId>
<version>1.0.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
<groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId>
<version>1.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/backport-util-concurrent/backport-util-concurrent -->
<dependency>
<groupId>backport-util-concurrent</groupId>
<artifactId>backport-util-concurrent</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>