AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Delphi [Indy] Attachement (related und nicht-related) + html + text funktioniert nicht
Thema durchsuchen
Ansicht
Themen-Optionen

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

Ein Thema von Drumbo · begonnen am 10. Jan 2018 · letzter Beitrag vom 12. Jan 2018
Antwort Antwort
HolgerX

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

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

  Alt 12. Jan 2018, 14: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
Drumbo

Registriert seit: 18. Okt 2013
22 Beiträge
 
#2

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

  Alt 12. Jan 2018, 16:18
Hey Holger,

vielen Dank für die Antwort.
Ich glaub ich hab etwas generell falsch Verstanden.
Ich dachte, dass der text/plain-Teil immer angezeigt wird.
Jedoch, wenn ich es denn jetzt richtig verstanden hab, wird der text/plain Teil nur als alternative angezeigt, wenn der html-Part nicht angezeigt werden kann.
Habe den text/plain-Teil nur nie gesehen, weil meine Email Programme Html zulassen.
So zumindest meine Theorie.

Gruß Chris
  Mit Zitat antworten Zitat
Antwort Antwort

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:29 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz