如何在HTML格式的电子邮件中发送附件?
我设法使用HTML格式的stmp.gmail.com发送电子邮件,但我的电子邮件附件。附件被转换为字符串,但我需要它是原始文件格式。
代码:
with FIdSmtp do
begin
// Configure IdSMTP
end;
FIdMessage.Clear;
FIdMessage.MessageParts.Clear;
FIdMessage.From.Address := 'EmailRemetente';
FIdMessage.From.Name := 'Apelido';
FIdMessage.Subject := 'Assunto';
FIdMessage.Body.Text := 'Mensagem';
FIdMessage.Recipients.EMailAddresses := 'aEmailDestinatario'; //email destino
FIdMessage.BccList.EMailAddresses := StringReplace(Trim(BCCList.Text),#13#10,',',[rfReplaceAll]);
FIdMessage.ccList.EMailAddresses := StringReplace(Trim(CCList.Text),#13#10,',',[rfReplaceAll]);
FIdMessage.ContentType := 'text/html'; // set html format
_Anexo := TStringList.Create;
try
_Anexo.Text := aAnexos;
for i := 0 to _Anexo.Count - 1 do
TIdAttachmentFile.Create(FIdMessage.MessageParts,_Anexo.Strings[i]);
finally
_Anexo.Free
end;
FIdSmtp.Send(FIdMessage);
有人可以帮助我吗?
答案 0 :(得分:4)
在Indy的网站上阅读我的博客,我详细讨论了这个主题:
New HTML Message Builder class
考虑到这一点,试试这个:
FIdMessage.Clear;
FIdMessage.From.Address := 'EmailRemetente';
FIdMessage.From.Name := 'Apelido';
FIdMessage.Subject := 'Assunto';
FIdMessage.Recipients.EMailAddresses := 'aEmailDestinatario';
FIdMessage.BccList.EMailAddresses := StringReplace(Trim(BCCList.Text),sLineBreak,',',[rfReplaceAll]);
FIdMessage.ccList.EMailAddresses := StringReplace(Trim(CCList.Text),sLineBreak,',',[rfReplaceAll]);
FIdMessage.ContentType := 'multipart/mixed';
with TIdText.Create(FIdMessage.MessageParts, nil) do
begin
ContentType := 'text/html';
Body.Text := 'Mensagem';
end;
_Anexo := TStringList.Create;
try
_Anexo.Text := aAnexos;
for i := 0 to _Anexo.Count - 1 do
TIdAttachmentFile.Create(FIdMessage.MessageParts, _Anexo.Strings[i]);
finally
_Anexo.Free
end;
或者这个:
FIdMessage.Clear;
with TIdMessageBuilderHtml.Create do
try
Subject := 'Assunto';
Html.Text := 'Mensagem';
_Anexo := TStringList.Create;
try
_Anexo.Text := aAnexos;
for i := 0 to _Anexo.Count - 1 do
Attachments.Add(_Anexo.Strings[i]);
finally
_Anexo.Free
end;
FillMessage(FIdMessage);
finally
Free;
end;
FIdMessage.From.Address := 'EmailRemetente';
FIdMessage.From.Name := 'Apelido';
FIdMessage.Recipients.EMailAddresses := 'aEmailDestinatario';
FIdMessage.BccList.EMailAddresses := StringReplace(Trim(BCCList.Text),sLineBreak,',',[rfReplaceAll]);
FIdMessage.ccList.EMailAddresses := StringReplace(Trim(CCList.Text),sLineBreak,',',[rfReplaceAll]);