我正在尝试使用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);
}
您发送的是一个只有一个正文部分的多部分消息。大多数邮件阅读器会假设第一个文本(纯文本或html)主体是主体,其余部分是附件。您的邮件没有正文部分,因此邮件阅读器可能会假设您发送的正文部分是附件。