Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtEmail.Text);
msg.To.Add("mymailid");
msg.IsBodyHtml = true;
msg.Body = "Name : " + txtname.Text + "<br /><br /> Email : " + txtEmail.Text + "<br /><br /> Phone : " + txtmobile.Text + "<br /><br /> Message : " + Txtmessage.Text + "<br>";
msg.Subject = txtSubject.Text;
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = "smtp.gmail.com";
smtpclient.Port = 587;
smtpclient.Credentials = new System.Net.NetworkCredential("mymailid", "password");
smtpclient.EnableSsl = true;
smtpclient.Send(msg);
Label1.Text = "Thanks for contacting us";
txtname.Text = "";
txtEmail.Text = "";
txtSubject.Text = "";
Txtmessage.Text = "";
txtmobile.Text = "";
}
catch(Exception ex)
{
Console.WriteLine("{0} Exeception caugh.",ex);
}



this code of mine working on local host but not working when i put it on server can an one suggest me what to do.

i als like to ask if there is another host(yahoo,my own website etc) then what changes i need to do in my code

What I have tried:

Changed the tag to C#
Posted
Updated 19-Sep-18 3:04am
v3
Comments
Sergey Alexandrovich Kryukov 3-Aug-14 1:43am    
What is "not working", exactly?
—SA
shilpamittal15 3-Aug-14 3:00am    
mail is not sending with this code when i upload it on server
shilpamittal15 3-Aug-14 3:18am    
this whole code is not working the mail is not sending
Marco Bertschi 3-Aug-14 12:03pm    
Have you tried to resolve if there is an exception thrown while you run it on the server? Does the server have access to "smtp.gmail.com" (Maybe try to ping it?)?
shilpamittal15 3-Aug-14 13:39pm    
there is no exception coming because code is working fine on local host but not doing any thing while uploading.

1 solution

Check this link!

It worked for other users, if it doesn't work, Check the firewall settings on server!
Try to run this code in a sample snippet, there must be something from the network which might be preventing your code to send an email.
As Marco suggested, try to ping "smtp.google.com" from server to check if server allows access to smtp.

Answer of 2nd question:
If you want to use the code for other SMTP Clients, then you can change the SmtpClient settings as SmtpServer name and port. :)
 
Share this answer
 

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