我尝试发送带有附加附件的邮件。邮件没有附件就到达了收件人。我在delphi(indy)中使用api访问。谁能帮我 ?我的代码:
try
IdHTTP1.Request.CharSet := 'utf-8';
IdHTTP1.Request.ContentType := 'multipart/form-data';
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := 'api';
IdHTTP1.Request.Password := 'my pass';
Parametri := TIdMultiPartFormDataStream.Create;
Parametri.AddFormField('from', UTF8Encode('Excited user<excited@sandbox.mailgun.org>'), 'utf-8').ContentTransfer := '8bit';
Parametri.AddFormField('to','to@mail.com');
Parametri.AddFormField('subject', UTF8Encode('xy: sub...'), 'utf-8').ContentTransfer := '8bit';
Parametri.AddFormField('text', UTF8Encode('Here is my text...'), 'utf-8').ContentTransfer := '8bit';
Parametri.AddFile('ABC.pdf', 'c:\ABC.pdf', '');
Memo1.Text := IdHTTP1.Post( 'https://api.mailgun.net/v3/sandbox.mailgun.org/messages', Parametri);
finally
Parametri.Free;
end;
谢谢
b。
答案 0 :(得分:0)
每个MailGun API documentation都需要在一个名为'attachment'
的MIME字段中发送每个电子邮件附件。但是,在调用AddFile()
时,您并不是在命名字段'attachment'
,而是在命名为'ABC.pdf'
。
更改此行:
Parametri.AddFile('ABC.pdf', 'c:\ABC.pdf', '');
为此:
Parametri.AddFile('attachment', 'c:\ABC.pdf');
或者,文档还指出MailGun支持SMTP,因此您可以使用TIdSMTP
和TIdMessage
而不是TIdHTTP
和TIdMultipartFormDataStream
,然后让{{1} }为您处理MIME格式,例如:
TIdMessage
或者:
IdSMTP1.Host := 'smtp.mailgun.org';
IdSMTP.Username := 'api';
IdSMTP.Password := 'my pass';
IdMessage1.Clear;
IdMessage1.From.Name := 'Excited user';
IdMessage1.From.Address := 'excited@sandbox.mailgun.org';
IdMessage1.Recipients.EmailAddresses := 'to@mail.com';
IdMessage1.Subject := 'xy: sub...';
IdMessage1.ContentType := 'multipart/mixed';
with TIdText.Create(IdMessage1.MessageParts, nil) do
begin
Body.Text := 'Here is my text...';
ContentType := 'text/plain';
CharSet := 'utf-8';
ContentTransfer := '8-bit';
end;
TIdAttachmentFile.Create(IdMessage1.MessageParts, 'c:\ABC.pdf');
IdSMTP1.Connect;
try
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;