Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
MailMessage mm = new MailMessage("xyz@gmail.com", TextBoxEmail.Text.Trim());
                                        mm.Subject = "Password Recovery";
                                        mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
                                        mm.IsBodyHtml = true;
                                        SmtpClient smtp = new SmtpClient();
                                        smtp.Host = "smtp.gmail.com";
                                        smtp.EnableSsl = true;
                                        NetworkCredential NetworkCred = new NetworkCredential();
                                        NetworkCred.UserName = "xyz@gmail.com";
                                        NetworkCred.Password = "xyz";
                                        smtp.UseDefaultCredentials = true;
                                        smtp.Credentials = NetworkCred;
                                        smtp.Port = 587;
                                        smtp.Send(mm);
                                        message = "Registration successful. Activation email has been sent.";
                                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);



this is my code to send email after registration is successful. it work fine on my localhost iis server. but after deploying web-site on server email is not send to user. there is
C#
Object reference not set to an instance of an object.

error message shown. please help.

C#
same code work on password recovery page. but here it is not working.


I did some changes in code and also create a new page for registration now it is showing a following error

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

first i thought that it is error of mail sending code but it work fine on iis local server and also on other page. so i think may be it is a problem of button click event. just able to reach here. please some help me as i know i get my answer here.

my new code:


C#
using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("xyz@gmail.com");
                    mail.Subject = "mailSubject";
                    mail.Body = "mailBody";
                    mail.IsBodyHtml = true;

                    mail.To.Add("xyz@gmail.com");



                    using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))//2nd parameter is PORT No.
                    {
                        smtp.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "xyz");
                        smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
                        smtp.Send(mail);//actual sending operation here
                    }
                }


What I have tried:

C#
MailMessage mm = new MailMessage("xyz@gmail.com", TextBoxEmail.Text.Trim());
                                        mm.Subject = "Password Recovery";
                                        mm.Body = string.Format("Hi ,<br /><br />Your password is .<br /><br />Thank You.");
                                        mm.IsBodyHtml = true;
                                        SmtpClient smtp = new SmtpClient();
                                        smtp.Host = "smtp.gmail.com";
                                        smtp.EnableSsl = true;
                                        NetworkCredential NetworkCred = new NetworkCredential();
                                        NetworkCred.UserName = "xyz@gmail.com";
                                        NetworkCred.Password = "xyz";
                                        smtp.UseDefaultCredentials = true;
                                        smtp.Credentials = NetworkCred;
                                        smtp.Port = 587;
                                        smtp.Send(mm);
                                        message = "Registration successful. Activation email has been sent.";
                                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);



this is my code to send email after registration is successful. it work fine on my localhost iis server. but after deploying web-site on server email is not send to user. please help.
Posted
Updated 31-May-16 23:26pm
v7
Comments
F-ES Sitecore 30-May-16 6:09am    
You have usedefaultcredentials set to true, it should be false.

http://forums.asp.net/post/5825785.aspx
Kishor-KW 30-May-16 9:09am    
same code work on password recovery page. but here it is not working.
F-ES Sitecore 31-May-16 5:30am    
This is one of the most frequently asked questions, google for sending email through gmail. The fact that it works in one scenario but not another means your code is not at fault, there are lots of reasons gmail won't send your mail, many are extensively documented. The only real solution is to not use gmail to send mail in the first place.
Kishor-KW 31-May-16 5:39am    
then what to use? yahoo?
F-ES Sitecore 31-May-16 6:12am    
No, use the smtp server provided by your webhost.

Probably, it's your NetworkCred values: I know you have substituted values into the code, but I suspect that gmail is rejecting your credentials. Try changing:
C#
smtp.UseDefaultCredentials = true;
To:
C#
smtp.UseDefaultCredentials = false;
And double check the username and password you are sending to gmail.
 
Share this answer
 
Comments
Kishor-KW 30-May-16 5:37am    
i did that changes but still it is not working. username and password are right.
Kishor-KW 30-May-16 9:09am    
same code work on password recovery page. but here it is not working.
Luc Pattyn 6-Jun-16 20:22pm    
Hi Griff,

My C# desktop applications just started having trouble sending emails using gmail; seems Google has enabled OAuth 2.0 now, and a simple credential isn't sufficient any more.

I'm eager to read a nice article on how to do OAuth 2.0, still googling though... Any suggestions? Or spare time?

BTW: of course one can change the gmail account to "Allow less secure apps" but that doesn't sound like the right way as it most likely disables OAuth 2.0 all together.

Cheers,

Luc
OriginalGriff 7-Jun-16 6:50am    
Hi Luc!
Long time no speak - how you doing? Good to see you back here.
I haven't had to deal with OAuth 2.0 yet - but it's on my (copious) TODO so I'm up to speed when I do need it. Guess I'll have to move it up the list... :sigh:
Hi Member__979,

You can try the below code and again if it doesnt work , then please upload the exact error message sothat we will be narrow down the issue properly. It also may due to some network related issue which is not allowing to send.

C#
using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("sendersMailAddressHere");
                    mail.Subject = mailSubject;
                    mail.Body = mailBody;
                    mail.IsBodyHtml = true;

                    mail.To.Add("Toaddress");
                    mail.CC.Add("CCaddress");
                        

                    using (SmtpClient smtp = new SmtpClient("hostNameHere",587))//2nd parameter is PORT No.
                    {
			smtp.Credentials = new System.Net.NetworkCredential("sendersEmail", "sendersPassword");//supply userId & psd here
       			smtp.EnableSsl = true;//set this as your Host Name properties, for gmail,its true
                        smtp.Send(mail);//actual sending operation here
                    }
                }


Thanks,
Prateek
 
Share this answer
 
v3
Comments
Kishor-KW 30-May-16 7:46am    
what is smtpTos,smtpCCs? as i tried but not work.
Prateek Dalbehera 31-May-16 1:38am    
sorry,these are variables of my code, you can simply write, mail.To.Add() and mail.CC.Add(). Please post the error message what u are getting, then only somebody can identify what the actual issue is.
Kishor-KW 31-May-16 1:42am    
there is no error.thats why it is tough to understand, just mail is not send.
but same code is work on other page. how it is possible?
Prateek Dalbehera 31-May-16 2:31am    
Its quite complicated then if there will be network issues then u ll not be able to send any mail. There is an event for mail sending completed in C#, try if this event is hit or not after sending mail.

//
// Summary:
// Raises the System.Net.Mail.SmtpClient.SendCompleted event.
//
// Parameters:
// e:
// An System.ComponentModel.AsyncCompletedEventArgs that contains event data.
protected void OnSendCompleted(AsyncCompletedEventArgs e);
Prateek Dalbehera 31-May-16 2:34am    
Also, try to write this just before smtp.send method, below is not the advised way but you try just for testing purpose.

ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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