关于 java发送 邮件会议 预约日程 的新建、修改、和取消

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("caizy@xiaopeng.com"));
            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>

 

代码下载地址:https://download.csdn.net/download/guodashuai123/10868683

 

参考:链接https://stackoverflow.com/questions/24365763/java-code-to-reschedule-a-outlook-calendar-event

https://www.ibm.com/developerworks/cn/java/j-lo-ical4j/

https://whoosh.iteye.com/blog/1436567

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值