Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    [C#] SmtpClient Connect e Disconnect

    Salve,
    in un invio massivo di E-mail tipo 300.000 tramite il Software che ho sviluppato, avrei la necessita di Connettere e Disconnetere ad ogni LOOP,

    cioè dovrebbe funzionare cosi:

    codice:
    SmtpClient smtp = new SmtpClient("smtp.domino.it", 25);
    
    foreach (DataRow emailDR in emailDt.Rows)
    {
         // *** Connect ***
         msg.To.Add(new MailAddress(emailDR["sEmail"], emailDR["sNomeVis"]));
         smtp.Send(msg);
         // *** Disconnect ***
    }
    solo ceh non esistono i metodi "Connect e Disconnect" nella Classe "SmtpClient".

    Come posso fare ???

    Creo un Istanza di "SmtpClient"e la Distruggo ad ogni LOOP ???

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    Connessione e disconessione è a carico del metodo Send.
    Cosa dovresti fare?
    I got the remedy

  3. #3
    Il tecnico del Server SMTP mi ha detto che nel LOG di Sistema si vede ceh la mia connessione rimane Aperta per tutte le ...mila... e-mail ceh invio, invece lui mi consiglia di Aprire e Chiudere la Connesione ad ogni Invio quindi ad ogni LOOP.

  4. #4
    Moderatore di Windows e software L'avatar di URANIO
    Registrato dal
    Dec 1999
    residenza
    Casalpusterlengo (LO)
    Messaggi
    1,290
    Devi usare .Dispose, rilascerà anche tutte le risorse

  5. #5
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    mi sembra strano, cmq prova col dispose oppure using(che è la stessa cosa)
    I got the remedy

  6. #6
    Utente di HTML.it L'avatar di alpynet
    Registrato dal
    Mar 2010
    Messaggi
    123
    Originariamente inviato da w_t
    Il tecnico del Server SMTP mi ha detto che nel LOG di Sistema si vede ceh la mia connessione rimane Aperta per tutte le ...mila... e-mail ceh invio, invece lui mi consiglia di Aprire e Chiudere la Connesione ad ogni Invio quindi ad ogni LOOP.
    Ciao, ma per quale motivo ti consiglia di Aprire e Chiudere la connessione ad ogni invio? Penso che sia normale che la connessione rimanga aperta fintanto che non hai finito di inviare le email. Sarebbe una perdita di tempo/risorse se ogni volta deve autenticarsi... eventualmente appena finito di inviare tutte le email fai un dispose dell'oggetto.

  7. #7
    Originariamente inviato da alpynet
    Ciao, ma per quale motivo ti consiglia di Aprire e Chiudere la connessione ad ogni invio? Penso che sia normale che la connessione rimanga aperta fintanto che non hai finito di inviare le email. Sarebbe una perdita di tempo/risorse se ogni volta deve autenticarsi... eventualmente appena finito di inviare tutte le email fai un dispose dell'oggetto.
    perchè dato che il mio Cliente deve inviare 300.000 e-mail, considerando 1/sec. la connessione rimane aperta per 85 ore circa.

    in ogni caso l'eccezione è la seguente:

    codice:
    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.
    
    ************** Exception Text **************
    System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to write data to the transport connection: Connessione in corso interrotta forzatamente dall'host remoto. ---> System.Net.Sockets.SocketException: Connessione in corso interrotta forzatamente dall'host remoto
       at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
       at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       --- End of inner exception stack trace ---
       at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.Mime.SevenBitStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.Mime.SevenBitStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
       at System.Net.Base64Stream.FlushInternal()
       at System.Net.Base64Stream.Close()
       at System.Net.Mime.MimePart.Send(BaseWriter writer)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
       at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at Gladiator_Mail.Invio.btnInvia_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    Gladiator Mail
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Gladiator%20Mail/bin/Debug/Gladiator%20Mail.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Data
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------
    System.EnterpriseServices
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Numerics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.0.30319.1 built by: RTMRel
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
    ----------------------------------------
    
    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.
    
    For example:
    
    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>
    
    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

  8. #8
    Originariamente inviato da albgen
    mi sembra strano, cmq prova col dispose oppure using(che è la stessa cosa)
    ho fatto cosi e funziona, ma voi m idite che è uno spreco ???

    using() come lo applico a questo spezzone di codice per fare il solito risultato ? ??

    codice:
    foreach (DataRow emailDR in emailDt.Rows)
    {
         SmtpClient smtp = new SmtpClient("out.alice.it", 25);
         msg.To.Add(new MailAddress(emailDR["sEmail"], emailDR["sNomeVis"]));
         smtp.Send(msg);
         msg.To.Clear();
         smtp.Dispose();
    }

  9. #9
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    codice:
    foreach (DataRow emailDR in emailDt.Rows)
    {
        using(SmtpClient smtp = new SmtpClient("out.alice.it", 25))
        {
            msg.To.Add(new MailAddress(emailDR["sEmail"], emailDR["sNomeVis"]));
            smtp.Send(msg);
            msg.To.Clear();
         }
    }
    using

    Se devi inviare 300k email conviene tenere la connessione aperta piuttosto che chiuderla e aprirla per 300.000 volte.
    I got the remedy

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.