CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2007
    Posts
    68

    Post Unable to send email using SmtpClient

    I have written the following code for sending Email. I have used my Gmail username and password for Network credentials.

    string strEmailID ; //It is some valid Email ID.

    MailMessage mail = new MailMessage("Admin@Accounts.com", strEmailID);
    mail.Subject = "New Reimbursement added";
    mail.Body = "Test";

    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Credentials = new NetworkCredential(username, password);
    client.Send(mail);

    But this doesn't work for all systems. In some systems, it is successfully sending mail, but in some systems, it is throwing an exception.

    Can any one help me regarding this.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Unable to send email using SmtpClient

    What kind of exception is thrown and what is the message and innerexception of the exception?

  3. #3
    Join Date
    Jul 2008
    Posts
    23

    Re: Unable to send email using SmtpClient

    I had a script running fine for the past month and yesterday it failed as well....any ideas?

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.OleDb;
    using System.Data;
    using System.IO;
    
    
    namespace mySendEmail
    {
        class Program
        {
            
            
            public static void Main(string[] args)
            {
                mySendEmail("\\\\somefile.txt");
            }
    
            // get numbers   
            static void mySendEmail(string filename)
            {
               
                try
                {              
                    StreamReader SR;
                    string fileContents;
                    
                    SR=File.OpenText(filename);
                    fileContents=SR.ReadLine();
                    string numbers="";
                    int i = 0;
                        while(fileContents!=null)
                        {
                            i++;
                            numbers += fileContents.Substring(6, 10);
                            numbers += "\n";
                            
                            fileContents=SR.ReadLine();
                        }
                    SR.Close();
    
    
                            
                    //send out email
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                    message.To.Add("myEmailAddress@someplace.com");
                    message.To.Add("myEmailAddress@someplace.com");
                    message.Subject = "Your numbers";
                    message.From = new System.Net.Mail.MailAddress("myEmailAddress@someplace.com");
                    message.Body = "Here's youre numbers"+ numbers;
                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mysmtpclient.location.anotherlocation.net");
                    smtp.Send(message);
                    
                }       
    
                    
                
                catch (Exception e)
                {
                    Console.WriteLine("The send email process failed: {0}", e.ToString());
                }
    
                
    
    
            }
        }
    
    
    }

    Code:
    The program to send email process failed: System.Net.Mail.SmtpException: Failure sending mail. ---> 
    System.IO.IOException: Unable to read data from the transport connection: 
    An existing connection was forcibly closed by the remote host. ---> 
    System.Net.Sockets.SocketException: An existing connection wasforcibly closed by the remote host   
    at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)   
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)   
    --- End of inner exception stack trace ---   
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)   
    at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count)   
    at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)   
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)   
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)   
    at System.Net.Mail.SmtpReplyReader.ReadLine()   
    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)   
    at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)  
     at System.Net.Mail.SmtpClient.GetConnection()   
     at System.Net.Mail.SmtpClient.Send(MailMessage message)  
     --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at mySendEmail.Program.mySendEmail(String filename) in C:\Documents and Settings\*****\My Documents\Visual Studio 2005\Projects\mySendEmail\mySendEmail\Program.cs:line 54Press any key to continue . . .

  4. #4
    Join Date
    Jul 2008
    Posts
    23

    Re: Unable to send email using SmtpClient

    bump?

  5. #5
    Join Date
    Jul 2008
    Posts
    23

    Re: Unable to send email using SmtpClient

    bump?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured