Einzelnen Beitrag anzeigen

HolgerX

Registriert seit: 10. Apr 2006
Ort: Leverkusen
969 Beiträge
 
Delphi 6 Professional
 
#2

AW: [Indy] Attachement (related und nicht-related) + html + text funktioniert nicht

  Alt 12. Jan 2018, 15:25
Hmm..

Hab hier ein Beispiel, wie der Aufbau einer solchen Mail sein sollte.

Das Ergebnis wird von Outlook und Thinderbird korrekt angezeigt.

Delphi-Quellcode:
uses
  idMessage,
  IdMessageParts,
  IdText,
  IdAttachmentFile;

{ EMail mit  Text und HTML+InlineImages und Attachments

  mixed
    alternative
      text
      related
        html
        inline image
        inline image
    attachment
    attachment

https://stackoverflow.com/questions/3902455/mail-multipart-alternative-vs-multipart-mixed

}


procedure TForm1.Button1Click(Sender: TObject);
var
  tmpEMail: TIdMessage;

  tmpHTMLTxt : TStrings;

  tmpBodyPart : TIdText;
  tmpTXTPart : TIdText;

  tmpHTMLRevPart : TIdText;
  tmpHTMLPart : TIdText;
  tmpInlineImagePart : TIdAttachmentFile;

  tmpImageName : string;
  tmpIMGFilename: string;

  tmpPDFName : string;
  tmpPDFFileName : string;

  tmpPDFPart : TIdAttachmentFile;
begin
  tmpImageName := 'Barcode.jpg';
  tmpPDFName := 'Muster.pdf';

  tmpIMGFilename := ExtractFilePath(Application.ExeName) + tmpImageName;
  tmpPDFFileName := ExtractFilePath(Application.ExeName) + tmpPDFName;

  tmpHTMLTxt := TStringList.Create();
  try
    tmpHTMLTxt.Add('<html>');
    tmpHTMLTxt.Add('<head>');
    tmpHTMLTxt.Add('</head>');
    tmpHTMLTxt.Add('<body><h1>Hallo</h1>');
    tmpHTMLTxt.Add('<img border="0" src="cid:' + tmpImageName + '"/></span>');
    tmpHTMLTxt.Add('<br>');
    tmpHTMLTxt.Add('Dies ist ein Bild eines Barcode!</body>');
    tmpHTMLTxt.Add('</html>');

    tmpEMail := TIdMessage.Create(nil);
    try
      // Email-Basisparameter
      tmpEMail.From.Text := 'Mustermann@Mustermann.com';
      tmpEMail.Recipients.EMailAddresses := 'Anderer@Mustermann.com';
      tmpEMail.Subject := 'Hallo';
      tmpEMail.ContentType := 'multipart/mixed';

      // Alternativen (zu PartNr -1 = Mail) (ist Index = 0)
      tmpBodyPart := TIdText.Create(tmpEMail.MessageParts);
      tmpBodyPart.ContentType := 'multipart/alternative';
      tmpBodyPart.ParentPart := -1;

        // Alternative 1 = TEXT Part (zu PartNr 0) (ist Index = 1)
        tmpTXTPart := TIdText.Create(tmpEMail.MessageParts);
        tmpTXTPart.ContentType := 'text/plain';
        tmpTXTPart.ParentPart := tmpBodyPart.Index;
        tmpTXTPart.Body.Text := 'Siehe HTML-Mailpart!';

        // Alternative 2 = HTML Part mit zusätzlichen Teilen = related (zu PartNr 0) (ist Index = 2)
        tmpHTMLRevPart := TIdText.Create(tmpEMail.MessageParts);
        tmpHTMLRevPart.ContentType := 'multipart/related';
        tmpHTMLRevPart.ParentPart := tmpBodyPart.Index;

          // HTML Part (zu PartNr 2) (ist Index = 3)
          tmpHTMLPart := TIdText.Create(tmpEMail.MessageParts, tmpHTMLTxt);
          tmpHTMLPart.ContentType := 'text/html';
          tmpHTMLPart.ParentPart := tmpHTMLRevPart.Index;

          // InlineImage 1 (zu PartNr 2) (ist Index = 4)
          tmpInlineImagePart := TIdAttachmentFile.Create(tmpEMail.MessageParts, tmpIMGFilename);
          tmpInlineImagePart.ContentType := 'image/jpeg';
          tmpInlineImagePart.FileIsTempFile := False;
          tmpInlineImagePart.ContentDisposition := 'inline';
          // Indy 10
          tmpInlineImagePart.ContentID := tmpImageName;
          // Indy 9
// tmpInlineImagePart.ExtraHeaders.Values['Content-ID'] := tmpImageName;
          tmpInlineImagePart.DisplayName := tmpImageName;
          tmpInlineImagePart.ParentPart := tmpHTMLRevPart.Index;


      // Weitere Anhänge (zu PartNr -1 = Mail) (ist PartNr = 5 -)
      tmpPDFPart := TIdAttachmentFile.Create(tmpEMail.MessageParts, tmpPDFFileName);
      tmpPDFPart.FileName := tmpPDFName;
      tmpPDFPart.ParentPart := -1;

      // Speichern oder Versenden
      tmpEMail.SaveToFile(ExtractFilePath(Application.ExeName) + 'Test.eml');
    finally
      tmpEmail.Free;
    end;
  finally
    tmpHTMLTxt.Free;
  end;
end;
  Mit Zitat antworten Zitat