Friday, July 8, 2011

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>

No comments:

Post a Comment

Your project is not referencing the "MonoAndroid,Version=v8.1" framework. Add a reference to "MonoAndroid,Version=v8.1" in the "frameworks" section of your project.json, and then re-run NuGet restore.

I had  the same problem. Reason: file obj/project.assets.json target "MonoAndroid,Version=v7.1" when my project target androi...