Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Had this error message when I send a mail from a form.

local host : 28642 says object reference not set to an instance of an object

When I invoke the mail in one form
, however the same commands works in another form.
C#
           EmailSubj = "Complaint Filed  ";
           EmailMsg  = Convert.ToString(txt_Comp_memo.Text);

           string[] emails = new string[] { txt_Email.Text, txt_Zemail.Text, txt_Bemail.Text };
           foreach (var email in emails)
           {
               if (!string.IsNullOrWhiteSpace(email))
                   SendEmail(email, EmailSubj, EmailMsg);



private void SendEmail(String ToEmail, String Subj, string Message)
   {
       try
       {
           //Reading sender Email credential from web.config file
           HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
           FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
           Pass = ConfigurationManager.AppSettings["Password"].ToString();

           //creating the object of MailMessage
           MailMessage mailMessage = new MailMessage();

           mailMessage.From = new MailAddress(FromEmailid); //From Email Id
           mailMessage.Subject = Subj; //Subject of Email
           mailMessage.Body = Message; //body or message of Email
           mailMessage.IsBodyHtml = true;
           mailMessage.To.Add(new MailAddress(ToEmail)); //reciver's Email Id

           SmtpClient smtp = new SmtpClient(); // creating object of smptpclient
           smtp.Host = HostAdd; //host of emailaddress for example smtp.gmail.com etc

           //network and security related credentials

           smtp.EnableSsl = true;
           NetworkCredential NetworkCred = new NetworkCredential();
           NetworkCred.UserName = mailMessage.From.Address;
           NetworkCred.Password = Pass;
           smtp.UseDefaultCredentials = true;
           smtp.Credentials = NetworkCred;
           smtp.Port = 587;
           smtp.Send(mailMessage); //sending Email

           string error = "Message Sent successfully. Thank you..!!";

           ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + error + "');", true);

           txt_Comp_memo.Text = " ";
           txt_Email.Text = " ";
           txt_Zemail.Text = " ";
           txt_Bemail.Text = " ";


       }
       catch (Exception ex)
       {
           string error = ex.Message;
           ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + error + "');", true);
       }

       finally
       {

       }

   }

           }


What I have tried:

Reviewed my own codes and looked for the error message on ways to resolve the problem.
Posted
Updated 28-Sep-16 0:08am
v3
Comments
ChienVH 28-Sep-16 6:23am    
What is the line of code throw the error?
Karthik_Mahalingam 29-Sep-16 1:13am    
in which line you are getting this error, use breakpoint to debug it

1 solution

This question is asked daily, please do basic research before asking a question. Search for the error message and you'll see from the many threads about this error that your post is lacking vital information, and that you need to investigate this via the debugger.
 
Share this answer
 
Comments
Member 10744248 28-Sep-16 6:12am    
I have searched for it and there is no clear answer related to the mail.
F-ES Sitecore 28-Sep-16 6:22am    
You're not going to find the answer to your exact question because no-one can help you debug your code remotely and the issue with this bug is usually to do with your data\inputs or something else outside what you've posted. If you looked at any of the numerous other threads on this error you'd have learned that.

http://www.codeproject.com/search.aspx?q=Object+reference+not+set+to+an+instance+of+an+object&sbo=qa&usfc=false&x=9&y=9

Once you learn to debug your code you'll find out exactly what line the error occurs on and exactly what is null. We can't do that for you from here. If you could even have provided the most basic information such as the line the error happens on someone could probably make a good guess what the issue might be, but just dumping 100 lines of code and thinking someone can help you when you don't even say what line the error is on is a little optimistic.
Member 10744248 28-Sep-16 6:14am    
If you have a well documented reference please state it. I have spent over 2 hours on this error and none of them gives a way out to resolve the problem
[no name] 28-Sep-16 7:05am    
Learn to use google. This is documented all over the internet. Learning to use the debugger would help also. You could have found your null reference error and fixed by now.

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