发送附件Client Agnostic的电子邮件

Jam*_*est 7 delphi delphi-2010

我目前有一个应用程序编写,生成pdf凭证和电子邮件给他们的透视收件人.然而,我使用的功能是客户端相关(MS Outlook),我真的想让这个电子邮件客户端不可知,因为我们有很多客户,而不是所有客户都使用Outlook.

我已经看了几个选项但是在搜索中找不到任何可以解决我的问题的东西.

有没有人知道使用客户smtp连接发送电子邮件的好方法,无论客户端如何,并发送附件,而无需直接调用客户端来执行此操作?

小智 6

或者,您可以使用Synapse库,使用SMTP发送邮件,最好是在其最新快照中.

这里是应该附有发送邮件的代码c:\voucher.pdf从文件sender@from.comrecipient@to.comsmtp.server.com与登录login和密码password.关于本TMimeMess课程的其他功能,我将直接引用您的参考.

我希望这会有效,因为我已经简化并本地化了我正在使用的更复杂的代码,我无法验证它或编译.如果没有,让我们投票吧:)

uses
  SMTPSend, MIMEPart, MIMEMess;

procedure TForm.SendEmailClick(Sender: TObject);
var
  MIMEText: TStrings;
  MIMEPart: TMimePart;
  MIMEMessage: TMimeMess;
begin
  MIMEText := TStringList.Create;
  MIMEText.Add('Hello,');
  MIMEText.Add('here is the text of your e-mail message,');
  MIMEText.Add('if you want the HTML format, use AddPartHTML');
  MIMEText.Add('or e.g. AddPartHTMLFromFile if you have your');
  MIMEText.Add('HTML message content in a file.');

  MIMEMessage := TMimeMess.Create;

  with MIMEMessage do
  try
    Header.Date := Now;
    Header.From := 'sender@from.com';
    Header.ToList.Clear;
    Header.ToList.Add('recipient@to.com');
    Header.CcList.Clear;
    Header.Subject := 'E-mail subject';
    Header.XMailer := 'My mail client name';

    MIMEPart := AddPartMultipart('mixed', nil);

    AddPartText(MIMEText, MIMEPart);
    AddPartBinaryFromFile('c:\voucher.pdf', MIMEPart);

    EncodeMessage;

    if SendToRaw(Header.From,               // e-mail sender
                 Header.ToList.CommaText,   // comma delimited recipient list
                 'smtp.server.com',         // SMTP server
                 Lines,                     // MIME message data
                 'login',                   // server authentication
                 'password')                // server authentication
    then
      ShowMessage('E-mail has been successfuly sent :)')
    else
      ShowMessage('E-mail sending failed :(');
  finally
    Free;
    MIMEText.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)


更新:

根据Downvoter的好评,步入光明(男人,请改变你的昵称,它不再酷了:),如果你将所有收件人的名单发送给每个人,那将是非常糟糕的.使用synapse,您无法将BCC添加到邮件头; 没有Header.BCCList财产MIMEMessage.相反,您可以在发送数据之前直接修改数据.

// First, you will remove the line where you are adding a recipient to the list
Header.ToList.Add('recipient@to.com');

// the rest between you can keep as it is and after the message encoding
EncodeMessage;

// and before sending the mail you'll insert the line with BCCs
Lines.Insert(1, 'Bcc: jane@invisiblecustomer.com, lisa@invisiblecustomer.com');

if SendToRaw ...
Run Code Online (Sandbox Code Playgroud)