另外搜索到一段代码,贴到下面,供大家参考:
利用Indy组件发送HTML格式的多信息邮件
uses
idMessage;
procedure TForm1.Button1Click(Sender: TObject);
var
html: TStrings;
htmpart, txtpart: TIdText;
bmppart: TIdAttachment;
email: TIdMessage;
filename: string;
begin
filename := ExtractFilePath(Application.ExeName) + 'YouImg.jpg';
html := TStringList.Create();
html.Add('<html>');
html.Add('<head>');
html.Add('</head>');
html.Add('<body><h1>Hello</h1>');
html.Add('<img src="cid:YouImg.jpg" />');
html.Add('This is a picture!</body>');
html.Add('</html>');
email := TIdMessage.Create(nil);
email.From.Text := 'you@from.com';
email.Recipients.EMailAddresses := 'my@mail.com';
email.Subject := 'Hello';
email.ContentType := 'multipart/mixed';
email.Body.Assign(html);
txtpart := TIdText.Create(email.MessageParts);
txtpart.ContentType := 'text/plain';
txtpart.Body.Text := '';
htmpart := TIdText.Create(email.MessageParts, html);
htmpart.ContentType := 'text/html';
bmppart := TIdAttachment.Create(email.MessageParts, filename);
bmppart.ContentType := 'image/jpeg';
bmppart.FileIsTempFile := true;
bmppart.ContentDisposition := 'inline';
bmppart.ExtraHeaders.Values['content-id'] := 'YouImg.jpg';
bmppart.DisplayName := 'YouImg.jpg';
try
idSMTP.Connect();
try
idSMTP.Send(email);
ShowMessage('Sent');
except
on E: Exceptiondo
ShowMessage('Failed: ' + E.Message);
end;
finally
idSMTP.Disconnect();
email.Free();
html.Free();
end;
end;