我正在尝试使用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让事情变得粗糙?
答案 0 :(得分:24)
所以我解决了这个问题,这就是我发现的:
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 - 替换你的变量,你很高兴去!
答案 1 :(得分:5)
除了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")));
答案 2 :(得分:1)
没有迹象表明您曾经将起始地址设置为实际地址,因此无法回复。
这可能不是您的生产代码中的问题,但这看起来像是一个问题......
答案 3 :(得分:0)
在这里
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-就这样,按RUN ...