Thema: Delphi Mail Senden über Indy

Einzelnen Beitrag anzeigen

Benutzerbild von CTV
CTV

Registriert seit: 20. Jan 2004
Ort: Zug CH
83 Beiträge
 
Delphi 7 Professional
 

Mail Senden über Indy

  Alt 21. Jan 2004, 10:34
Mit dieser Funktion können E-Mails versendet werden Es werden keine Komponenten auf der Form Benötigt (die werden dynmisch erzeugt) Jedoch müssen die Indy Komponenten installiert sein und die uses für die Indy Komponenten eingetragen sein.

Hinweis
Es ist von Vorteil wenn man eine Funktion hat welche RTF in HTML konvertieren kann, denn dann kann man ganz einfach ein Richedit zu HTML wandeln und dann den HTML Quelltext im Mail mit senden.

Paramenter:
Delphi-Quellcode:
an: Tstringlist //Enthält die Empfänger E-Mail Adressen
att1: Tstrings //Enthält die Dateipfade zu den Attachments
Nachricht: Trichedit //Enthält die Mail Nachricht
vonMail: String //Enhält die Absender E-Mail Adresse
Betreff: String //Enhälst den Mail Betreff
Priority: String //Gibt die Mail Priorität an Werte:[mpHighest, mpHigh, mpNormal, mpLow, mpLowest]
CT: String //Content Type des Mails
SMTPServer: String //Zu verwendender SMTP Server
SMTPUsername: String //SMTP Username (meist identisch zu Pop Username)
SMTPPass: String //SMTP Passwort (meist identisch zu Pop Passwort)
SMTPPort: Integer //SMTP Port (25
SmtpAuthType: Integer //0=Normal 1=SMTPAuth 2=AfterPop 3=SMPTAuth und AfterPop
POPServer: String //Zu verwendender POP Server
POPUser: String //POP Username
POPPass: String //POP Passwort
POPPort: Integer //POP Port (110)
Delphi-Quellcode:
procedure sendNewsLetter(an: Tstringlist; att1: Tstrings; Nachricht: Trichedit;
vonMail, Betreff, Priority, CT, SMTPServer, SMTPUsername, SMTPPass: String;
SMTPPort, SmtpAuthType: integer; PopServer, PopUser, PopPass: String; PopPort: integer);
var
  IdMsgSend: TidMessage;
  SMTP: TidSmtp;
  POP: TidPop3;
  i: integer;
  s: string;
begin
  IdMsgSend := TidMessage.Create(nil);
  SMTP := TidSmtp.Create(nil);
  POP := TidPop3.create(nil);
  IdMsgSend.Clear;

  // Plain Text
  with TIdText.Create(IdMsgSend.MessageParts, nil) do
  begin
    ContentType := 'text/plain';
    Body.Text := Nachricht.Text;
  end;

  // HTML Part
  with TIdText.Create(IdMsgSend.MessageParts, nil) do
  begin
    ContentType := 'text/html';
    Body.Text := RtfToHtml('MetaHead', Nachricht); //Benötigt funktion um RTF zu HTML umzuwandeln
  end;

  with IdMsgSend do
  begin
    ContentType := CT;
    From.Text := vonMail;
    ReplyTo.EMailAddresses := vonMail;
    Subject := Betreff;
    Priority := Priority ;
    s := '';
    for i := 0 to an.Count-1 do
    begin
      s := s + BccList.EMailAddresses+an.Strings[i] + ';'
    end;
    BccList.EMailAddresses := s;
    ReceiptRecipient.Text := '';
  end;

  if att1.Count >= 1 then
  begin
    for i := 0 to att1.Count - 1 do
    begin
      TIdAttachment.Create(IdMsgSend.MessageParts, att1.Strings[i] );
    end;
  end;
  IdMsgSend.ContentType := CT ;

  case SmtpAuthType of
    0: SMTP.AuthenticationType := atNone; //Normal
    1: SMTP.AuthenticationType := atLogin; //SMTPAuth
    2: begin //AfterPop
         SMTP.AuthenticationType := atNone;
         POP.Host := POPServer;
         POP.Username := POPUser;
         POP.Password := POPPass;
         POP.Port := POPPort;
         POP.Connect(5);
         POP.Disconnect;
       end;
    3: begin //afterPop+SMTPAuth
         SMTP.AuthenticationType := atLogin;
         POP.Host := POPServer;
         POP.Username := POPUser;
         POP.Password := POPPass;
         POP.Port := POPPort;
         POP.Connect(5);
         POP.Disconnect;
       end;
  end;
  SMTP.Username := SMTPUsername;
  SMTP.Password := SMTPPass;

  SMTP.Host := SMTPServer;
  SMTP.Port := SMTPPort;

  SMTP.Connect;

  ShowMessage(IntToStr(IdMsgSend.MessageParts.Count));
  try
    SMTP.Send(IdMsgSend);
  finally
    SMTP.Disconnect;
  end;
  IdMsgSend.free;
  SMTP.free;
  POP.free;
end;
Beispiel:
Delphi-Quellcode:
sendNewsLetter(an, Formatt.ListBoxFiles.items, editor, 'Absender_EMail', EditBetreff.text, 'mpnormal', 'text/html',
  'SmtpServer', 'SmtpBenutzername', 'SmtpPasswort', 25, 0, 'PopServer', 'PopBenutzername', 'PopPasswort', 110));
[edit=Matze]Code formatiert. Mfg, Matze[/edit]
[edit=Chakotay1308]Delphi-Tags korrigiert. Mfg, Chakotay1308[/edit]
  Mit Zitat antworten Zitat