首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Java Mail发送iCal会议请求并接收响应

如何使用Java Mail发送iCal会议请求并接收响应
EN

Stack Overflow用户
提问于 2009-07-14 03:58:11
回答 3查看 38.1K关注 0票数 11

我正在尝试使用Java Mail Library将iCal发送到outlook,我已经阅读了Question,并且我已经有了一些示例代码

public class SendMeetingRequest {

String host = "" ;
String port = "" ;
String sender = "" ;

public static SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd'T'HHmm'00'" ) ;
public static SimpleDateFormat dateParser = new SimpleDateFormat( "dd-MM-yyyy HH:mm" ) ;

public static void main(String[] args) throws Exception {
    SendMeetingRequest sender = new SendMeetingRequest() ;

    sender.sendInvitation( “LogicaCMG Inschrijf Site”
                         , new String[] { “robert<dot>willems<dot>of<dot>brilman<at>logicacmg<dot>com”
                                        }
                         , “Outlook Meeting Request Using JavaMail”
                         , dateParser.parse( “28-08-2006 18:00″ )
                         , dateParser.parse( “28-08-2006 21:00″ )
                         , “LIS-42″
                         , “Bar op scheveningen”
                         , “<font color=\”Red\”>Aanwezigheid verplicht!</font><br>We gaan lekker een biertje drinken met z’n allen.”
                         ) ;
}


void sendInvitation( String organizer
                   , String[] to
                   , String subject
                   , Date start
                   , Date end
                   , String invitationId
                   , String location
                   , String description
                   ) throws Exception {

    try {
        Properties prop = new Properties();
        prop.put(”mail.smtp.port”, port );
        prop.put(”mail.smtp.host”, host );

        Session session = Session.getDefaultInstance(prop);
        session.setDebug(true);

        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender));

        // Set TO
        if( to != null && ( to.length > 0 ) ) {
            InternetAddress[] address = new InternetAddress[ to.length ] ;

            for( int i = 0; i < to.length; i++ ) {
                address[ i ] = new InternetAddress( to[ i ] ) ;
            }

            message.setRecipients( Message.RecipientType.TO, address ) ;
        }

        // Set subject
        message.setSubject(subject);

        // Create iCalendar message
        StringBuffer messageText = new StringBuffer();
        messageText.append("BEGIN:VCALENDAR\n" +
                           "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n" +
                           "VERSION:2.0\n" +
                           "METHOD:REQUEST\n" +
                               "BEGIN:VEVENT\n" +
                               "ORGANIZER:MAILTO:" ) ;
        messageText.append( organizer ) ;
        messageText.append( "\n" +
                            "DTSTART:");
        messageText.append( dateFormat.format( start ) ) ;
        messageText.append( "\n" +
                            "DTEND:" ) ;
        messageText.append( dateFormat.format( end ) ) ;
        messageText.append( "\n" +
                            "LOCATION:" ) ;
        messageText.append( location ) ;
        messageText.append( "\n" +
                             "UID:" ) ;
        messageText.append( invitationId ) ;
        messageText.append( "\n" +
                            "DTSTAMP:" ) ;
        messageText.append( dateFormat.format( new java.util.Date() ) ) ;
        messageText.append( "\n" +
                            "DESCRIPTION;ALTREP=\"CID:<eventDescriptionHTML>\”" ) ;
        messageText.append( “\n” +
                                    ”BEGIN:VALARM\n” +
                                    ”TRIGGER:-PT15M\n” +
                                    ”ACTION:DISPLAY\n” +
                                    ”DESCRIPTION:Reminder\n” +
                                    ”END:VALARM\n” +
                               ”END:VEVENT\n” +
                           ”END:VCALENDAR”
                           ) ;

        Multipart mp = new MimeMultipart();

        MimeBodyPart meetingPart = new MimeBodyPart() ;
        meetingPart.setDataHandler( new DataHandler( new StringDataSource( messageText.toString(), “text/calendar”, “meetingRequest” ) ) ) ;
        mp.addBodyPart( meetingPart ) ;

        MimeBodyPart descriptionPart = new MimeBodyPart() ;
        descriptionPart.setDataHandler( new DataHandler( new StringDataSource( description, “text/html”, “eventDescription” ) ) ) ;
        descriptionPart.setContentID( “<eventDescriptionHTML>” ) ;
        mp.addBodyPart( descriptionPart ) ;

        message.setContent( mp ) ;

        // send message
        Transport.send(message);

    } catch (MessagingException me) {
        me.printStackTrace();

    } catch (Exception ex) {
        ex.printStackTrace();

    }
}

private static class StringDataSource implements DataSource {
    private String contents ;
    private String mimetype ;
    private String name ;


    public StringDataSource( String contents
                           , String mimetype
                           , String name
                           ) {
        this.contents = contents ;
        this.mimetype = mimetype ;
        this.name = name ;
    }

    public String getContentType() {
        return( mimetype ) ;
    }

    public String getName() {
        return( name ) ;
    }

    public InputStream getInputStream() {
        return( new StringBufferInputStream( contents ) ) ;
    }

    public OutputStream getOutputStream() {
        throw new IllegalAccessError( “This datasource cannot be written to” ) ;
    }
} }

但它是作为outlook 2007和outlook 2003的附件发送的,即使我单击该附件进行查看和接受,我也收不到答案,这就是应用程序具有类似于outlook的功能的目的。

有没有我需要知道的程序,或者是邀请函ID让事情变得艰难?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-07-15 08:52:38

所以我解决了这个问题,这是我发现的:

1-您必须更新到Java Mail API 1.4.2才能使一切正常工作

2-编写邮件的文本/日历部分,如下所示:

package com.xx.xx;

import java.util.Properties;

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;

public class Email {

    public Email() {
    }

    /*
     * @param args
     */
    public static void main(String[] args) {
        try {
            Email email = new Email();
            email.send();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send() throws Exception {

        try {
            String from = "xx@xx.com";
            String to = "xx@xx.com";
            Properties prop = new Properties();
            prop.put("mail.smtp.host", "mailhost");

            Session session = Session.getDefaultInstance(prop, null);
            // Define message
            MimeMessage message = new MimeMessage(session);
            message.addHeaderLine("method=REQUEST");
            message.addHeaderLine("charset=UTF-8");
            message.addHeaderLine("component=VEVENT");

            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Outlook Meeting Request Using JavaMail");

            StringBuffer sb = new StringBuffer();

            StringBuffer buffer = sb.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:xx@xx.com\n" +
                    "ORGANIZER:MAILTO:xx@xx.com\n" +
                    "DTSTART:20051208T053000Z\n" +
                    "DTEND:20051208T060000Z\n" +
                    "LOCATION:Conference room\n" +
                    "TRANSP:OPAQUE\n" +
                    "SEQUENCE:0\n" +
                    "UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n" +
                    " 000004377FE5C37984842BF9440448399EB02\n" +
                    "DTSTAMP:20051206T120102Z\n" +
                    "CATEGORIES:Meeting\n" +
                    "DESCRIPTION:This the description of the meeting.\n\n" +
                    "SUMMARY:Test meeting request\n" +
                    "PRIORITY:5\n" +
                    "CLASS:PUBLIC\n" +
                    "BEGIN:VALARM\n" +
                    "TRIGGER:PT1440M\n" +
                    "ACTION:DISPLAY\n" +
                    "DESCRIPTION:Reminder\n" +
                    "END:VALARM\n" +
                    "END:VEVENT\n" +
                    "END:VCALENDAR");

            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Fill the message
            messageBodyPart.setHeader("Content-Class", "urn:content-  classes:calendarmessage");
            messageBodyPart.setHeader("Content-ID", "calendar_message");
            messageBodyPart.setDataHandler(new DataHandler(
                    new ByteArrayDataSource(buffer.toString(), "text/calendar")));// very important

            // Create a Multipart
            Multipart multipart = new MimeMultipart();

            // Add part one
            multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            message.setContent(multipart);

            // send message
            Transport.send(message);
        } catch (MessagingException me) {
            me.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

3-替换你的变量,你就可以开始了!

票数 27
EN

Stack Overflow用户

发布于 2015-12-23 20:34:08

除了javamail之外,您还可以使用ical4j library来构建消息体(而不是使用StringBuffer)。

示例:

Calendar calendar = new Calendar();
PropertyList calendarProperties = calendar.getProperties();
calendarProperties.add(Version.VERSION_2_0);
calendarProperties.add(Method.REQUEST);
// other properties ...

VEvent vEvent = new VEvent(/*startDate, endDate*/);
PropertyList vEventProperties = vEvent.getProperties();
vEventProperties.add(Priority.MEDIUM);
vEventProperties.add(Clazz.PUBLIC);
// other properties ...

calendar.getComponents().add(vEvent);

messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(calendar.toString(), "text/calendar")));
票数 6
EN

Stack Overflow用户

发布于 2020-11-06 10:00:40

这里,

1-从here (JavaMail API » 1.6.2)下载jar。

2-复制并粘贴以下代码(更改变量值)

import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;

public class Index {

public static void main(String[] args) {

     try {
            final String username = "sender@mail.com";
            final String password = "xxxxx";
            
            String from = "sender@mail.com";
            String to = "attendee@mail.com";
            String subject = "Meeting Subject";
            String startDate = "20201208"; // Date Formate: YYYYMMDD
            String endDate = "20201208"; // Date Formate: YYYYMMDD
            String startTime = "0400"; // Time Formate: HHMM
            String endTime = "0600"; // Time Formate: HHMM
            String emailBody = "Hi Team, This is meeting description. Thanks"; 
            
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.starttls.enable", "true");
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.port", "25");

            Session session = Session.getDefaultInstance(prop,  new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
              });
            
            MimeMessage message = new MimeMessage(session);
            message.addHeaderLine("method=REQUEST");
            message.addHeaderLine("charset=UTF-8");
            message.addHeaderLine("component=VEVENT");

            message.setFrom(new InternetAddress(from, "New Outlook Event"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);

            StringBuffer sb = new StringBuffer();

            StringBuffer buffer = sb.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:"+ to +"\n" +
                    "DTSTART:"+ startDate +"T"+ startTime +"00Z\n" +
                    "DTEND:"+ endDate +"T"+ endTime +"00Z\n" +
                    "LOCATION:Conference room\n" +
                    "TRANSP:OPAQUE\n" +
                    "SEQUENCE:0\n" +
                    "UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n" +
                    " 000004377FE5C37984842BF9440448399EB02\n" +
                    "CATEGORIES:Meeting\n" +
                    "DESCRIPTION:"+ emailBody +"\n\n" +
                    "SUMMARY:Test meeting request\n" +
                    "PRIORITY:5\n" +
                    "CLASS:PUBLIC\n" +
                    "BEGIN:VALARM\n" +
                    "TRIGGER:PT1440M\n" +
                    "ACTION:DISPLAY\n" +
                    "DESCRIPTION:Reminder\n" +
                    "END:VALARM\n" +
                    "END:VEVENT\n" +
                    "END:VCALENDAR");

            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Fill the message
            messageBodyPart.setHeader("Content-Class", "urn:content-  classes:calendarmessage");
            messageBodyPart.setHeader("Content-ID", "calendar_message");
            messageBodyPart.setDataHandler(new DataHandler(
                    new ByteArrayDataSource(buffer.toString(), "text/calendar")));// very important

            // Create a Multipart
            Multipart multipart = new MimeMultipart();

            // Add part one
            multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            message.setContent(multipart);

            // send message
            Transport.send(message);
            
            System.out.println("Email sent!");
        } catch (MessagingException me) {
            me.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}
}

3-仅此而已,点击奔跑......

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1121715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档