如何发送包含日历请求的电子邮件(内容类型:文本/日历)

时间:2010-04-18 07:29:58

标签: delphi indy10

我试图将icalendar代码嵌入到电子邮件中,通过indy发送内容类型的文本/日历,但它只是挂在电子邮件的编码上,当我作为附件添加它只是作为附件到达而且不像其他日历请求那样提示。有没有人通过indy得到如何做日历请求的示例代码?

2 个答案:

答案 0 :(得分:4)

@David,如果您将该属性设置为ContentType:='text/calendar',则电子邮件客户端会将该附件识别为日历请求,请参阅此iCalendar format Specification

的链接

请参阅此示例代码(已测试en Delphi 2010)

program SendMailWithCalendarRequest;
{$APPTYPE CONSOLE}


uses
  IdSMTP,
  Classes,
  DateUtils,
  IdAttachmentFile,
  IdMessage,
  SysUtils;

 procedure SendCalendarRequest;
 var
  SMTP        : TIdSMTP;
  MailMessage : TIdMessage;
  Calendar    : TStrings;
  CalendarFile: String;
  Attachment  : TIdAttachmentFile;
  SenderMail  : String;
 begin
    SenderMail:='mail@server.com';
    CalendarFile:=ExtractFilePath(ParamStr(0))+'\appmnt.vcs';
    Calendar:=TStringList.Create;
    try
        Calendar.Add('BEGIN:VCALENDAR');
        Calendar.Add('VERSION:1.0');
        Calendar.Add('BEGIN:VEVENT');
        Calendar.Add('ORGANIZER:MAILTO:'+SenderMail);
        Calendar.Add('DTStart:'+FormatDateTime('YYYY-DD-DD',Now));
        Calendar.Add('DTEnd:'+FormatDateTime('YYYY-DD-DD',  Tomorrow));
        Calendar.Add('Location;ENCODING=QUOTED-PRINTABLE: My home');
        Calendar.Add('UID:'+FormatDateTime('YYYY-DD-DD',Now)+FormatDateTime('YYYY-DD-DD',Tomorrow));
        Calendar.Add('SUMMARY:Appointment Reminder');
        Calendar.Add('DESCRIPTION:Test message');
        Calendar.Add('PRIORITY:5');
        Calendar.Add('END:VEVENT');
        Calendar.Add('END:VCALENDAR');
        Calendar.SaveToFile(CalendarFile);
    finally
    Calendar.Free;
    end;

   SMTP:= TIdSMTP.Create(nil);
   MailMessage := TIdMessage.Create(nil);
   try
     SMTP.Host := 'smtp.mailserver.com';
     SMTP.Port := 25;
     SMTP.Username:='the account';
     SMTP.Password:='the password';
     SMTP.AuthType:=satDefault;
     MailMessage.From.Address := SenderMail;
     MailMessage.Recipients.EMailAddresses := 'the Recipient';
     MailMessage.Subject   := 'Send calendar';
     MailMessage.Body.Text := '';
     Attachment:=TIdAttachmentFile.Create(MailMessage.MessageParts, CalendarFile) ;
     Attachment.ContentType:='text/calendar';//set the content type to text/calendar
     try
       try
         SMTP.Connect;
         SMTP.Send(MailMessage) ;
         Writeln('OK')
       except on E:Exception do
         Writeln(0, 'ERROR: ' + E.Message) ;
       end;
     finally
       if SMTP.Connected then SMTP.Disconnect;
     end;
   finally
    SMTP.Free;
    MailMessage.Free;
   end;

 end;

begin
  try
    SendCalendarRequest;
    readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

答案 1 :(得分:4)

以下是RRUZ示例的替代方案:

program SendMailWithCalendarRequest; 
{$APPTYPE CONSOLE} 

uses 
  IdSMTP, 
  Classes, 
  DateUtils, 
  IdMessage, 
  SysUtils; 

 procedure SendCalendarRequest; 
 var 
  SMTP        : TIdSMTP; 
  MailMessage : TIdMessage; 
 begin 
   SMTP:= TIdSMTP.Create(nil); 
   MailMessage := TIdMessage.Create(nil); 
   try 
     SMTP.Host := 'smtp.mailserver.com'; 
     SMTP.Port := 25; 
     SMTP.Username := 'the account'; 
     SMTP.Password := 'the password'; 
     SMTP.AuthType := satDefault; 
     MailMessage.From.Address := 'mail@server.com'; 
     MailMessage.Recipients.EMailAddresses := 'the Recipient'; 
     MailMessage.Subject := 'Send calendar'; 
     MailMessage.Body.Add('BEGIN:VCALENDAR'); 
     MailMessage.Body.Add('VERSION:1.0'); 
     MailMessage.Body.Add('BEGIN:VEVENT'); 
     MailMessage.Body.Add('ORGANIZER:MAILTO:'+SenderMail); 
     MailMessage.Body.Add('DTStart:'+FormatDateTime('YYYY-DD-DD',Now)); 
     MailMessage.Body.Add('DTEnd:'+FormatDateTime('YYYY-DD-DD',  Tomorrow)); 
     MailMessage.Body.Add('Location;ENCODING=QUOTED-PRINTABLE: My home'); 
     MailMessage.Body.Add('UID:'+FormatDateTime('YYYY-DD-DD',Now)+FormatDateTime('YYYY-DD-DD', Tomorrow)); 
     MailMessage.Body.Add('SUMMARY:Appointment Reminder'); 
     MailMessage.Body.Add('DESCRIPTION:Test message'); 
     MailMessage.Body.Add('PRIORITY:5'); 
     MailMessage.Body.Add('END:VEVENT'); 
     MailMessage.Body.Add('END:VCALENDAR'); 
     MailMessage.ContentType := 'text/calendar';
     SMTP.Connect; 
     try 
       try 
         SMTP.Send(MailMessage) ; 
         Writeln('OK') 
       except on E:Exception do 
         Writeln(0, 'ERROR: ' + E.Message) ; 
       end; 
     finally 
       SMTP.Disconnect; 
     end; 
   finally 
    SMTP.Free; 
    MailMessage.Free; 
   end; 
 end; 

begin 
  try 
    SendCalendarRequest; 
    readln; 
  except 
    on E: Exception do 
      Writeln(E.ClassName, ': ', E.Message); 
  end; 
end.