Click here to Skip to main content
15,913,758 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'm unable to send an e-mail from my website in online,but in offline it can be executed
i use the following code

C#
MailMessage msg = new MailMessage();
       msg.To.Add(new MailAddress(TextBox3.Text.ToString()));
       msg.From = new MailAddress("myid@gmail.com");
       msg.Subject = "welcome hi";
       msg.Body = "name" + TextBox1.Text + "course" + TextBox2.Text;
       msg.IsBodyHtml = true;
       msg.Priority = MailPriority.Normal;
       SmtpClient smtp = new SmtpClient();
       smtp.Host = "smtp.gmail.com";
       smtp.Port = 587;
       smtp.EnableSsl = true;
       smtp.Credentials = new System.Net.NetworkCredential("myid@gmail.com", "psw");
       smtp.Send(msg);
       Response.Write("<script>alert('mail sent')</script>");
Posted
Updated 3-Jun-15 1:27am
v2
Comments
ZurdoDev 3-Jun-15 7:35am    
What do you mean you it works when offline but not when online?
[no name] 3-Jun-15 7:38am    
Can you show the error in online ?
AdamASPGeek 4-Jun-15 1:39am    
I'm sorry I dont fully understand your question, you said that you can send email via offline, how can this be done?

1 solution

That is impossible! No such technology has yet been introduced that can send emails offline (unless we are talking about storing the email in outbox). So, the actual thing happens when you are online, exception is raised. There are a lot of problems that can happen when you are working with networks. Network, authentication, SSL or other protocol or proxy based problems occur.

Your code looks fair to be executed, the only problem occurs if your authentication is not OK. The problem would cause your application to show you an error message that is returned from the Google servers telling you that you cannot send the email, using the credentials that you have provided them with.

Also, since you have not shown the error message, I would assume that the only problem is the username/password or your account is a very new one and Google doesn't allow SMTP settings right now. You need to check that in your settings panel and then allow SMTP or other settings; I had the similar problem 2 months ago with my new account.

Also, the code that you are using is not even of ASP.NET framework, that is native .NET framework's System.Net.Mail namespace being used. For that, please read this article of mine about Sending emails over .NET framework, and general problems – using C# code[^]. The article would discuss the possible problems while sending the emails, and a solution to solve them.
 
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