Click here to Skip to main content
15,906,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Eror-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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

our program:-


C#
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.Security;
using System.Net.Mail;
using System.Net;

namespace WebDesign
{
    public partial class contact_us : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void SendMail()
        {

            // any address where the email will be sending
            var toAddress = YourEmail.Text.ToString();
            // Gmail Address from where you send the mail
            var fromAddress = "....";
            //Password of your gmail address
            const string fromPassword = "....";
            // Passing the values and make a email formate to display
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Question: \n" + Comments.Text + "\n";
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(toAddress, fromAddress, subject, body);
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //here on button click what will done
                SendMail();
                DisplayMessage.Text = "mail send";
                DisplayMessage.Visible = true;
                YourSubject.Text = "";
                YourEmail.Text = "";
                YourName.Text = "";
                Comments.Text = "";
            }
            catch (Exception) { }
        }
    }
}
Posted
Comments
OriginalGriff 9-Apr-14 5:23am    
And?
Where did the stack trace say the error was?
Ajith K Gatty 9-Apr-14 5:33am    
Hi OriginalGriff

in SendMail() function what is that var smtp = new System.Net.Mail.SmtpClient();{//codes// }. is that correct?
OriginalGriff 9-Apr-14 5:52am    
I should know? I can't run it...and if I did know, would I have posted asking the OP? :laugh:
ADI@345 9-Apr-14 5:57am    
the code u have given isn't working...

1 solution

Hi Adi,

Check this code. especially smtpClient smtp ...... part
try
     {
       SmtpClient smtp = new SmtpClient
       {
         Host = "smtp.gmail.com", // smtp server address here...
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
         Timeout = 30000,
       };
       MailMessage message = new MailMessage(senderID, toAddress, subject, body);
       smtp.Send(message);
     }
     catch (Exception ex)
     {
       result = "Error sending email.!!!";
     }
     return result;
 
Share this answer
 
Comments
ADI@345 9-Apr-14 5:49am    
atually sir,my program works on localhost but not through server ..pls solve my problem
ADI@345 9-Apr-14 5:52am    
smtp.Send(toAddress, fromAddress, subject, body);


in this line,error occurs sometimes..
Ajith K Gatty 9-Apr-14 6:08am    
Check your smtp.Send parameters. it should be in smtp.Send(string From,string To,string sub,string body).

If still you have problems you should ask someone else. I dont know the actual problem then.

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