Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the below code for sending mail from c# using gmail but I got the error:
"The SMTP host was not".
Please help me..

static bool mailSent = false;


 public void SendMail()
        {
            //Builed The MSG
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add("gan@gmail.com");
            msg.To.Add("ganesan@gmail.com");
            msg.From = new MailAddress("ganesan@gmail.com");
            msg.Subject = "Test mail using .net2.0";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "This is my msg Body";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = false;
            msg.Priority = MailPriority.High;            
           
            //Add the Creddentials
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");
            client.Port = 587;//or use 587            
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true;
            client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);                
            object userState=msg;
            try
            {
                //you can also call client.Send(msg)
                client.SendAsync(msg, userState);   // i am gettint error in this line........             
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "Send Mail Error");
            }
        }

      void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
       {
           MailMessage mail = (MailMessage)e.UserState;
           string subject = mail.Subject;

           if (e.Cancelled)
           {
               string cancelled = string.Format("[{0}] Send canceled.", subject);
               MessageBox.Show(cancelled);
           }
           if (e.Error != null)
           {
               string error = String.Format("[{0}] {1}", subject, e.Error.ToString());
               MessageBox.Show(error);
           }
           else
           {
               MessageBox.Show("Message sent.");
           }
           mailSent = true;
       }
Posted
Updated 19-Apr-11 1:08am
v5
Comments
Rahul VB 11-Mar-14 3:15am    
Are you using a PROXY SERVER? check that out first....

The clue is in the error message.. there is a problem connecting to the SMTP Server, check your connection settings
 
Share this answer
 
Comments
ganesan2510 19-Apr-11 7:06am    
tell me what i have to change in connection
jim lahey 19-Apr-11 7:12am    
Without a detailed error message I can't tell you. The problem is located somewhere in these three lines:

client.Port = 587;//or use 587
client.Host = "smtp.gmail.com";
client.EnableSsl = true;

your code can't actually connect to the SMTP server, and these are the three lines that deal with connection specifics.
Add this line of code

client.UseDefaultCredentials = false;


before your

<pre lang="midl">client.Credentials = new System.Net.NetworkCredential
                ("ganesan@gmail.com", "*******");

 
Share this answer
 
Comments
ganesan2510 20-Apr-11 0:21am    
again the same error is coming please help..
 
Share this answer
 
Could be a DNS error too. Can you resolve the SMTP server outside of your app?
 
Share this answer
 
Comments
Rahul VB 28-Mar-14 0:04am    
Yes Sir, thats true, i would also like to add "Please dont use proxy server".
Thanks a ton,
Rahul
You should use SMTP credentials

C#
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
 SmtpServer.EnableSsl = true;


http://csharp.net-informations.com/communications/csharp-smtp-mail.htm[^]

gail
 
Share this answer
 
Hello Friend,

Firstly, you must PING the host and check if it is available.
Next you must do one thing, check if your internet connection is through a proxy, if so, remove it.


Try this :


protected static string SendEmail(string from,string password,string host,int port,string toAddress,string[] CCAdress,string subject, string body)
        {
            string result = "Message Sent Successfully..!!";
            string senderID = from;// use sender's email id here..
            string senderPassword = password; // sender password here...
            try
            {
                SmtpClient smtp = new SmtpClient
                {
                    Host = host, // smtp server address here...
                    Port = port,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
                };
                MailMessage message = new MailMessage(senderID, toAddress, subject, body);
                foreach (string email in CCAdress)
                {
                    message.CC.Add(email);
                }
                    message.Subject = subject;
                smtp.Send(message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                result = "Error sending email.!!!";
            }
            return result;
        }


Just try this,.....
From the main method call the above code with correct parameters.....


I would like to thank OG(Original Griff)for this....(i read OG's article...great one).

One doubt:Using Proxy Server....(You wont be able to send an email)...Why?


Thanks
 
Share this answer
 
v3
Comments
CHill60 10-Mar-14 8:41am    
I'm a little confused about this post - the question is 2 years old, and to do with connectivity. Also the OP has not written any articles
Rahul VB 11-Mar-14 3:24am    
I read Original Griff's article i call him OG. He has a great article on sending an email using C#. He is a code project MVP. I cant actually recollect about his article, i read it recently. Nice one.

I am sorry to confuse you my friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900