0
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.Web.Mail;






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

    }


   
    protected void SendMail()
    {
        var fromAddress = new MailAddress("user@gmail.com", txtName.Text);
        var toAddress = new MailAddress("recipient@recipient.com", "Recipient");
        const string fromPassword = "gmailPassword";
        const string subject = "Contact Me";
        string body = "From: " + txtName.Text + "<br />";
        body += "Email: " + txtEmail.Text + "<br />";
        body += "Comments: " + txtComments.Text + "<br />";

        var smtp = new SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress.Address, fromPassword);
            smtp.Timeout = 20000;
        }

        using (var message = new MailMessage(fromAddress, toAddress))
        {
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;

            smtp.Send(message);
        }

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SendMail();
        }
        catch (Exception)
        {
        }

    }

i am not able to send the email using this code.

[email removed]

Edited by Ezzaral: Added code tags. Removed email.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by jbisono
1

Maybe because you have two namespace that contains the same class name on it

using System.Net.Mail;
using System.Web.Mail;

I would try first taking out the second one because that one is obsolete, and give that a try.

Edited by jbisono: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.