2

I have some text boxes like name,email address,phone no. and comment on my page.

I have to send the values to my email address..

How should I do this?? I am doing:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    SmtpClient client = new SmtpClient();
    MailMessage message = new MailMessage();

    try
    {
        MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

        SmtpClient.Host = "localhost";
        SmtpClient.Port = 25;

        message.From = fromAddress;
        message.To.Add("xyz@gmail.com");
        message.Subject = "Feedback";
        message.IsBodyHtml = false;
        message.Body = txtComment.Text;
        SmtpClient.Send(message);

        Response.Write("Email successfully sent.");
    }
    catch (Exception ex)
    {
        Response.Write("Send Email Failed." + ex.Message);
    }
}

and I am getting the following error:

An object reference is required for the nonstatic field, method, or property 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)'
divya
  • 405
  • 2
  • 6
  • 18

4 Answers4

2
    SmtpClient.Host = "localhost";
    SmtpClient.Port = 25;
    ~~~~~~~~~~~~~~~~~~~~
    SmtpClient.Send(message);

These lines are attempting to use members of the class SmtpClient. However, as these members are not defined as static, you need to refer to your instance of that class, which you have called client.

Try

    client.Host = "localhost";
    client.Port = 25;
    ~~~~~~~~~~~~~~~~~~~~
    client.Send(message);

Also, have a read of this article on the differences between class and instance members.

Finally, as SmtpClient implements IDisposable, I would change your code to wrap it in a using block, as this will ensure you are correctly cleaning up your SMTP session after you are finished with it.

using (SmtpClient client = new SmtpClient()) 
{
    // YOUR CODE
}
RB.
  • 36,301
  • 12
  • 91
  • 131
  • But still email sending become failed..can you tell me? that why it is so?? – divya May 25 '11 at 08:12
  • @divya install stmp4dev(http://smtp4dev.codeplex.com/) on you local machine and test against it. It might be that your SMTP server is not setup properly – marto May 25 '11 at 09:41
  • Based on your comments on the other answer, it looks like you don't actually have an SMTP server running on `localhost`. Do you understand what an SMTP server is and how it works (I'm not trying to be rude - just trying to understand your issues). I would have a look at a question like this:http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail. – RB. May 25 '11 at 12:07
  • no.. not at all..(you are not rude..). but why mail sending become failed when i am doing it on localhost?? please can you tell me?? – divya May 26 '11 at 06:44
  • Basically, you need to be pointing your code towards an SMTP server. If there is not an SMTP server running on localhost, then the code will fail. @just_name's answer shows code using GMail's SMTP server as an alternative to running your own. – RB. May 26 '11 at 07:57
  • thanks @RB.. but when i am doing this using GMail's SMTP then on my mail it is taking my 2nd id in FROM.. it is not taking the id which i entered in `MAIL.FROM()`... – divya May 26 '11 at 09:47
0

Try

client.Host = "localhost";
client.Port = 465;
~~~~~~~~~~~~~~~~~~~~
client.Send(message);

Do not try to use or manage disposable emails with this because you will be clearly exposed to spam-trappers. Your code is open and very easy to identify who is using the code and from where.

0
public static string sendMail(string to, string title, string subject, string body)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

                if (to == "")
                    to = fromAddress.Address;
                MailAddressCollection m = new MailAddressCollection();
                m.Add(to);
                mail.Subject = subject;
                mail.From = fromAddress;
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.ReplyTo = fromAddress;
                mail.To.Add(m[0]);
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 25;
                smtp.EnableSsl = true;
                smtp.Credentials = new System.Net.NetworkCredential(fromAddress.Address, "XXXX");//"XXXX" is the password of the sender.
                ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; 

                smtp.Send(mail);

                return "done";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 1
    in line smtp.Credentials = new System.Net.NetworkCredential(fromAddress, "XXXX") how can i write the password of the sender...?? – divya May 25 '11 at 09:45
  • i am not sending mail from one mail id to another..even i have to send some data like name and phone no and comment on a particular email id.. – divya May 25 '11 at 09:50
  • excuse me , i don't understand what u wanna to do.. Do u want to send an email to yourself ? – Anyname Donotcare May 25 '11 at 09:55
  • okkkk...i have a page on which user insert his name,email id,contact no and comment and then when he click on the submit button.. i want all the info filled by him. send on my email id.. – divya May 25 '11 at 10:00
  • how please, because according to my little knowledge :: ASP.NET does not include a `POP3` (email receive) component. `SMTP` is for sending only.How you execute some thing like that. – Anyname Donotcare May 25 '11 at 12:03
0

i had done this:-

protected void btnSubmit_Click(object sender, EventArgs e)

{

MailMessage mailmsg = new MailMessage();

    string name = txtName.Text.ToString();
    string contact = txtPhoneNo.Text.ToString();
    string comment = txtComment.Text.ToString();
    string checkIn = txtCheckIn.Text.ToString();
    string from = txtEmail.Text.ToString();

    mailmsg.To.Add("recipientemailid@gmail.com");
    mailmsg.From = new MailAddress(from.Trim());

    mailmsg.Subject = "Test message";

    mailmsg.Body = "This Query is submitted by user " + name.Trim() + ", "+ contact.Trim() + ", " + comment.Trim() + ", " + checkIn.Trim();
    mailmsg.IsBodyHtml = true;

    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true;

    NetworkCredential credentials = new NetworkCredential("recipientemailid@Gmail.com", "password");
    client.Credentials = credentials;

    try
    {
        //try to send the mail message
        client.Send(mailmsg);
        Response.Write("sent!");
    }
    catch
    {
        //some feedback if it does not work
        Response.Write("failed!");
    }
}
divya
  • 405
  • 2
  • 6
  • 18
  • I am using here GMAIL SMTP Server.. can anyone tell me that how can i do this using smtp localhost..??? – divya May 26 '11 at 05:37