Hello Forum,
After I created a contact form where users can reach out to the website owner via message to the owner’s email. I tried to test it by sending message by the message.
It is working. it sent the message with the sender as me to my own mail but from a user
Here is my HTML and CODE. I also have my web.config file where mail settings are set
HMTL
<div class="col"><br />
<div class="page">
<div class="form-group">
<div class="col-xs-11">
<asp:Label ID="Label1" runat="server" Font-Bold="true" CssClass="col-md-2 control-label" Text="What's your full Name"></asp:Label>
<div class="col-md-3">
<asp:TextBox ID="Nametext" CssClass="form-control" runat="server" Font-Size="Small" Width="300px" placeholder="e.g Bola Mark"></asp:TextBox>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-11">
<asp:Label ID="Label4" runat="server" Font-Bold="true" CssClass="col-md-2 control-label" Text="Tell us your number"></asp:Label>
<div class="col-md-3">
<asp:TextBox ID="phonetext" CssClass="form-control" runat="server" Font-Size="Small" Width="300px" placeholder="+234 8124550987"></asp:TextBox>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-11">
<asp:Label ID="Label2" runat="server" Font-Bold="true" CssClass="col-md-2 control-label" Text="Email Address"></asp:Label>
<div class="col-md-3">
<asp:TextBox ID="emtext" CssClass="form-control" runat="server" Font-Size="Small" Width="300px" placeholder="youremail@mail.com"></asp:TextBox>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-11">
<asp:Label ID="Label6" runat="server" Font-Bold="true" CssClass="col-md-2 control-label" Text="Subject"></asp:Label>
<div class="col-md-3">
<asp:TextBox ID="subjectxt" CssClass="form-control" runat="server" Font-Size="Small" Width="300px" placeholder="e.g message Title"></asp:TextBox>
</div>
</div>
</div>
<div class="form-group">
<div class="col-xs-11">
<asp:Label ID="Label3" runat="server" Font-Bold="true" CssClass="col-md-2 control-label" Text="Your message"></asp:Label>
<div class="col-md-3">
<asp:TextBox ID="Textmsg" CssClass="form-control" runat="server" placeholder="type your message here" Width="300px" Font-Size="Small" TextMode="MultiLine" Style="overflow: hidden; resize: none;" oninput="Resize(this)"/>
<script type="text/javascript">
function Resize(textbox) {
textbox.style.height = "";
textbox.style.height = Math.min(textbox.scrollHeight, 300) + "px";
}
</script>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-2"></div>
<div class="col-md-6">
<asp:Button ID="btnreg" runat="server" Text="Send message" BackColor="#12254d" Font-Size="Larger" margin-left="100px" class="btn btn-primary navbar-btn" Width="295px" OnClick="btnreg_Click" />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<br />
<br />
</div>
</div>
</div>
</div>
CODE
protected void btnreg_Click(object sender, EventArgs e)
{
SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
using (MailMessage mm = new MailMessage(smtpSection.From, "georgeakpan13@gmail.com"))
{
mm.Subject = subjectxt.Text.Trim();
mm.Body = "Name: " + Nametext.Text + "<br /><br />Email: " + emtext.Text + "<br />" + Textmsg.Text;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = smtpSection.Network.Host;
smtp.EnableSsl = smtpSection.Network.EnableSsl;
NetworkCredential networkCred = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials;
smtp.Credentials = networkCred;
smtp.Port = smtpSection.Network.Port;
smtp.Send(mm);
}
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Thank you, We'll get back to you soon";
}
WEB.CONFIG
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="georgeakpan13@gmail.com">
<network host="smtp.gmail.com" port="587" enableSsl="true" userName="emailaddress@gmail.com" password="password" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>