umair mohsin

umair mohsin

  • 1.4k
  • 387
  • 66.6k

having issue in sending a mail message

Aug 1 2015 5:17 AM
  i am having a n issue is sending a mail message using asp,net web application.
 
i have applied all the things which i could do but there is an error always occur.
here is my code.can anyone please guide me
 
UI
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoContact Us Form.aspx.cs" Inherits="DemoContact_Us_Form" %>
<!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></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 400px;
}
.style3
{
width: 87px;
}
.style4
{
width: 87px;
height: 458px;
}
.style5
{
width: 400px;
height: 458px;
}
.style6
{
height: 458px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td class="style3">
<asp:Label ID="Label1" runat="server" Text="Full name:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtname" runat="server" Width="226px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtname" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label2" runat="server" Text="Email:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtmail" runat="server" Height="24px" Width="225px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtmail" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtmail" Display="Dynamic"
ErrorMessage="Please enter a valid email" ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label3" runat="server" Text="Subject:"></asp:Label>
</td>
<td class="style2">
<asp:TextBox ID="txtsubject" runat="server" Height="26px" Width="222px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtsubject" ErrorMessage="Required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="Label4" runat="server" Text="Comments:"></asp:Label>
</td>
<td class="style5">
<asp:TextBox ID="txtcommnets" runat="server" Height="415px" Width="371px"
TextMode="MultiLine"></asp:TextBox>
</td>
<td class="style6">
&nbsp;</td>
</tr>
<tr>
<td class="style3" colspan="3">
<asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" />
</td>

</tr>
<tr>
<td class="style3" colspan="3">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</td>


</tr>
<tr>
<td class="style3">
&nbsp;</td>
<td class="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>

</div>

</form>
</body>
</html>
 
 
 
 
 
code behind
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

public partial class DemoContact_Us_Form : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label5.Text = "";



}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (Page.IsValid)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(txtmail.Text);
mm.To.Add("[email protected]");
mm.Subject = txtsubject.Text;
mm.Body = txtcommnets.Text;
mm.IsBodyHtml = true;

SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com";
client.Credentials = new System.Net.NetworkCredential("","");
client.UseDefaultCredentials = true;
client.Port = 25;
client.EnableSsl = true;
client.Send(mm);
Label5.ForeColor = System.Drawing.Color.BlueViolet;
Label5.Text = " thank you for contacting us your mail has received ";

txtmail.Enabled = false;
txtname.Enabled = false;
txtsubject.Enabled = false;
txtcommnets .Enabled = false;
}

}
catch (Exception ex)
{
Label5.ForeColor = System.Drawing.Color.Red;
Label5.Text = " there is a problem sending your email.please try again later ";

}

}
 
i have entered password and username is network credentials.but it still throw an error .please help me in this
 
 
 

Answers (29)