TIdSMTP和TIdAttachmentMemory - 垃圾邮件过滤器拒绝了电子邮件

问题描述 投票:0回答:1

我正在尝试使用TIdSMTP发送带有PDF附件的电子邮件,存储在BLOB字段中。为此,我使用的是TIdAttachmentMemory,但显示的代码导致“被垃圾邮件过滤器拒绝”;

  1. 省略IdMessage.ContentType := 'multipart/mixed'有效但附件未发送(或收到?) - 正如所料。
  2. 离开此声明并从文件创建附件(如在注释代码中)它一切正常(即通过附件正确接收邮件)。

显然我错过了一些东西。我怀疑附件的方向没有正确地“关闭”(即处于不完整的状态)或者可能是不正确的ContentType?

欢迎所有建议。谢谢!

procedure TfrmSendMail.btnSendClick(Sender: TObject);
var
  ms: TMemoryStream;
  Attachment: TIdAttachmentMemory;
  // Attachment: TIdAttachmentFile;
begin
  memStatus.Clear;

  IdSSLIOHandlerSocketOpenSSL.Destination := teHost.Text + ':587';
  IdSSLIOHandlerSocketOpenSSL.Host := teHost.Text;
  // IdSSLIOHandlerSocketOpenSSL.MaxLineAction := maException;
  IdSSLIOHandlerSocketOpenSSL.Port := 587;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvTLSv1_2;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmUnassigned;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := [];
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := 0;

  IdSMTP.Host := teHost.Text;
  IdSMTP.Port := 587;

  IdMessage.From.Address := teFrom.Text;
  IdMessage.Recipients.EMailAddresses := teTo.Text;
  IdMessage.Subject := teSubject.Text;
  IdMessage.Body.Text := memBody.Text;
  IdMessage.Body.Add('Timestamp: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now()));

  IdMessage.ContentType := 'multipart/mixed';

  if not sqlPDFPDF_Incasso.IsNull then
  begin
    ms := TMemoryStream.Create;
    try
      try
        TBlobField(sqlPDF.FieldByName('PDF_Incasso')).SaveToStream(ms);
        ms.Position := 0;
        Attachment := TIdAttachmentMemory.Create(IdMessage.MessageParts, ms);
        Attachment.ContentType := 'application/pdf';
        Attachment.FileName := 'Invoice.pdf';
      except
        on E: Exception do
          messageDlg('Error creating attachment' + #13#10 + E.Message, mtError, [mbOK], 0);
      end;
    finally
      ms.Free;
    end;
  end;

  // if FileExists(beAttachment.Text) then
  // Attachment := TIdAttachmentFile.Create(IdMessage.MessageParts, beAttachment.Text);

  Screen.Cursor := crHourGlass;
  try
    try
      IdSMTP.Connect;
      IdSMTP.Send(IdMessage);
      memStatus.Lines.Insert(0, 'Email sent - OK.');
    except
      on E: Exception do
        memStatus.Lines.Insert(0, 'ERROR: ' + E.Message);
    end;
  finally
    if assigned(Attachment) then
      Attachment.Free;
    if IdSMTP.Connected then
      IdSMTP.Disconnect(true);
    Screen.Cursor := crDefault;
  end;
end;
delphi pdf smtp attachment indy
1个回答
1
投票

您没有正确填充TIdMessage(有关详细信息,请参阅this blog article - 您的用例将属于“HTML和非相关附件,没有纯文本”部分,但用纯文本替换HTML)。

简而言之,如果你包含附件,将TIdMessage.ContentType设置为'multipart/mixed'就可以了,但你需要将正文放入TIdText中的TIdMessage.MessageParts对象而不是TIdMessage.Body。如果你不包括附件,使用TIdMessage.Body很好,但你需要将TIdMessage.ContentType设置为'text/plain'

试试这个:

procedure TfrmSendMail.btnSendClick(Sender: TObject);
var
  Text: TIdText;
  Attachment: TIdAttachmentMemory;
  Strm: TStream;
begin
  memStatus.Clear;

  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvTLSv1_2;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmClient;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := [];
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := 0;

  IdSMTP.Host := teHost.Text;
  IdSMTP.Port := 587;

  try
    IdMessage.Clear;

    IdMessage.From.Address := teFrom.Text;
    IdMessage.Recipients.EMailAddresses := teTo.Text;
    IdMessage.Subject := teSubject.Text;

    //if FileExists(beAttachment.Text) then
    if not sqlPDFPDF_Incasso.IsNull then
    begin
      IdMessage.ContentType := 'multipart/mixed';

      Text := TIdText.Create(IdMessage.MessageParts, nil);
      Text.Body.Text := memBody.Text;
      Text.Body.Add('Timestamp: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now()));
      Text.ContextType := 'text/plain';

      //Attachment := TIdAttachmentFile.Create(IdMessage.MessageParts, beAttachment.Text);
      Attachment := TIdAttachmentMemory.Create(IdMessage.MessageParts);
      Attachment.ContentType := 'application/pdf';
      Attachment.FileName := 'Invoice.pdf';
      Strm := Attachment.PrepareTempStream;
      try
        TBlobField(sqlPDFPDF_Incasso).SaveToStream(Strm);
      finally
        Attachment.FinishTempStream;
      end;
    end else
    begin
      IdMessage.ContentType := 'text/plain';

      IdMessage.Body.Text := memBody.Text;
      IdMessage.Body.Add('Timestamp: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now()));
    end;

    Screen.Cursor := crHourGlass;
    try
      IdSMTP.Connect;
      try
        IdSMTP.Send(IdMessage);
      finally
        IdSMTP.Disconnect;
      end;
      memStatus.Lines.Insert(0, 'Email sent - OK.');
    finally
      Screen.Cursor := crDefault;
    end;
  except
    on E: Exception do
      memStatus.Lines.Insert(0, 'ERROR: ' + E.Message);
  end;
end;

或者,Indy有一个TIdMessageBuilderPlain类,可以为您正确设置TIdMessage(有关详细信息,请参阅this blog article - 您的用例将属于“纯文本和HTML和附件:仅与非相关附件”部分):

uses
  ..., IdMessageBuilder;

procedure TfrmSendMail.btnSendClick(Sender: TObject);
var
  Strm: TStream;
  Bldr: TIdMessageBuilderPlain;
begin
  memStatus.Clear;

  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvTLSv1_2;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmClient;
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := [];
  IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := 0;

  IdSMTP.Host := teHost.Text;
  IdSMTP.Port := 587;

  try
    IdMessage.Clear;

    IdMessage.From.Address := teFrom.Text;
    IdMessage.Recipients.EMailAddresses := teTo.Text;
    IdMessage.Subject := teSubject.Text;

    Strm := nil;
    try
      Bldr := TIdMessageBuilderPlain.Create;
      try
        Bldr.PlainText.Text := memBody.Text;
        Bldr.PlainText.Add('Timestamp: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', Now()));

        //if FileExists(beAttachment.Text) then
        if not sqlPDFPDF_Incasso.IsNull then
        begin
          //Bldr.Attachments.Add(beAttachment.Text);
          Strm := sqlPDFPDF_Incasso.DataSet.CreateBlobStream(sqlPDFPDF_Incasso, bmRead);
          Bldr.Attachments.Add(Strm, 'application/pdf').WantedFileName := 'Invoice.pdf';
        end;

        Bldr.FillMessage(IdMessage);
      finally
        Bldr.Free;
      end;
    finally
      Strm.Free;
    end;

    Screen.Cursor := crHourGlass;
    try
      IdSMTP.Connect;
      try
        IdSMTP.Send(IdMessage);
      finally
        IdSMTP.Disconnect;
      end;
      memStatus.Lines.Insert(0, 'Email sent - OK.');
    finally
      Screen.Cursor := crDefault;
    end;
  except
    on E: Exception do
      memStatus.Lines.Insert(0, 'ERROR: ' + E.Message);
  end;
end;
© www.soinside.com 2019 - 2024. All rights reserved.