.NET Best Method to Send Email (System.Net.Mail has issues)

后端 未结 7 923
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 19:17

This seems to be pretty straight forward. I need to send email from some ASP.NET applications. I need to do this consistently without strange errors and without CPU utilizat

相关标签:
7条回答
  • 2020-12-14 19:37

    I've always used Quiksoft's EasyMail .NET components with no issues at all.

    Product home page: http://www.quiksoft.com/emdotnet/

    They also have a free version of the component if you only need to send out e-mails:

    http://www.quiksoft.com/freesmtp/

    0 讨论(0)
  • 2020-12-14 19:38

    I've used Quicksoft in the past and have no complaints. Another thing you can try is switching the SMTP configuration to use a pickup folder instead of sending using the network which should get around the "it doesn't send QUIT" issue.

    0 讨论(0)
  • 2020-12-14 19:39

    I faced the same CPU utilization problem with the settings described. I ended up opening a ticket with Microsoft to determine the cause of the problem. The CPU utilization problem lies in the ServicePoint class. Internally in the ServicePoint class, there is a timer that runs every (MaxIdleTime/2) milliseconds. See the problem? By changing the MaxIdleTime value to 2, the CPU utilization will drop down to normal levels.

    0 讨论(0)
  • 2020-12-14 19:47

    We have used hMailserver with great success. The configuration can take a while to get used to but it has been a great -- and free -- mail server product.

    If you want to roll your own (which I did years ago when I was having a bear of a time with CDONTS), you can start with the code below and customize to your heart's content. It uses the TcpClient to create a TCP connection directly to the mailserver. Not that I'd recommend this when there are so many established and debugged solutions, but I found this very useful for debugging and determining where the problem was with the prefab MS mail components.

        private void Send_Email() 
        {
            #region Email Sending Function
            string strMail = "";
    
            try 
            {
                // See RFC821 http://www.faqs.org/rfcs/rfc821.html for more specs
                // TcpClient is an abstraction of a TCP Socket connection
                TcpClient myTCP = new TcpClient();
    
                // Connect to the mail server's port 25
                myTCP.Connect(mailserver, 25);
    
                // Open a network stream which sends data to/from the TcpClient's socket
                System.Net.Sockets.NetworkStream ns = myTCP.GetStream();
    
                // The data to send to the mail server, basically a raw SMTP mail message
                strMail = "HELO\n";
                strMail += "MAIL FROM:from@address.com\n";
                strMail += "RCPT TO:" + recipient + "\n";
                strMail += "DATA\n";
                strMail += "Subject: mySubject\n";
                strMail += "To:" + recipient + "\n";
                strMail += "From: \"From Real Name\" <from@address.com>\n";
                strMail += "\n";
                strMail += " ---------------------------------------\n";
                strMail += "Name:     " + txtName.Text + "\n";
                strMail += "Address1: " + txtAddress1.Text + "\n";
                strMail += "Address2: " + txtAddress2.Text + "\n";
                strMail += "City:     " + txtCity.Text + "\n";
                strMail += "State:    " + txtState.Text + "\n";
                strMail += "Zip:      " + txtZip.Text + "\n";
                strMail += "Email:    " + txtEmail.Text + "\n";
                strMail += "Dealer:   " + txtDealer.Text + "\n";
                strMail += " ---------------------------------------\n";
                strMail += "THIS IS AN AUTOMATED EMAIL SYSTEM. DO NOT REPLY TO THIS ADDRESS.\n";
                strMail += "\n.\n";
    
                // Defines encoding of string into Bytes (network stream needs
                // an array of bytes to send -- can't send strings)
                ASCIIEncoding AE = new ASCIIEncoding();
                byte[] ByteArray = AE.GetBytes(strMail);
    
                // send the byte-encoded string to the networkstream -> mail server:25
                ns.Write(ByteArray, 0, ByteArray.Length);
    
                //ns.Read(ByteArray, 0, ByteArray.Length);
                //lblStatus.Text = ByteArray.ToString();
    
                // close the network stream
                ns.Close();
    
                // close the TCP connection
                myTCP.Close();
            }
            catch(Exception ex) 
            {
                throw new Exception("Couldn't send email: <p>" + ex.Message);
            }
    
            #endregion
    
        }
    
    0 讨论(0)
  • 2020-12-14 19:50

    I send most of my mail using a Sproc. I can even attach a file.

    
    CREATE PROCEDURE [dbo].[sendMail_With_CDOMessage]  
        @to VARCHAR(64), 
        @CC VARCHAR(1024)='',
        @BCC VARCHAR(1024)='',
        @subject VARCHAR(500)='', 
        @body VARCHAR(8000)='' ,
        @from VARCHAR(64),
        @filename VARCHAR(255)='',
        @priority INT = 0
    AS  
    BEGIN  
        SET NOCOUNT ON  
    
        DECLARE  
            @handle INT,  
            @return INT,  
            @s VARCHAR(64),  
            @sc VARCHAR(1024),  
            @up CHAR(27),   
            @server VARCHAR(255)  
    
        SET @s = '"http://schemas.microsoft.com/cdo/configuration/'  
    
        SELECT  
            @s = 'Configuration.Fields(' + @s,  
            @up = 'Configuration.Fields.Update',
            @server = 'smtp.yourdomain.com' 
    
    
    
        EXEC @return = sp_OACreate 'CDO.Message', @handle OUT  
        SET @sc = @s + 'sendusing").Value'  
        EXEC @return = sp_OASetProperty @handle, @sc, '2'  
        SET @sc = @s + 'smtpserver").Value'  
        EXEC @return = sp_OASetProperty @handle, @sc, @server  
        EXEC @return = sp_OAMethod @handle, @up, NULL  
        EXEC @return = sp_OASetProperty @handle, 'To', @to  
        EXEC @return = sp_OASetProperty @handle, 'CC', @CC 
        EXEC @return = sp_OASetProperty @handle, 'BCC', @BCC 
        EXEC @return = sp_OASetProperty @handle, 'From', @from  
        EXEC @return = sp_OASetProperty @handle, 'Subject', @subject  
        EXEC @return = sp_OASetProperty @handle, 'HTMLBody', @body    
        EXEC @return = sp_OASetProperty @handle, 'Priority', 'cdoHigh'  
    
        IF @filename IS NOT NULL  
            EXEC @return = sp_OAMethod @handle, 'AddAttachment', NULL, @filename  
    
        EXEC @return = sp_OAMethod @handle, 'Send', NULL  
        IF @return  0  
        BEGIN  
            PRINT 'Mail failed.'  
            IF @from IS NULL  
                PRINT 'From address undefined.'  
            ELSE  
                PRINT 'Check that server is valid.' 
        END 
        ELSE  
            PRINT 'Mail sent.'  
    
        EXEC @return = sp_OADestroy @handle  
    END  
    
    
    0 讨论(0)
  • 2020-12-14 19:53

    While I haven't had any specific problems with System.Net.Mail so far, you can always use the older System.Web.Mail API which is a wrapper for CDOSYS.

    0 讨论(0)
提交回复
热议问题