我尝试发送带有附件的邮件。邮件没有附件就到达了收件人。我在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<[email protected]>'), 'utf-8').ContentTransfer := '8bit';
Parametri.AddFormField('to','[email protected]');
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。
每个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
,并让TIdMessage
为您处理MIME格式,例如:] >
IdSMTP1.Host := 'smtp.mailgun.org'; IdSMTP.Username := 'api'; IdSMTP.Password := 'my pass'; IdMessage1.Clear; IdMessage1.From.Name := 'Excited user'; IdMessage1.From.Address := '[email protected]'; IdMessage1.Recipients.EmailAddresses := '[email protected]'; 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;
或:
IdSMTP1.Host := 'smtp.mailgun.org';
IdSMTP.Username := 'api';
IdSMTP.Password := 'my pass';
IdMessage1.Clear;
IdMessage1.From.Name := 'Excited user';
IdMessage1.From.Address := '[email protected]';
IdMessage1.Recipients.EmailAddresses := '[email protected]';
IdMessage1.Subject := 'xy: sub...';
with TIdMessageBuilderPlain.Create do
try
PlainText := 'Here is my text...';
PlainTextCharSet := 'utf-8';
PlainTextContentTransfer := '8-bit';
Attachments.Add('c:\ABC.pdf');
FillMessage(IdMessage1);
finally
Free;
end;
IdSMTP1.Connect;
try
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;