簡體   English   中英

Delphi Indy附件不起作用

[英]Delphi Indy Attachments not working

我有一個應用程序,需要從網絡驅動器上進行一些FTP上傳。 我正在使用Indy。 然后,當文件位於網絡驅動器上並成功上傳到FTP服務器時,我想將此同一文件通過電子郵件發送給同事。

我正在使用下面的代碼來執行此操作。 電子郵件被發送正常,但由於某種原因,附件永遠不會成功。 我的代碼中我做錯了什么?

我將文件(在FTP過程中)添加到名為EmailFiles(TStringList)的公共(表單)成員變量中,並將其傳遞給過程。 在這里,我獲取文件名列表並嘗試將其添加到我的TIdMessage組件中。 發送電子郵件時,沒有附件....

    procedure TfrmMain.SendEmail(FromMail, ToMail, Subject, Body: String;
  Attachments: TStringList);
var
  i: Integer;
  Att : TIdAttachmentFile;
begin
   Memo1.Lines.Add('');
   Memo1.Lines.Add('Starting Email service...');
   SMTP.Host := 'mail.*****.com';
   SMTP.Username := '***UN***';
   SMTP.Password := '***PW***';
   try
     Msg1.From.Address := FromMail;
     Msg1.Recipients.EmailAddresses := ToMail;
     Msg1.Subject := Subject;
     Msg1.Body.Add(Body);

     //Add attachment(s)
     if Attachments.Count <= 0 then Memo1.Lines.Add('Warning: Cannot detect attachments for the Email...');

     for i := 0 to Attachments.Count - 1 do
       begin
          if FileExists(Attachments[i]) then
            begin
              //Memo1.Lines.Add('Adding Attachment ' + Msg1.MessageParts.Items[0].FileName + '...');
              Att := TIdAttachment.Create(Msg1.MessageParts, Attachments[i]);
              Msg1.MessageParts.Add;  //an attempt to explicitly ADD the Att object, to no avail
              Memo1.Lines.Append('Added Attachment ' + Attachments[i]);
              Att.Free;
            end
          else
            begin
              Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
            end;
       end;

       //Try to send the message
       try
         SMTP.Connect;
         if Msg1.MessageParts.AttachmentCount > 0 then begin
           SMTP.Send(Msg1);
           Memo1.Lines.Add('Sent Email successfully!');
         end
          else begin
            if Messagedlg('Do you want to send the Email without attachments?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
              begin
                SMTP.Send(Msg1);
                Memo1.Lines.Add('Sent Email successfully, without attachments!');
              end
            else
              Memo1.Lines.Add('No files attached to the Email Message - Cannot send!');
          end;

       except
         on E:Exception do
           begin
             Messagedlg('Could not send the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
           end;
       end;

   except
     on E:Exception do
       ShowMessage('Could not connect to SMTP Server' + #13#10 + E.Message);
   end;
end;

不要釋放Att對象,並且調用Msg1.MessageParts.Add將不會執行任何操作。

if FileExists(Attachments[i]) then
begin      
  TIdAttachment.Create(Msg1.MessageParts, Attachments[i]);
end
else
begin
  Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
end;

您還需要指定電子郵件內容類型:

Msg1.ContentType := 'multipart/mixed';

請參閱此Indy博客 ,請參閱“HTML和非相關附件,無明文”部分

此代碼未正確管理電子郵件。 使用更像這樣的東西:

procedure TfrmMain.SendEmail(FromMail, ToMail, Subject, Body: String;
  Attachments: TStringList);
var
  i: Integer;
  Att : TIdAttachmentFile;
begin
  Memo1.Lines.Add('');
  Memo1.Lines.Add('Starting Email service...');

  try
    Msg1.Clear;
    Msg1.From.Address := FromMail;
    Msg1.Recipients.EmailAddresses := ToMail;
    Msg1.Subject := Subject;

    // note, if attachments are being sent, the Body needs to
    // be added as a TIdText in the Msg1.MessageParts collection.
    // If ConvertPreamble is true, Msg1.Body is moved to a
    // TIdText for you during sending...
    Msg1.ConvertPreable := True;
    Msg1.Body.Text := Body;

    //Add attachment(s)
    if Attachments.Count = 0 then
      Memo1.Lines.Add('Warning: Cannot detect attachments for the Email...');

    for i := 0 to Attachments.Count - 1 do
    begin
      if FileExists(Attachments[i]) then
      begin
        //Memo1.Lines.Add('Adding Attachment ' + Attachments[i] + '...');
        Att := TIdAttachmentFile.Create(Msg1.MessageParts, Attachments[i]);
        // set properties of Att as needed...
        Memo1.Lines.Append('Added Attachment ' + Attachments[i]);
        // DO NOT free Att here! It will be freed when
        // the TIdMessage is cleared/freed...
      end
      else
      begin
        Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
      end;
    end;

    Msg1.MessageParts.CountParts;
    if Msg1.MessageParts.AttachmentCount = 0 then
    begin
      if MessageDlg('Do you want to send the Email without attachments?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
      begin
        Memo1.Lines.Add('No files attached to the Email Message - Cannot send!');
        Exit;
      end;
      // only the Body is being sent
      Msg1.ContentType := 'text/plain';
    end else
    begin
      // Body and Attachments are being sent
      Msg1.ContentType := 'multipart/mixed';
    end;
  except
    on E: Exception do
    begin
      MessageDlg('Could not prepare the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  //Try to send the message

  SMTP.Host := 'mail.*****.com';
  SMTP.Username := '***UN***';
  SMTP.Password := '***PW***';

  try
    SMTP.Connect;
  except
    on E: Exception do
    begin
      MessageDlg('Could not connect to SMTP Server!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  try
    try
      SMTP.Send(Msg1);
    finally
      SMTP.Disconnect;
    end;
  except
    on E: Exception do
    begin
      MessageDlg('Could not send the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  if Msg1.MessageParts.AttachmentCount > 0 then begin
    Memo1.Lines.Add('Sent Email successfully!');
  end else begin
    Memo1.Lines.Add('Sent Email successfully, without attachments!');
  end;
end;

或者,您可以使用TIdMessageBuilderPlain來幫助您正確設置TIdMessage

uses
  ..., IdMessageBuilder;

procedure TfrmMain.SendEmail(FromMail, ToMail, Subject, Body: String;
  Attachments: TStringList);
var
  i: Integer;
  Bldr: TIdMessageBuilderPlain;
begin
  Memo1.Lines.Add('');
  Memo1.Lines.Add('Starting Email service...');

  try
    Msg1.Clear;
    Msg1.From.Address := FromMail;
    Msg1.Recipients.EmailAddresses := ToMail;
    Msg1.Subject := Subject;

    Bldr := TIdMessageBuilderPlain.Create;
    try
      Bldr.PlainText.Text := Body;

      //Add attachment(s)
      if Attachments.Count = 0 then
        Memo1.Lines.Add('Warning: Cannot detect attachments for the Email...');

      for i := 0 to Attachments.Count - 1 do
      begin
        if FileExists(Attachments[i]) then
        begin
          //Memo1.Lines.Add('Adding Attachment ' + Attachments[i] + '...');
          Bldr.Attachments.Add(Attachments[i]);
          Memo1.Lines.Append('Added Attachment ' + Attachments[i]);
        end
        else
        begin
          Memo1.Lines.Add('Could not locate file: ' + Attachments[i] + ' for Email attachment!');
        end;
      end;

      if Bldr.Attachments.Count = 0 then
      begin
        if MessageDlg('Do you want to send the Email without attachments?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
        begin
          Memo1.Lines.Add('No files attached to the Email Message - Cannot send!');
          Exit;
        end;
      end;

      Bldr.FillMessage(Msg1);
    finally
      Bldr.Free;
    end;
  except
    on E: Exception do
    begin
      MessageDlg('Could not prepare the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  //Try to send the message

  SMTP.Host := 'mail.*****.com';
  SMTP.Username := '***UN***';
  SMTP.Password := '***PW***';

  try
    SMTP.Connect;
  except
    on E: Exception do
    begin
      MessageDlg('Could not connect to SMTP Server!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  try
    try
      SMTP.Send(Msg1);
    finally
      SMTP.Disconnect;
    end;
  except
    on E: Exception do
    begin
      MessageDlg('Could not send the Email Message!' + #13#10 + E.Message, mtError, [mbOK], 0);
      Exit;
    end;
  end;

  if Msg1.MessageParts.AttachmentCount > 0 then begin
    Memo1.Lines.Add('Sent Email successfully!');
  end else begin
    Memo1.Lines.Add('Sent Email successfully, without attachments!');
  end;
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM