通过带有delphi附件的mailgun发送邮件

问题描述 投票:0回答:1

我尝试发送带有附件的邮件。邮件没有附件就到达了收件人。我在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。

http delphi email-attachments mailgun
1个回答
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,因此您可以使用TIdSMTPTIdMessage代替使用TIdHTTPTIdMultipartFormDataStream,并让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;
© www.soinside.com 2019 - 2024. All rights reserved.