Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi [Indy] Attachement (related und nicht-related) + html + text funktioniert nicht (https://www.delphipraxis.net/194816-%5Bindy%5D-attachement-related-und-nicht-related-html-text-funktioniert-nicht.html)

Drumbo 10. Jan 2018 14:32

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

ich suche schon seit mehreren Stunden nach einer Lösung für mein Indy-Email Problem.
Und zwar möchte ich zum einen eine Email schicken, welche eine PDF im Anhang hat, welcher auch als solcher angezeigt werden soll!.
Des Weiteren möchte ich aber auch eine Html-Signatur verschicken, in der ein Logo drin ist, welches wiederum nicht in den Anhängen auftauchen soll und zu guter letzt möchte ich auch noch 1-2 Sätze "normalen Text" hinzufügen.
Bis auf den normalen Text funktioniert auch alles problemlos.
Auf diesen kann ich allerdings nicht verzichten.
Untenstehend findet ihr den Code.
Die Geschichte mit den MessageParts hab ich von der Indy Seite (http://www.indyproject.org/Sockets/B...8_17_A.EN.aspx).
Wenn ich alle Parentparts wie auf der Seite beschrieben nutze, bleibt mein Programm hängen.

Delphi-Quellcode:
procedure TfrmMessageEditor.CreateAndSendEmail(MailTo:String;ExtSubject:String;AttachmentPath:string;BodyText:tstrings;contenttype:string);
var
  lTextPart: TIdText;
  TechnikSignatur:string;
begin

IdMsgSend:=TIdMessage.Create(nil); // Erstellen der Message
// Html Datei für individuelle HTML-Signaturen für diverse Techniker
TechnikSignatur:='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
TechnikSignatur:=TechnikSignatur+'</head><body><table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="padding-left: 0px; padding-top: 0px; padding-bottom: 0px; padding-right: 0px;">';
TechnikSignatur:=TechnikSignatur+'<span style="text-align: left; color: #000000; font-family: ''Arial'', sans-serif; font-size: 10pt; font-weight: bold"> ';
TechnikSignatur:=TechnikSignatur+trim(Main.Techniker_Anmelde_datasetVorname.Value)+' '+trim(Main.Techniker_Anmelde_datasetName.Value)+'</span><br><span style="text-align: left; margin-top: 0px; margin-bottom: 0px; color: #000000; font-family: ''Arial'', sans-serif;' ;
TechnikSignatur:=TechnikSignatur+'font-weight: normal; font-size: 9pt;"> '+trim(Main.Techniker_Anmelde_datasetTitel.value)+'</span><br></td></tr>';
//PDF Anhang generieren falls notwendig
    if AttachmentPath<>'' then
        begin
          with TIdAttachmentFile.Create(IdMsgSend.MessageParts, AttachmentPath) do
          begin
            FileName:= ExtSubject+'.pdf';
          end;
        end;

//IDMessage konfigurieren
     with IdMsgSend do
      begin
         Body.Clear;
         From.Text := Main.Techniker_Anmelde_datasetemail.Value;
         ReplyTo.EMailAddresses := Main.Techniker_Anmelde_datasetemail.Value;
         Recipients.EMailAddresses := MailTo; { To: header }
         Subject := ExtSubject; { Subject: header }
         Priority := TIdMessagePriority(cboPriority.ItemIndex); { Message Priority }
         CCList.EMailAddresses := edtCC.Text; {CC}
         BccList.EMailAddresses := edtBCC.Text; {BBC}
         ReceiptRecipient.Text := Main.Techniker_Anmelde_datasetemail.Value;
      end;
// Verschiedene MessageParts erstellen (wie auf http://www.indyproject.org/Sockets/Blogs/RLebeau/2005_08_17_A.EN.aspx beschrieben)
     with TIdText.Create(IdMsgSend.MessageParts, nil) do begin
      ContentType := 'multipart/related; type="multipart/alternative"';
    end;
    with TIdText.Create(IdMsgSend.MessageParts, nil) do begin
      ContentType := 'multipart/alternative';
    end;
    with TIdText.Create(IdMsgSend.MessageParts, nil) do begin
      Body.text:='TestTestTest';
      ContentType := 'text/plain';
    end;
    with TIdText.Create(IdMsgSend.MessageParts, nil) do begin
      Body.Text := TechnikSignatur+Options_form.Option_datasetMailSignature.Value;
      ContentType := 'text/html';
    end;

  if not FileExists(ExtractFilePath(ParamStr(0))+'Logo.gif') then
    Options_form.DBAdvPicture1.Picture.SaveToFile(ExtractFilePath(ParamStr(0))+'Logo.gif');
  With TIdAttachmentFile.Create(IdMsgSend.MessageParts,ExtractFilePath(ParamStr(0))+'Logo.gif' ) do begin
      ContentID := 'image1';
      ContentType := 'image/gif';
      FileName := 'Logo.gif';
    end;
   IdMsgSend.ContentType := 'multipart/mixed';
  {authentication settings}
   SMTP.AuthType := satdefault; {Simple Login}

   SMTP.Username := Main.Techniker_Anmelde_datasetemail.Value;
   SMTP.Password := Main.Techniker_Anmelde_datasetMailPasswort.Value;

   {General setup}
   SMTP.Host := main.Techniker_Anmelde_datasetsmtphost.Value;
   SMTP.Port := main.Techniker_Anmelde_datasetsmtpport.value;

   {now we send the message}
   SMTP.Connect;
   try
      SMTP.Send(IdMsgSend);
   finally
      SMTP.Disconnect;
   end;
   IdMsgSend.free;
end;
Ich hoffe ihr könnt mir helfen.

Gruß Chris

HolgerX 12. Jan 2018 14:25

AW: [Indy] Attachement (related und nicht-related) + html + text funktioniert nicht
 
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;

Drumbo 12. Jan 2018 16:18

AW: [Indy] Attachement (related und nicht-related) + html + text funktioniert nicht
 
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


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz