使用Indy组件的最大消息长度

时间:2015-04-28 19:38:20

标签: delphi indy indy-9

我尝试使用TIdMessage从delphi代码发送消息。该邮件由两部分组成:text/plaintext/html。    我有html页面作为模板和一些需要插入模板的大文本(在标记为## text ##的特殊位置)。合并模板和文本后,我得到了我的新消息的正文。然后我发送此消息(使用TIdMessageTIdSMTP),但结果消息仅包含文本的第二部分。该文本有40,000多个字符。 当我发送没有html-tmplate(只有大文本)的消息时,一切都很好。

如何设置消息长度?

我试图设置FIdMessage.ContentTransferEncoding := 'quoted-printable' and FIdSMTP.MsgLineLength := MAXWORD;,但它对我没有帮助。

提前致谢。
以下是一些代码:

var
  FSMTP: TIdSMTP;
  FIdMessage: TIdMessage;
  idx: integer;
  i: Integer;
  FIdText: TIdText;
begin
  FSMTP := TIdSMTP.Create( nil );
  FIdMessage := TIdMessage.Create( nil );
  try
    try
      FIdMessage.ExtraHeaders.Clear;
      FIdMessage.MessageParts.Clear;

      // Attachments
      for idx := 0 to Attachments.Count - 1 do
        TIdAttachment.Create( FIdMessage.MessageParts,
          TEmailAttachment( Attachments.Items[ idx ] ).FLocalTemplateName );


      FIdMessage.From.Text := FFrom;
      FIdMessage.Recipients.EMailAddresses := FTo;
      FIdMessage.Subject := FSubject;
      if (FHtmlTemplateFilePath <> '') then
      begin
        FIdMessage.ContentType := 'multiparts/related; type="text/html"';
        AddAttachements(FIdMessage);  // procedure that added Attachments

        FIdText := TIdText.Create(FIdMessage.MessageParts, nil);
        FIdText.Body.Text := 'Please view a html version of this email';
        FIdText.ContentType := 'text/plain';


        FIdText := TIdText.Create(FIdMessage.MessageParts, nil);
        FIdText.Body.Text := FBody;
        FIdText.ContentType := 'text/html';

      end
      else
      begin
        FIdMessage.Body.Text := FBody;
      end;    

      FIdMessage.CCList.Clear;
      FIdMessage.ReceiptRecipient.Text := '';

      FSMTP.AuthenticationType := FSMTPAuthenticationType;
      FSMTP.UserID := FSMTPUserID;
      FSMTP.Password := FSMTPPassword;
      FSMTP.Host := FSMTPHost;
      FSMTP.Port := FSMTPPort;
      FSMTP.MsgLineLength := MAXWORD;

      RepeatRetryCount := FSMTPRepeatRetryCount;
      FSMTP.Connect;
       try
            FSMTP.Send( FIdMessage );
       finally
            FSMTP.Disconnect;
       end;
     except
       end;
  finally
    FSMTP.Free;
    FIdMessage.Free;
  end;
end;
procedure AddAttachements(AIdMessage: TidMessage);
var
  LCid: Integer;
  LFileName, LFileExt, LSearchFolder: String;
  LSearchResult: TSearchRec;
  Attachment: TIdAttachment;
begin
  FHtmlCids.Clear;
  LSearchFolder := ExtractFilePath(FHtmlTemplateFilePath)+ChangeFileExt(ExtractFileName(FHtmlTemplateFilePath),'');
  if (FindFirst(LSearchFolder + '_*', faDirectory, LSearchResult)=0) or
     (FindFirst(LSearchFolder + '.*', faDirectory, LSearchResult)=0)
  then
    LSearchFolder := LSearchResult.Name;

  FindClose(LSearchResult);
  if FindFirst(ExtractFilePath(FHtmlTemplateFilePath)+LSearchFolder+'\*.*', faAnyFile - faDirectory, LSearchResult)=0 then
  begin
    repeat
      Attachment := TIdAttachment.Create(AIdMessage.MessageParts, ExtractFilePath(FHtmlTemplateFilePath)+ LSearchFolder + '\' + LSearchResult.Name);
      LCid := Random(MaxInt);
      Attachment.ExtraHeaders.Values['Content-ID'] := IntToStr(LCid);
      LFileName := ExtractFileName(LSearchResult.Name);
      LFileExt := ExtractFileExt(LSearchResult.Name);
      if UpperCase(LFileExt) = '.XML' then
        Attachment.ContentType := 'text/xml'
      else if UpperCase(LFileExt) = '.PNG' then
        Attachment.ContentType := 'image/png'
      else if UpperCase(LFileExt) = '.THMX' then
        Attachment.ContentType := 'application/vnd.ms-officetheme'
      else if UpperCase(LFileExt) = '.JPG' then
        Attachment.ContentType := 'image/jpeg'
      else if UpperCase(LFileExt) = '.GIF' then
        Attachment.ContentType := 'image/gif'
      else if UpperCase(LFileExt) = '.SVG' then
        Attachment.ContentType := 'image/svg+xml'
      else if UpperCase(LFileExt) = '.TIF' then
        Attachment.ContentType := 'image/tiff'
      else if UpperCase(LFileExt) = '.TIFF' then
        Attachment.ContentType := 'image/tiff'
      else if UpperCase(LFileExt) = '.ICO' then
        Attachment.ContentType := 'image/vnd.microsoft.icon'
      else if UpperCase(LFileExt) = '.BMP' then
        Attachment.ContentType := 'image/bmp'
      else if UpperCase(LFileExt) = '.CSS' then
        Attachment.ContentType := 'text/css'
      else if UpperCase(LFileExt) = '.JS' then
        Attachment.ContentType := 'application/javascript'
      else if UpperCase(LFileExt) = '.JPEG' then
        Attachment.ContentType := 'image/jpeg'
      else if UpperCase(LFileExt) = '.WMZ' then
        Attachment.ContentType := 'application/x-ms-wmz'
      else raise Exception.CreateFmt('Unknown file type "%s"', [LFileExt]);
      FHtmlCids.AddObject(LSearchFolder+'/'+LFileName, TObject(LCid));
      FHtmlCids.AddObject(UrlEncode(LSearchFolder+'/'+LFileName), TObject(LCid));
    until FindNext(LSearchResult)<>0;
    FindClose(LSearchResult);
  end;
  FHtmlCids.Sort;
end;

2 个答案:

答案 0 :(得分:2)

TIdMessage不会对文字长度施加限制。还有其他事情正在发生。我的猜测是你没有正确填写TIdMessage。您没有显示AddAttachments()代码,但是您要在TIdAttachment个对象之前添加TIdText个对象,这是错误的,尤其是对于您正在使用的ContentType。我建议您阅读以下关于Indy网站上的博客文章,了解TIdMessage中HTML电子邮件的正确结构:

HTML Messages

但是,请注意以下警告:

  

不幸的是,Indy 9并没有处理明文+ html +图像的场景。 Indy 10处理得更好......

你显然没有使用Indy 10(正如你将文件名传递给TIdAttachment构造函数那样明显 - 构造函数被移动到Indy 10中的新TIdAtttachmentFile类)。 / p>

另外,仅仅是FYI,你不需要设置MsgLineLength属性,因为它没有任何效果(事实上,我不知道Indy甚至还有那个属性 - 它是一个死的属性使用的TIdMessageClient.WriteFoldedLine(),这本身就是一种不被任何东西使用的死方法。

答案 1 :(得分:1)

感谢大家,我似乎找到了解决方案。 问题出在CR LF上。 在调用我的程序(见上文)发送消息之前,我在html-template中插入了大文本而不是## text ##。首先,我在我的文字中替换了CR上的所有LF <br>,然后我在我的文字上替换了## text ##在模板中。 (我的文字 - 一些大文字)

结果我在发送之前收到了消息正文:

<html>
....
<body>
some large text <br> in one line. This text have more 40,000 characrets.....
</body>
</html>

按摩:

  

INE。本文有40,000多个特征

当我离开CR LF并添加标记<br>时,我就得到了这个帖子:

<html>
....
  <body>
    some large text <br> 
    in one line. This text have more 40,000 characrets.....
  </body>
</html>

并留言:

  一些大文字
  在一条线上。这个文本有40,000多个   characrets .....

因此,在这种情况下,如果所有文本都在一行中 - &gt;按摩不是完整的,如果html中的文字在几行 - &gt;一切都好吗

PS。对不起我的英文

相关问题