You must Sign In to post a response.
  • Category: ASP.NET

    How to receive a Mail to from Address

    Hello Developers,
    I want to receive email to my email(or a website address) address through my asp.net application. Its like someone who send an inquiry through a form.Anyone know just give me ideas for that please.

    Thanking you
    Paul.S
  • #762613
    Hi Paul,

    In one of my article, I have written the same. Please have a look at below code.


    protected void Button1_Click(object sender, EventArgs e)
    {
    var fromAddress = "Gmail@gmail.com";
    var toAddress = txtEmail.Text.ToString();
    const string fromPassword = "Password";
    string subject =txtSubject.Text.ToString();
    string body = "From: " + txtName.Text + "\n";
    body += "Email: " + txtEmail.Text + "\n";
    body += "Subject: " + txtSubject.Text + "\n";
    body += "Message: \n" + txtMessageText + "\n";
    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;
    }
    smtp.Send(fromAddress, toAddress, subject, body);
    }

    For complete reference have a look at below article.
    http://www.dotnetspider.com/resources/44329-How-implement-user-registration-with-email-confirmation-using-ASPNET.aspx
    All the Best.

    -------------
    Glad to be,
    John Bhatt
    Editor - DNS Forums
    https://www.pyarb.com

  • #762670

    Hello Paul Raj,

    You can refer the below code :

    string ipaddress;
    public void MsgBox(String ex, Page pg, Object obj)
    {
    string s = "<SCRIPT language='javascript'>alert('" + ex.Replace("\r\n", "\\n").Replace("'", "") + "'); </SCRIPT>";
    Type cstype = obj.GetType();
    ClientScriptManager cs = pg.ClientScript;
    cs.RegisterClientScriptBlock(cstype, s, s.ToString());
    }

    public void SendMailToUs()
    {
    TextBox Email = new TextBox();
    Email.Text = "Enquiry or Suggestion from user. Below are the user details." + Environment.NewLine + Environment.NewLine;
    Email.Text += "Name:" + " " + TextBoxName.Text + Environment.NewLine;
    Email.Text += "Email:" + " " + TextBoxEmail.Text + Environment.NewLine;
    Email.Text += "Contact:" + " " + TextBoxContact.Text + Environment.NewLine;
    Email.Text += "Message:" + " " + TextBoxMessage.Text + Environment.NewLine;
    Email.Text += "IP Address:" + " " + ipaddress;

    MailMessage mailmesssage = new MailMessage();
    mailmesssage.From = new MailAddress("sender@gmail.com", "Paul Raj");
    mailmesssage.To.Add("receiver@domainname.com");
    mailmesssage.Subject = "Recently visited / enquiry user details";
    mailmesssage.Body = Email.Text;

    //G-mail settings
    try
    {
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    smtpClient.Port = 587;
    smtpClient.Credentials = new NetworkCredential("sender@gmail.com", "senderpassword");
    smtpClient.EnableSsl = true;
    smtpClient.Send(mailmesssage);
    MsgBox("Thank you for contacting us." + Environment.NewLine + "We will get back you soon.", this.Page, this);
    }
    catch (Exception ex)
    {
    MsgBox("An error occured." + Environment.NewLine + "Error details: " + ex.Message + "", this.Page, this);
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];
    }
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
    Page.Validate();
    if (Page.IsValid == true)
    {
    SendMailToUs();
    TextBoxName.Text = "";
    TextBoxEmail.Text = "";
    TextBoxContact.Text = "";
    TextBoxMessage.Text = "";
    string jScript = "<script>window.close();</script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);
    }
    }


    I have made it for our website.

    See the image of inquiry form.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment


  • Sign In to post your comments