ASP.NET - Submitting form details in Email - Asked By Naresh Kumar on 12-Jul-11 08:47 AM

Hi All,

Is there any possiblity to send an email to Users which contains the HTML controls? Then the Users enters there details and submit. I need to capture these details and save.

I donot know whether it is possible or not. Please guide me in this.

Appreciate your help.
Jitendra Faye replied to Naresh Kumar on 12-Jul-11 08:54 AM

Follow these steps-


1. Create a Web application or add a new aspx page to your web application.
2. Place controls as required. for example, screen below-






3. Design of ASPX page is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Web Mail</title>

<style>

.width100

{

width=100px;

}

.width200

{

width=200px;

}

</style>

</head>

<body>

<form id="form1" runat="server">

<table style="background-color: Window; border: solid 1px blue; width: 500px; border-color: Blue;">

<tr>

<td colspan="2" style="text-align: center; background-color: Blue; font: bold, 15px;

color: White;">

<asp:Label ID="lblTitle" runat="server" Text="Send Your Feedback"></asp:Label>

</td>

</tr>

<tr>

<td class="width100">

<asp:Label ID="lblName" runat="server" Text="Your Name:"></asp:Label>

</td>

<td class="width200">

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td class="width100">

<asp:Label ID="lblEmail" runat="server" Text="Your E-mail:"></asp:Label>

</td>

<td class="width200">

<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

</td>

</tr>

<tr>

<td colspan="2" style="width: 300px">

<asp:TextBox ID="txtMailBody" runat="server" TextMode="MultiLine" Rows="10" Width="478px"></asp:TextBox>

</td>

</tr>

<tr>

<td style="text-align: center;">

<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />

</td>

<td style="text-align: center;">

<asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" />

</td>

</tr>

<tr>

<td>

<asp:Label ID="lblStatus" runat="server" Width="81px"></asp:Label>

</td>

</tr>

</table>

</form>

</body>

</html>


4. Now change the Code behind of .aspx.cs as below:


using
System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Net.Mail;

public partial class _Default : System.Web.UI.Page

{

string strUserName = "";

string strUserEmail = "";

string strSubject = "";

string strUserMailBody = "";

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnReset_Click(object sender, EventArgs e)

{

clearFields();

}

private void clearFields()

{

txtEmail.Text = string.Empty;

txtName.Text = string.Empty;

txtMailBody.Text = string.Empty;

lblStatus.Text = string.Empty;

}

protected void btnSend_Click(object sender, EventArgs e)

{

strUserName = txtName.Text;

strUserEmail = txtEmail.Text.ToString().Trim();

strSubject = "User Feedback";

strUserMailBody = txtMailBody.Text;

sendMail(strUserName, strUserEmail, strSubject, strUserMailBody);

}

private void sendMail(string uname, string umail, string sub, string mailbody)

{

string SMTPServerName = "SLONP143EEX1";

SmtpClient client = new SmtpClient(SMTPServerName);

MailMessage message = new MailMessage();

try

{

MailAddress fromAddress = new MailAddress(umail, uname);

message.From = fromAddress;

message.Subject = sub;

message.Body = mailbody;

message.To.Add(deepak.solanki@xyx.com);   /*Site admin email*/

message.CC.Add("sanjeet.kumar@xyz.com");

client.Send(message);

lblStatus.Text = "Mail sent successfully.";

}

catch(Exception ex)

{

lblStatus.Text = "Error : " + ex.ToString();

}

}

}

5. Build and Run.
6. Test by sending mail.



Hope this will help you.

dipa ahuja replied to Naresh Kumar on 12-Jul-11 08:55 AM
Yes you can attatch html control in email

protected void Button1_Click(object sender, EventArgs e)
  {
    string toEmailAddress = "xx@gmail.com";
    string GmailId = "xx@gmail.com";
    string password = "xxx";
    string bodyMsg = "<html><body>Enter Name<input type='text'/></body></html>";
 
    MailMessage mail = new MailMessage();
    mail.To.Add(toEmailAddress);
 
    mail.From = new MailAddress(GmailId);
    mail.Subject = txtSubject.Text;
    mail.Body = bodyMsg;
    mail.IsBodyHtml = true;
 
    //OR attatch from your computer
    //  mail.Attachments.Add(new Attachment("path of word document"));
    
    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);
    smtp.Send(mail);
  
  }
Reena Jain replied to Naresh Kumar on 12-Jul-11 09:47 AM
Hi,

here are two good links for help

http://howtouseasp.net/send-a-web-form-as-a-email-asp-net-vb-net/
http://www.4guysfromrolla.com/articles/102203-1.aspx

Hope  this  will help you
Radhika roy replied to Naresh Kumar on 12-Jul-11 10:08 AM
Follow these steps-

1. Add the below code between the <appSettings> Add the below with your IP  </appSettings> tags. Your resultant    code looks like this

<appSettings>  

<add key="SmtpServer" value="<Your SMTP IP>"/>

</appSettings>

2. Use this code

Private Sub SendClick()
      Dim mailMsg As New MailMessage
      Try
        mailMsg.To = "<tomailid>"
        mailMsg.From = "<frommailid>"
        'mailMsg.BodyFormat = MailFormat.Text 'optional for sending text in body
        mailMsg.BodyFormat = MailFormat.Html
        mailMsg.Subject = "Statistics Report"
        mailMsg.Body = "<html><body><Table><tr><td>Hi,</td></tr><tr><td>Details of the Statistics :</td></tr></Table></body></html><html><body>" & "sometext" & _
        "</body></html><html><body><Table><tr><td> </td></tr><tr><td>NOTE: This is an automated mail. Please, do not reply.</td></tr>" & _
        "<tr><td>*Green coloured rows indicates temporary demos</td></tr>" & _
        "<tr><td>**All statistics are based on the page naming conventions Eg., 22_10_2005_</td></tr>" & _
        "<tr><td> </td></tr><tr><td>Regards,</td></tr><tr><td>some text,</td></tr><tr><td>some text,</td></tr>" & _
        "<tr><td> Some text </td></tr></table></body></html>" 
        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer")
        SmtpMail.Send(mailMsg)
        'xm.InnerHtml = "Your message has been sent"
      Catch ex As Exception
        Response.Write(ex.Message)
      End Try
    End Sub

Hope this will help you.
Naresh Kumar replied to dipa ahuja on 12-Jul-11 11:05 AM
Thanks for your reply.

I need to submit the Form from Email and capture the submitted details.

I have tried with Gmail but while clicking on submit button in the email I am getting message "You are submitting information to external page! Are you sure?"

This will open the page and I am able to capture the details. when user clicks on the button it is opening the page and processing. But user should not require the page where I am capturing. Below is the code:

string toEmailAddress = "YYYY@gmail.com";

string GmailId = "YYYY@gmail.com";

string password = "XXXXXX";

string bodyMsg = "<html><head></head><body><form action='http://ServerName/Default2.aspx' id='frmget' method='GET'><p>Your Full Name?<br /><input type='text' name= 'txtName' runat='server'/><br /><input type='submit'></p></form></body></html>";

MailMessage mail = new MailMessage();

mail.To.Add(toEmailAddress);

mail.From = new MailAddress(GmailId);

mail.Subject = "Hello";

mail.Body = bodyMsg;

mail.IsBodyHtml = true;

//OR attatch from your computer

// mail.Attachments.Add(new Attachment("path of word document"));

SmtpClient smtp = new SmtpClient("smtp.gmail.com");

smtp.EnableSsl = true;

smtp.UseDefaultCredentials = false;

smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);

smtp.Send(mail);


Default2.aspx.cs:

Response.Write(Page.Request.QueryString["txtName"]);


Page Default2 should not be opened for the users, I need to capture in this way.

Any suggestions... Please guide me...

Thanks in advance.

dipa ahuja replied to Naresh Kumar on 12-Jul-11 11:47 AM
its somewhat tough and odd.. i think you should just give a url of form which contain the data for user to fill in the email

so when user will open that url he.she will get the page to fill data.

Now because now url is from your own website you can write aspx code to perform whatever you want.
Naresh Kumar replied to dipa ahuja on 13-Jul-11 02:43 AM
Thanks for your reply.

I have already suggested the same, but I need to implement in this way only as per client.

Any suggestions or links!!!

Much Appreciate your help.