Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Mar : Re: Socket Error # 0

www.cryer.info
Managed Newsgroup Archive

Re: Socket Error # 0

Subject:Re: Socket Error # 0
Posted by:"Stephan Jaschke" (ne..@stj-software.de)
Date:Thu, 22 Mar 2007 19:18:28

Remy Lebeau (TeamB) schrieb:
> But you did not show the actual code.

Sorry. It's a bit lengthy but nothing special.

<--- cut here --->

procedure SendEMail(const ARecipients,ACC,ABCC,ASubject: string;
                     ABody,AAttachments: TStrings;
                     AConfig: TTjEMailConfig);

   var
     SMTP: TIdSMTP;
     Msg: TIdMessage;
     i: Integer;

   function ContentType(const Ext: string): string;
     var
       R: TRegistry;
   begin
     Result := 'application/octet-stream';
     R := TRegistry.Create(KEY_READ);
     try
       R.RootKey := HKEY_CLASSES_ROOT;
       if not R.OpenKeyReadOnly(AnsiLowerCase(Ext)) then
         Exit;
       try
         if R.ValueExists('Content Type') then
           Result := R.ReadString('Content Type');
       finally
         R.CloseKey;
       end;
     finally
     end;
   end;

begin
   SMTP := TIdSMTP.Create(nil);
   try
     SMTP.MaxLineAction := maException;
     SMTP.ReadTimeout := 0;
     SMTP.RecvBufferSize := 8192;
     SMTP.Host := AConfig.SMTPHost;
     SMTP.Port := AConfig.SMTPPort;
     SMTP.AuthenticationType := atLogin;
     SMTP.Username := AConfig.Account;
     SMTP.Password := AConfig.Pass;
     Msg := TIdMessage.Create(nil);
     try
       if Assigned(ABody) then
         Msg.Body.Assign(ABody)
       else
         Msg.ClearBody;
       Msg.ContentType := 'text/plain';
       Msg.Encoding := meMIME;
       Msg.AttachmentEncoding := 'MIME';
       Msg.From.Name := AConfig.SenderName;
       Msg.From.Address := AConfig.SenderEMail;
       Msg.Organization := AConfig.SenderOrganization;
       Msg.ReplyTo.EMailAddresses := AConfig.SenderReplyTo;
       Msg.Recipients.EMailAddresses := ARecipients;
       Msg.CCList.EMailAddresses := ACC;
       Msg.BccList.EMailAddresses := ABCC;
       Msg.Subject := ASubject;
       Msg.Priority := mpNormal;
       if Assigned(AAttachments) then begin
         for i := 0 to AAttachments.Count-1 do begin
           if Trim(Trim(AAttachments.Strings[i])) <> '' then begin
             if FileExists(Trim(AAttachments.Strings[i])) then
               TIdAttachment.Create(
                   Msg.MessageParts,Trim(AAttachments.Strings[i])
                 ).ContentType :=
ContentType(ExtractFileExt(Trim(AAttachments.Strings[i])))
             else
               raise
ETjCheck.CreateResFmt(@SErrAttachNotFound,[Trim(AAttachments.Strings[i])]);
           end;
         end;
       end;
       try
         SMTP.Connect;
         try
           SMTP.Send(Msg);
         finally
           SMTP.Disconnect;
         end;
       except
         on E: EIdSocketError do
           case E.LastError of
             10060:  raise
ETjCheck.CreateResFmt(@SErrConnectFailed,[AConfig.SMTPHost]);
             else    raise;
           end;
         on E: EIdProtocolReplyError do
           raise ETjCheck.CreateFmt('Fehler beim
E-Mail-Versand.'+sLineBreak+
                                    'Antwort des Servers: '+sLineBreak+
                                    E.Message,[AConfig.SMTPHost]);
       end;
     finally
       Msg.Free;
     end;
   finally
     SMTP.Free;
   end;
end;

<--- cut here --->

--
Logik ist die Kunst, zuversichtlich in die Irre zu gehen.

Stephan Jaschke - EDV-Beratung und Softwareentwicklung
Mail: support@stj-software.de - ICQ: 361511624

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive