开发者

How can i send a comment in email?

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.IsBo开发者_高级运维dyHtml = 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)'


    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
}


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;
            }
        }


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!");
    }
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜