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

时间:2019-04-22 09:11:44

标签: delphi pdf smtp attachment indy

我正在尝试使用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;

1 个答案:

答案 0 :(得分:1)

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

简而言之,如果您包括附件,可以将TIdMessage.ContentType设置为'multipart/mixed',但是您需要将正文文本放入{{1 }},而不是TIdText中。而且,如果您不包括附件,则可以使用TIdMessage.MessageParts,但是您需要将TIdMessage.Body设置为TIdMessage.Body

尝试一下:

TIdMessage.ContentType

或者,Indy有一个'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; (有关详细信息,请参见this blog article-您的用例将属于“纯文本和HTML”和附件:“仅不相关的附件”部分):

TIdMessageBuilderPlain