Skip to main content

Sending Email in C#

To send email in C# do following:


//NameSpace used


using System.Net.Mail;


//
//
MailMessage objEmail = new MailMessage();

objEmail.From = new MailAddress("me@MyCompany.com", "MY Name");

objEmail.To.Add("To@MyCompany.com");

objEmail.To.Add("To2@MyCompany.com");

objEmail.CC.Add("CC@MyCompany.com");

objEmail.CC.Add("CC2@MyCompany.com");

objEmail.Bcc.Add("BCC@MyCompany.com");

objEmail.Bcc.Add("BCC2@MyCompany.com");

objEmail.Subject = "Enter Subject here";

objEmail.Body = "Email body here";

objEmail.IsBodyHtml = true;





SmtpClient objEmailSender = new SmtpClient("Mail Server Name/IP address here");

//Send email

objEmailSender.Send(objEmail);

//////////////////////////////////////////////////////

Following code can be pasted to your ASP.net WebForm page
<%@ Import Namespace="System.Net.Mail" %> <%@ Import Namespace="System.Web.Mail" %>


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

<script runat="server">
protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)

{
txtMailServerName.Text = "Mail Server Name Here";txtMailFrom.Text = "Email from here";

}

}


protected void btnSendMal_Click(object sender, EventArgs e)

{





try

{
System.Net.Mail.MailMessage objEmail = new System.Net.Mail.MailMessage();objEmail.From = new MailAddress(txtMailFrom.Text, txtMailFrom.Text);

objEmail.To.Add(txtMailTo.Text);

objEmail.Subject = txtSubject.Text;

objEmail.Body = txtBody.Text;
System.Net.Mail.SmtpClient objEmailSender = new SmtpClient(txtMailServerName.Text);

//Send email

objEmailSender.Send(objEmail);
Response.Write("Your Mail has been send");

}
catch (Exception ex)

{

Response.Write(ex.Message + ex.Source);

}

}




protected void btnSendMal_Click2(object sender, EventArgs e)

{





try

{
System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();

msg.To = txtMailTo.Text;

msg.From = txtMailFrom.Text;

//msg.BodyFormat = MailFormat.Text;

//msg.Priority = MailPriority.Normal;

msg.Subject = txtSubject.Text;

msg.Body = txtBody.Text;
System.Web.Mail.SmtpMail.SmtpServer = txtMailServerName.Text;

System.Web.Mail.SmtpMail.Send(msg);Response.Write(
"Your Mail has been send");

}
catch (Exception ex)

{

Response.Write(ex.Message + ex.Source);

}

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

<head runat="server">

<title>Mail Test Page</title>

<style type="text/css">
#form1 {height: 239px;

}

</style></
head>

<body>



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

<table>

<tr>

<td>

<asp:Label ID="Label1" runat="server" Text="Mail From: "></asp:Label>

</td>

<td>

<asp:TextBox ID="txtMailFrom" runat="server" Width="233px"></asp:TextBox>

</td>

</tr>

<tr>

<td>

<asp:Label ID="Label2" runat="server" Text="Mail To: "></asp:Label>

</td>

<td>

<asp:TextBox ID="txtMailTo" runat="server" Width="232px"></asp:TextBox>

</td>

</tr>

<tr>

<td>

<asp:Label ID="Label3" runat="server" Text="Subject: "></asp:Label>

</td>

<td>

<asp:TextBox ID="txtSubject" runat="server" Width="232px"></asp:TextBox>

</td>

</tr>

<tr>

<td>

<asp:Label ID="Label4" runat="server" Text="Body: "></asp:Label>

</td>

<td>

<asp:TextBox ID="txtBody" runat="server" Width="230px"></asp:TextBox>

</td>

</tr>

<tr>

<td>

<asp:Label ID="Label5" runat="server" Text="Mail Server Name/IP address here: "></asp:Label>

</td>

<td>

<asp:TextBox ID="txtMailServerName" runat="server" Width="232px"></asp:TextBox>

</td>
</tr>



<tr>

<td>

</td>

<td>
<asp:Button ID="btnSendMail" onclick="btnSendMal_Click" runat="server" Text="Send Mail(System.Net.Mail)" />

</td>

</tr>

<tr>

<td>

</td>

<td>
<asp:Button ID="Button1" onclick="btnSendMal_Click2" runat="server" Text="Send Mail(System.Web.Mail)" />

</td>

</tr>

</table>

</form>


</body>

</html>

Comments

Popular posts from this blog

WPF - Checking Cap Lock Status in WPF

Checking Cap Lock status will be useful in Logon page where we can provide warning to user <Caps Lock is on. Having Caps Lock on may cause you to enter password incorrectly.>

Following sample uses the Control class that is a standard class within the System.Windows.Forms namespace. The DLL containing this namespace is automatically included in Windows Forms applications. The class includes a method named IsKeyLocked, which allows you to determine whether keys such as Caps Lock are switched on or off. To check the status of the Caps Lock key, you can use the method in the following manner:

Example:
privatevoid KeyDownEventHanlder(object sender, KeyEventArgs e)
{
if (Console.CapsLock == true)
{
lblError.Foreground = Brushes.Red;
lblError.Content = "Caps Lock is on.";// Having Caps Lock on may cause you to enter password incorrectly.";

}

else

{
if (lblError.Foreground == Brushes.Red)lblError.Foreground = Brushes.Transparent;
}

}

Error CS0234 The type or namespace name 'ApplicationInsights' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Error CS0234 The type or namespace name 'ApplicationInsights' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
To fix this issue run the following command in the Package Manager Console 
Install-Package Microsoft.ApplicationInsights.WindowsApps