【问题标题】:How to send an email with a calendar request (content type: text/calendar)如何发送带有日历请求的电子邮件(内容类型:文本/日历)
【发布时间】:2011-02-09 07:51:40
【问题描述】:

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

【问题讨论】:

    标签: delphi indy10


    【解决方案1】:

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

    查看此示例代码(在 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.
    

    【讨论】:

      【解决方案2】:

      这是 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. 
      

      【讨论】:

      • 谢谢,我运行了 RRUZ 程序,有点像在 gmail 中工作,但检索电子邮件时的 Outlook 刚刚终止。更加具体。该服务使用 Indy 自动从服务器发送电子邮件,每封电子邮件都有一个 text/plain 部分、text/html 部分和可选的附件。这工作正常。当尝试将 ical 与内容文本/日历类似的示例合并到末尾时;方法=请求作为最后一部分,程序在为第一部分编码消息后冻结,但日历部分出现问题。消息部分就像一个文本/纯文本,带有 ical 的 tstrings 和提到的内容。
      • 那么您可能没有正确填写 TIdMessage,请出示您的实际代码。
      • 我的代码在下面,添加的部分是附件等之后的最后一部分 Procedure Emailer.BuildCalendar(Msg_In : TIdMessage); var s: TStrings;常量制表符 = #9;开始尝试 s:= TStringList.Create; s.Add('BEGIN:VCALENDAR'); ...此处添加的其他行 ..... s.Add('END:VCALENDAR'); // 使用 TIdText.Create(Msg_In.MessageParts, s) 创建日历部件 do begin ContentType := 'text/calendar;方法="请求";字符集="UTF-8"';父部分 := 0 ; // IdText.CharSet 结束;终于 s.free;结尾;结尾; ....感谢
      • 您没有显示整个 TIdMessage 代码。但是,您正在设置 ParentPart:=0,这会将附件置于错误的邮件级别。 ParentPart 用于嵌套,附件不应嵌套。将 ParentPart 设置为 -1。
      • 就是这样。更改了 ParentPart,现在可以正常工作了。多么尴尬。非常感谢您的所有帮助。问候大卫
      猜你喜欢
      • 1970-01-01
      • 2020-11-18
      • 2017-08-26
      • 1970-01-01
      • 2021-09-13
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多