或者您可以使用Synapse 库来使用 SMTP 发送邮件,最好是在其newest snapshot 中。
这里是应该使用登录login 和密码password 将邮件从sender@from.com 到recipient@to.com 发送到smtp.server.com 的附加c:\voucher.pdf 文件的代码。关于 TMimeMess 类中的其余函数,我会直接将您推荐给 the reference。
我希望这会起作用,因为我已经简化并本地化了我正在使用的更复杂的代码,但我无法验证它也无法编译。如果没有,让我们投反对票:)
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;
更新:
根据Downvoter step into the light 的好评论(伙计,请更改您的昵称,它不再酷了 :),如果您将所有收件人的列表发送给每个人,那将是非常糟糕的。使用突触you cannot 将密件抄送添加到消息头; MIMEMessage 中没有 Header.BCCList 属性。
相反,您可以在发送数据之前直接修改数据。
// 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 ...