0

我正在尝试使用 iCalendar 和 Java Mail API 在 Lotus notes 上发送会议邀请,它工作正常,除了邮件内容中的附件即将到来,甚至没有打开。我已经检查了我的代码,并且可以确认代码末尾没有包含此类附件。有人可以解释一下为什么会这样吗?这是我的一段代码:

public void postMail(String meetingId, List<String> ToList,
        List<String> existingAttendeeList, String subject, String message,
        Date startDate, Date endDate, String from, boolean isMeetingInvite)
        throws UtilException {
    String[] recipients = null;
    if ((ToList == null || ToList.size() == 0)) {

        log.error("Both recipients and recipientCC are null");

        throw new UtilException("Both recipients and recipientCC are null");
    } else {
        recipients = new String[ToList.size()];
        recipients = (String[]) ToList.toArray(recipients);
    }

    boolean debug = false;


        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "false");
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);
        session.setDebug(debug);
        MimeMessage mimeMessage = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        mimeMessage.setFrom(addressFrom);
        if (!(recipients == null)) {
            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; i < recipients.length; i++) {
                addressTo[i] = new InternetAddress(recipients[i]);

            }
            mimeMessage.setRecipients(Message.RecipientType.TO, addressTo);
        }
        Multipart multipart = new MimeMultipart();
        MimeBodyPart iCalAttachment = new MimeBodyPart();
        TimeZone tz = TimeZone.getDefault();
        byte[] invite = createICalInvitation(meetingId, subject, message,
                startDate, endDate, tz, from, isMeetingInvite,
                existingAttendeeList);
        iCalAttachment.setDataHandler(new DataHandler(
                new ByteArrayDataSource(new ByteArrayInputStream(invite),
                        "text/calendar;method=REQUEST;charset=\"UTF-8\"")));
        multipart.addBodyPart(iCalAttachment);
        mimeMessage.setContent(multipart);
        Transport.send(mimeMessage);
    }   throw new UtilException(e.toString());
    }

}

private byte[] createICalInvitation(String _meetingID, String _subject,
        String _content, Date _start, Date _end, TimeZone _tz,
        String mailToAddress, boolean isMeetingInvite,
        List<String> existingAttendeeList) throws Exception {
    CompatibilityHints.setHintEnabled(
            CompatibilityHints.KEY_OUTLOOK_COMPATIBILITY, true);
    DateTime start = new DateTime(_start);
    DateTime end = new DateTime(_end);
    VEvent vEvent = new VEvent(start, end, _subject);
    vEvent.getProperties().add(new Uid(_meetingID));
    vEvent.getProperties().add(new Description(_content));
    vEvent.getProperties().add(new XProperty("X-LOTUS-BROADCAST", "false"));
    Attendee dev = new Attendee(URI.create("mailto:" + mailToAddress));
    vEvent.getProperties().add(dev);
    Organizer organizer = new Organizer(URI.create(mailToAddress));
    vEvent.getProperties().add(organizer);
    if (existingAttendeeList != null && existingAttendeeList.size() != 0) {
        for (String index : existingAttendeeList) {
            Attendee existingAttendee = new Attendee(index);
            vEvent.getProperties().add(existingAttendee);
        }
    }
    net.fortuna.ical4j.model.Calendar cal = new net.fortuna.ical4j.model.Calendar();
    cal.getProperties()
            .add(new ProdId(
                    "//Lotus Development Corporation//NONSGML Notes 8.5.2//EN_C"));
    cal.getProperties().add(
            net.fortuna.ical4j.model.property.Version.VERSION_2_0);
    cal.getProperties().add(CalScale.GREGORIAN);
    if (isMeetingInvite) {
        cal.getProperties().add(
                net.fortuna.ical4j.model.property.Method.REQUEST);
    } else {
        cal.getProperties().add(
                net.fortuna.ical4j.model.property.Method.CANCEL);
    }
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance()
            .createRegistry();
    VTimeZone tz = registry.getTimeZone(_tz.getID()).getVTimeZone();
    cal.getComponents().add(tz);
    cal.getComponents().add(vEvent);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.output(cal, bout);
    return bout.toByteArray();
}

下面是我运行此代码的主要方法:

public static void main(String[] args) {
    List a = new ArrayList();
    a.add("smtp.test2@abc.net");
    Calendar c = Calendar.getInstance();
    c.roll(Calendar.DATE, 5);
    Date d1 = c.getTime();
    c.add(Calendar.HOUR, 1);
    Date d2 = c.getTime();
    SendMail s = new SendMail();
    String id = "" + System.currentTimeMillis();
    try {
        s.postMail("212", a, null, "Hel22lo", "hel22lo", d1, d2,
                "smtp.test@abc.net", true);

}
4

1 回答 1

1

您正在发送只有一个正文部分的多部分消息。大多数邮件阅读器会假设第一个文本(纯文本或 html)正文是主体,其余的是附件。您的邮件没有正文部分,因此邮件阅读器可能会假设您发送的正文部分是附件。

于 2013-08-19T06:36:00.180 回答