Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
my code : that is timeout or failure.
C#
////my message setting 
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("myGmail@gmail.com");
            msg.To.Add("tomail@gmail.com");
            msg.Body = "body my mail";
            msg.Subject = "my mail subject";
            //msg.IsBodyHtml = true;
            //msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Credentials=new NetworkCredential("mygmail@gmail.com", "PWD","smtp.gmail.com);
            client.Host = "smtp.gmail.com";
            client.Port = 465;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Send(msg);

XML
<mailsettings>
      <smtp from="myGmail@gmail.com" deliverymethod="Network">
        <network defaultcredentials="false" enablessl="true" host="smtp.gmail.com" port="465" password="pwd" username="mygmail" />
      </smtp>
    </mailsettings>

Please help me.
Posted
Updated 8-Mar-12 10:10am
v3
Comments
R. Giskard Reventlov 8-Mar-12 16:08pm    
Please format correctly. What is the exception you are getting? Note: if you copied and pasted the code above from your class there is a missing quote mark at: new NetworkCredential("mygmail@gmail.com", "PWD","smtp.gmail.com);
sadegh_rusta 8-Mar-12 16:28pm    
i do it.but not ok
Ed Nutting 8-Mar-12 16:12pm    
I want a exact error code - to put it in your words. How can we help you if you don't give any details as to what the error actually is!? "Timeout or failure" covers just about every error possible!
sadegh_rusta 8-Mar-12 16:29pm    
/???i dont know where is error in my code
Ed Nutting 8-Mar-12 16:32pm    
apparently... look the code must be either throwing an exception - in which case you need to put a try/catch block round it and gives us the specific error message - or the web server is returning an error code/message so you need to tell use what that is.

1 solution

Change your port number to 587 and try once.

Refer this code

C#
public int SendUserMail(string fromad, string toad, string body, string header, string subjectcontent)
    {
        int result = 0;
        MailMessage usermail = Mailbodplain(fromad, toad, body, header, subjectcontent);
        SmtpClient client = new SmtpClient();
        //Add the Creddentials- use your own email id and password
        client.Credentials = new System.Net.NetworkCredential("your user id ", "pwd"); ;
        
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true; 
        try
        {
            client.Send(usermail);
            result = 1;
        }
        catch (Exception ex)
        {
            result = 0;
        } // end try 

        return result;
 
    }
    public MailMessage Mailbodplain(string fromad, string toad, string body, string header, string subjectcontent)
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        try
        {
            string from = fromad; 
            string to = toad; 
            mail.To.Add(to);
            mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
            mail.Subject = subjectcontent;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;
        }
        catch (Exception ex)
        {
            throw;
        }
        return mail;
    }
 
Share this answer
 
Comments
sadegh_rusta 9-Mar-12 15:18pm    
ok thx
it is ok
sadegh_rusta 9-Mar-12 15:18pm    
ok thx
it is ok
sadegh_rusta 10-Mar-12 16:14pm    
but after run,i have this error :
smtpexeception was caught

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
please help me

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