2017-07-25 151 views
0

首先,編碼。用Indy發送帶有附件的電子郵件

procedure TMain.SendEmailIndy(
    const SMTPServer: string; 
    const FromName, FromAddress: string; 
    const ToAddresses: string; //comma "," separated list of e-mail addresses 
    const CCAddresses: string; //comma "," separated list of e-mail addresses 
    const BCCAddresses: string; //comma "," separated list of e-mail addresses 
    const Subject: string; 
    const EmailBody: string; 
    const IsBodyHtml: Boolean); 
    var 
     smtp: TIdSMTP; // IdSmtp.pas 
     msg: TidMessage; // IdMessage.pas 
     builder: TIdCustomMessageBuilder; //IdMessageBuilder.pas 
     s: string; 
     emailAddress: string; 
     FileToSend: TIdAttachmentfile; 
begin 
    msg := TidMessage.Create(nil); 

    try 
     if IsBodyHtml then begin 
      builder := TIdMessageBuilderHtml.Create; 
      TIdMessageBuilderHtml(builder).Html.Text := EmailBody 
     end else begin 
      builder := TIdMessageBuilderPlain.Create; 
     end; 

     if (Realization.AttachmentD.FileName <> '') then begin 
      msg.IsEncoded := true; 
      FileToSend := TIdAttachmentFile.Create(msg.MessageParts, Realization.AttachmentD.FileName); 
      FileToSend.FileName := Realization.AttachmentD.FileName; 
      //FileToSend.ContentDisposition := 'attachment'; 
      FileToSend.ContentType := 'multipart/mixed'; 
      ShowMessage('Sent: '+Realization.AttachmentD.FileName); 
     end; 

     msg.From.Name := FromName; 
     msg.From.Address := FromAddress; 
     msg.Subject := Subject; 

     //If the message is plaintext then we must fill the body outside of the PlainText email builder. 
     //(the PlainTextBuilder is unable to build plaintext e-mail) 
     if not IsBodyHtml then begin 
      msg.Body.Text := EmailBody; 
     end; 

     for s in ToAddresses.Split([',']) do 
     begin 
      emailAddress := Trim(s); 
      if emailAddress <> '' then begin 
        with msg.recipients.Add do 
        begin 
        //Name := '<Name of recipient>'; 
        Address := emailAddress; 
        end; 
      end; 
     end; 

     for s in CCAddresses.Split([',']) do 
     begin 
      emailAddress := Trim(s); 
       if emailAddress <> '' then 
        msg.CCList.Add.Address := emailAddress; 
     end; 

     for s in BCCAddresses.Split([',']) do 
     begin 
      emailAddress := Trim(s); 
       if emailAddress <> '' then 
        msg.BccList.Add.Address := emailAddress; 
     end; 

    smtp := TIdSMTP.Create(nil); 
    try 
     msg.Encoding := meMIME; 
     msg.ContentType := 'text/html'; 
     msg.CharSet := 'UTF-8'; 
     msg.ContentTransferEncoding:= 'quoted-printable'; 

     smtp.Host := SMTPServer; // IP Address of SMTP server 
     Smtp.UseTLS := utNoTLSSupport; 
     smtp.Port := 587; //The default already is port 25 (the SMTP port) 
     smtp.Username := _GlobalData.EMail; 
     smtp.Password := _GlobalData.Password; 
     smtp.Connect; 

     try 
      smtp.Send(msg); 
      ShowMessage('Wiadomość wysłana.'); 
      Realization.AttachmentD.FileName := ''; 
     finally 
      smtp.Disconnect(); 
     end; 
    finally 
     smtp.Free; 
    end; 
finally 
    msg.Free; 
    end; 
end; 

我發送帶附件的電子郵件時遇到問題。

當我刪除從上面的代碼下面的行,將消息而不HTML郵件(電子郵件體)應該有發送:

FileToSend.ContentType := 'multipart/mixed'; 

然而,當我離開這條線中的代碼並嘗試發送消息,我收到此消息:

A policy-violation was found in an Email message you sent. 
This Email scanner intercepted it and stopped the entire message 
reaching its destination. 

The policy-violation was reported to be: 

Disallowed breakage found in header name - not valid email 


Please contact your IT support personnel with any queries regarding this 
policy. 

因此,我的問題是,如何正確發送附加文件的電子郵件。

回答

0

您濫用TIdMessageBuilder...類(TIdMessageBuilderHtml是完全能夠創建純文本電子郵件,但更重要的是你是不是叫TIdCustomMessageBuilder.FillMessage()的建設者數據傳輸到TIdMessage)。

您沒有正確填充TIdMessage(例如,當附件存在時,您沒有正確設置TIdMessage.ContentTypeTIdAttachmentFile.ContentType屬性)。

嘗試一些更喜歡這個:

procedure TMain.SendEmailIndy(
    const SMTPServer: string; 
    const FromName, FromAddress: string; 
    const ToAddresses: string; //comma separated list of e-mail addresses 
    const CCAddresses: string; //comma separated list of e-mail addresses 
    const BCCAddresses: string; //comma separated list of e-mail addresses 
    const Subject: string; 
    const EmailBody: string; 
    const IsBodyHtml: Boolean); 
var 
    smtp: TIdSMTP; // IdSmtp.pas 
    msg: TidMessage; // IdMessage.pas 
    builder: TIdMessageBuilderHtml; //IdMessageBuilder.pas 
begin 
    msg := TidMessage.Create(nil); 
    try 
    builder := TIdMessageBuilderHtml.Create; 
    try 
     if IsBodyHtml then 
     begin 
     builder.Html.Text := EmailBody; 
     builder.HtmlCharSet := 'utf-8'; 
     builder.HtmlContentTransfer := 'quoted-printable'; 
     end else 
     begin 
     builder.PlainText.Text := EmailBody; 
     builder.PlainTextCharSet := 'utf-8'; 
     builder.PlainTextContentTransfer := 'quoted-printable'; 
     end; 

     if Realization.AttachmentD.FileName <> '' then 
     begin 
     builder.Attachments.Add(Realization.AttachmentD.FileName); 
     ShowMessage('Sending: ' + Realization.AttachmentD.FileName); 
     end; 

     builder.FillMessage(msg); 
    finally 
     builder.Free; 
    end; 

    msg.From.Name := FromName; 
    msg.From.Address := FromAddress; 
    msg.Subject := Subject; 

    msg.Recipients.EmailAddresses := ToAddresses; 
    msg.CCList.EmailAddresses := CCAddresses; 
    msg.BccList.EmailAddresses := BCCAddresses; 

    smtp := TIdSMTP.Create(nil); 
    try 
     smtp.Host := SMTPServer; // IP Address of SMTP server 
     Smtp.UseTLS := utNoTLSSupport; 
     smtp.Port := 587; //The default already is port 25 (the SMTP port) 
     smtp.Username := _GlobalData.EMail; 
     smtp.Password := _GlobalData.Password; 
     smtp.AuthType := satDefault; 

     smtp.Connect;  
     try 
     smtp.Send(msg); 
     finally 
     smtp.Disconnect; 
     end; 
    finally 
     smtp.Free; 
    end; 
    finally 
    msg.Free; 
    end; 

    ShowMessage('Wiadomość wysłana.'); 
    Realization.AttachmentD.FileName := ''; 
end; 
相關問題