我正在尝试使用delphi中的indy组件创建一个电子邮件应用程序,但是我一直想弄清楚如何发送附件。

时间:2018-07-25 22:04:30

标签: email delphi

我的表单包含TIdSMTP,TIdMessage,TOpenDialog,SSL处理程序和其他可视组件。我也有用于发送和附加不同文件的按钮。

procedure TForm1.Button1Click(Sender: TObject);
begin
  email_connecter_TIdSMTP.Host := entered_host_TEdit.Text;
  email_connecter_TIdSMTP.Username := entered_username_TEdit.Text;
  email_connecter_TIdSMTP.Password := entered_password_TEdit.Text;

  message_parts_TIdMessage.Clear();
  message_parts_TIdMessage.Recipients.EMailAddresses := to_sender_email_TEdit.Text;
  message_parts_TIdMessage.Subject := email_subject_TEdit.Text;
  message_parts_TIdMessage.Body.Text := email_body_message_TMemo.Text;

  email_connecter_TIdSMTP.Connect();
  email_connecter_TIdSMTP.Send(message_parts_TIdMessage);
  email_connecter_TIdSMTP.Disconnect();

end;


procedure TForm1.Button3Click(Sender: TObject);
var t:textfile;
s:string;
selected_file:string;
attatchment_message: TIdMessageBuilderHtml;
begin

selected_file := '';
    try
      attatchment_finder_TOpenDialog.InitialDir := 'C:\Documents';
      attatchment_finder_TOpenDialog.Filter := 'All files (*.*)|*.*';

    if attatchment_finder_TOpenDialog.Execute(Handle) then
      selected_file := attatchment_finder_TOpenDialog.FileName;

      if selected_file <>'' then

        attatchment_message := TIdMessageBuilderHtml.Create;
        attatchment_message.HtmlContentTransfer := 'quoted-printable';

        memo_attachment_box_TMemo.Lines.Add(selected_file);
        attatchment_message.Attachments.Add(selected_file);
        attatchment_message.FillMessage(message_parts_TIdMessage);
      finally
      attatchment_finder_TOpenDialog.Free;
    end;
end;
end.

添加所选文件时我在做什么错?我该如何做才能发送任何文件类型?

当我在程序上单击“发送”时,它将文件目录文本添加到我的备忘录框中,但实际上并没有将文件附加到我的TIdMessage组件上。

谢谢!

1 个答案:

答案 0 :(得分:4)

您显示的代码在发送前便清除了整个TIdMessage,清除了Button3Click()可能预先添加的所有附件。

实际上,Button3Click()根本不应该直接与TIdMessage做任何事情。清除后填充Button1Click()时,该责任仅属于TIdMessage

此外,您没有正确使用TIdMessageBuilderHtml。您应该使用其PlainTextHTML属性(取决于您要发送的文本类型),而不是直接设置TIdMessage.Body。否则,FillMessage()将无法正确设置TIdMessage.ContentType。如果要发送纯文本而不是HTML,则应改用TIdMessageBuilderPlain

尝试更多类似的方法:

procedure TForm1.Button1Click(Sender: TObject);
var
  email_builder: TIdMessageBuilderPlain;
  I: integer;
begin
  email_connecter_TIdSMTP.Host := entered_host_TEdit.Text;
  email_connecter_TIdSMTP.Username := entered_username_TEdit.Text;
  email_connecter_TIdSMTP.Password := entered_password_TEdit.Text;

  message_parts_TIdMessage.Clear;
  message_parts_TIdMessage.Recipients.EMailAddresses := to_sender_email_TEdit.Text;
  message_parts_TIdMessage.Subject := email_subject_TEdit.Text;

  email_builder := TIdMessageBuilderPlain.Create;
  try
    email_builder.PlainText.Assign(email_body_message_TMemo.Lines);
    email_builder.PlainTextContentTransfer := 'quoted-printable';

    for I := 0 to memo_attachment_box_TMemo.Lines.Count-1 do
      email_builder.Attachments.Add(memo_attachment_box_TMemo.Lines[I]);

    email_builder.FillMessage(message_parts_TIdMessage);
  finally
    email_builder.Free;
  end;

  email_connecter_TIdSMTP.Connect;
  try
    email_connecter_TIdSMTP.Send(message_parts_TIdMessage);
  finally
    email_connecter_TIdSMTP.Disconnect;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  attatchment_finder_TOpenDialog.InitialDir := 'C:\Documents';
  attatchment_finder_TOpenDialog.Filter := 'All files (*.*)|*.*';

  if attatchment_finder_TOpenDialog.Execute(Handle) then
  begin
    memo_attachment_box_TMemo.Lines.Add(attatchment_finder_TOpenDialog.FileName);
    // or, if ofAllowMultiSelect is enabled:
    // memo_attachment_box_TMemo.Lines.AddStrings(attatchment_finder_TOpenDialog.Files);
  end;
end;