procedure TForm1.Button1Click(Sender: TObject);
var
sl_text, sl_html: TStringList;
MimeMsg: TMimeMess;
SMTPSend: TSMTPSend;
MIMEPart, MIMEPart2, HTMLPart: TMimePart;
i: Integer;
begin
sl_text := TStringList.Create;
sl_text.Text := 'Hello world';
sl_html := TStringList.Create;
sl_html.Text := '<html><body><font size=7><b>Hello</b></font> <i>world</i><br />Zażółć gęślą jaźń</body></html>';
MimeMsg := TMimeMess.Create;
SMTPSend := TSMTPSend.Create;
try
MimeMsg.Header.From := 'Test <test@example.com>';
MimeMsg.Header.ToList.Add('user1@another-domain.com');
MimeMsg.Header.ToList.Add('user2@another-domain.com');
MimeMsg.Header.CCList.Add('copy@another-domain.com');
MimeMsg.Header.Subject := 'Test message';
MimeMsg.Header.Priority := MP_high;
MimeMsg.Header.CustomHeaders.Add('Disposition-Notification-To: ' + MimeMsg.Header.From); //read confirmation
MimeMsg.Header.Date := Now;
MimeMsg.Header.XMailer := 'lazarus';
MimeMsg.Header.CharsetCode := UTF_8;
MimeMsg.Header.ReplyTo := 'Test <test@example.com>';
MIMEPart := MimeMsg.AddPartMultipart('related', nil);
MIMEPart.CharsetCode := UTF_8;
HTMLPart := MimeMsg.AddPart(MIMEPart);
HTMLPart.ConvertCharset := False;
with HTMLPart do
begin
sl_html.SaveToStream(DecodedLines);
Primary := 'text';
Secondary := 'html';
Description := 'HTML text';
Disposition := 'inline';
CharsetCode := UTF_8;
EncodingCode := ME_QUOTED_PRINTABLE;
EncodePart;
EncodePartHeader;
end;
MIMEPart2 := MimeMsg.AddPartMultipart('alternative', MIMEPart);
MIMEPart2.CharsetCode := UTF_8;
MimeMsg.AddPartTextEx(sl_text, MIMEPart2, UTF_8, True, ME_8BIT); //add alternative text message
MimeMsg.EncodeMessage;
SMTPSend.TargetHost := 'smtp.example.com';
SMTPSend.TargetPort := '587';
SMTPSend.UserName := 'test@example.com';
SMTPSend.Password := 'myemailpassword';
SMTPSend.AutoTLS := False;
SMTPSend.FullSSL := False;
if SMTPSend.Login then
begin
if SMTPSend.AuthDone then
begin
SMTPSend.MailFrom(email, Length(MimeMsg.Lines.Text));
for i := 0 to MimeMsg.Header.ToList.Count - 1 do //TO
SMTPSend.MailTo(MimeMsg.Header.ToList[i]);
for i := 0 to MimeMsg.Header.CCList.Count - 1 do //CC
SMTPSend.MailTo(MimeMsg.Header.CCList[i]);
SMTPSend.MailTo('admin@example.com'); //BCC
if SMTPSend.MailData(MimeMSg.Lines) then
begin
SMTPSend.Logout;
Caption := 'ok';
end
else
Caption := 'message error: ' + SMTPSend.ResultString;
end
else
Caption := 'auth error: ' + SMTPSend.ResultString;
end
else
Caption := 'login error: ' + SMTPSend.ResultString;
finally
FreeAndNil(MimeMsg);
FreeAndNil(SMTPSend);
end;
sl_text.Free;
sl_html.Free;
end;