Email Send in ASP.net

22
Introduction Well till now you have seen sending email from Asp.Net But not formatted text and attachments in one application. Here is what I bring forward to you complete email sending with formatting text editor like Colored, Bold, Italicized, Indented you can even have the Links and pictures and attachments in single application. The basic formatting tools and attachments are integrated in one application and also demonstrates using SMTP server and how to configure the default SMTP server. The System.Web.Mail namespace provides classes for sending email in .NET. The classes involved are MailMessage which manages the content of the mail message and MailAttachment which manages the mail attachments. And formatting of the text is done by TextEdit.js and MsgBody.htm files which are included in the SendEmail.aspx page. Configuring SMTP server

description

sending email c#

Transcript of Email Send in ASP.net

Page 1: Email Send in ASP.net

Introduction

Well till now you have seen sending email from Asp.Net But not formatted text and attachments in one application. Here is what I bring forward to you complete email sending with formatting text editor like Colored, Bold, Italicized, Indented you can even have the Links and pictures and attachments in single application.

The basic formatting tools and attachments are integrated in one application and also demonstrates using SMTP server and how to configure the default SMTP server. The System.Web.Mail namespace provides classes for sending email in .NET. The classes involved are MailMessage which manages the content of the mail message and MailAttachment which manages the mail attachments. And formatting of the text is done by TextEdit.js and MsgBody.htm files which are included in the SendEmail.aspx page.

Configuring SMTP server

1. To configure "Default SMTP Virtual Server", right-click on it, go into "Properties", and select "Access" tab, and then click the "Relay" button. With "only the list below" radio button selected, you should see the local IP address: "127.0.0.1", if it's not there, you need to add it.

Page 2: Email Send in ASP.net

2. If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer, make sure "Anonymous access is allowed". To allow access, open up the IIS. Locate the SMTP virtual server, and right-click select Properties.

3. You need to replace "localhost" with the name or IP address of your SMTP mail server. On a Windows desktop computer, "localhost" is the default value and usually works.

How the code Works

MailMessage has all required properties such as To, Subject, BCC, CC etc. For a complete list of method and properties, that you can make use of, please visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp Now before writing the actual code for sending mail we have to write the code which formats the text or prepare the text editor. For text editor we need 3 files

1)     MsgBody.htm (This file holds the text which is to be edited.)2)     Textedit.js (This file contains the code which formats the text)3)     ColorPalette.htm (An html color palette is created which is used to color the text as

shown below)

  

Steps to add the Text Editor:-In TextEdit.js the following Code displays the ColorPaletteHere I used a IFrame for this purpose and sets its property to “on”. The IFrane element functions as a document within document. we are typing the text and all formats in the inner text of the <SPAN style="FONT-SIZE: 10pt">iframe and retrieving the innerTextof the IFrame. Add the following code in SendEmail.aspx page <SCRIPT language="javascript" src="TextEdit.js"></SCRIPT>This file executes all the contents by using the <SPAN style="FONT-SIZE: 10pt">execCommand method in JavaScript.

 This below line of code in SendEmail.aspx.vb page Button Click event triggers the JavaScript to load the formatted text as the message Body. Button1.Attributes.Add("onClick", "javascript:fillTxt();")

Page 3: Email Send in ASP.net

Actual DotNet Code

We did the background work of formatting text Now we start the actual coding of sending mail by importing the namespace, "System.Web.Mail". Then, in the ButtonClick event, we create an instance of MailMessage object. It is through the MailMessage object, we set all the properties such as To, From, Subject, Body etc. We can either send a text message or a html message. We need to specify the bodyformat in the BodyFormat property. One we set all the properties, it is ready to send the email. Before sending the email, you have to set another important property, ie; SmtpServer. You have to set this property. You should assign the name of your SMTP server to this property. In most cases you can assign this as "localhost" or “127.0.0.1”. If you do not set this property, then you will not be able to send email from an ASP .NET page. Finally we send the email using SmtpMail.Send.

Button1.Attributes.Add("onClick", "javascript:fillTxt();")Dim attach1 As String = ""Dim strFileName As String = ""Dim message As New MailMessage()If (attachFile1.PostedFile.FileName <> "") Then

Dim ulFile As HttpPostedFile = attachFile1.PostedFile Dim nFileLen As Int64 = ulFile.ContentLength If (nFileLen > 0) Then

strFileName= Path.GetFileName(attachFile1.PostedFile.FileName) strFileName = "Uploads/" + strFileName

attachFile1.PostedFile.SaveAs(Server.MapPath(strFileName))Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName)) message.Attachments.Add(attach) attach1 = strFileName End IfEnd If message.From = TextBox2.Text message.To = TextBox3.Text message.Cc = txtcc.Text message.Bcc = txtbcc.Text message.Subject = TextBox4.Text message.Body = hdnmsg.Value message.BodyFormat = MailFormat.Html

SmtpMail.SmtpServer = "127.0.0.1" SmtpMail.Send(message)lblMessage.Text = "Your email has been sent"

Now, we send the email with the attachments: To send attachments, we need to add attachments using the Add method, which is available in the Attachments object. The only thing that we need to add to the above example is Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))

Enhancements that you add to the above examples.

We can add any number of attachments to an email. To send multiple attachments, just repeat the line Msg.Attachments.Add with the file that needs to be attached. Points to remember:-

Make sure that you have created a Folder “ Uploads” in the root and have full access to this folder.

Do not foget to change the Form tag property to encType="multipart/form-data" Enable your browser for JavaScript because the colorpalette is based on JavaScript.

Page 4: Email Send in ASP.net

Configure your Default SMTP Virtual Server as SmtpMail.SmtpServer = "127.0.0.1" or Localhost.

Import namespace system.web.mail

Asp.net send email using c# By: Suresh Dasari Nov 30, 2010

Categories: Asp.net, SendMail

Introduction:

In this article I will explain how to send email using asp.net.

Description:

In realtime we will use gmail,yahoo,hotmail etc to send mails to particular user here I will show to how to implement mail sending concept in asp.net.

To implement mail concept in asp.net first we need to add following reference to our application System.Web.Mail namespace 

What is System.Web.Mail

System.Web.Mail is a namespace which contains classes that enable us to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.

How we can get this reference (System.Web.Mail)

We need to add System.web.dll reference to our application for that follow below steps

a a)  On the Project menu, click Add Reference.b) On the .NET tab, locate System.Web.dll, and then click Select.

c) Click OK in the Add References.

After that design your aspx page like this

Page 5: Email Send in ASP.net

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

<head runat="server">

<title>Send Mail using asp.net</title>

</head>

<body>

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

<div>

<table style=" border:1px solid" align="center">

<tr>

<td colspan="2" align="center">

<b>Send Mail using asp.net</b>

</td>

</tr>

<tr>

<td>

From:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

Subject:

</td>

<td>

Page 6: Email Send in ASP.net

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

</td>

</tr>

<tr>

<td>

To:

</td>

<td>

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

</td>

</tr>

<tr>

<td valign="top">

Body:

</td>

<td>

<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>

</td>

</tr>

<tr>

<td>

</td>

<td>

<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />

</td>

</tr>

</table>

Page 7: Email Send in ASP.net

</div>

</form>

</body>

</html>

Now add following namcespaces in your codebehind

using System.Web.Mail;

After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e)

{

try

{

MailMessage Msg = new MailMessage();

// Sender e-mail address.

Msg.From = txtFrom.Text;

// Recipient e-mail address.

Msg.To = txtTo.Text;

Msg.Subject = txtSubject.Text;

Msg.Body = txtBody.Text;

// your remote SMTP server IP.

SmtpMail.SmtpServer = "10.20.72.1";

SmtpMail.Send(Msg);

Msg = null;

Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert)

Page 8: Email Send in ASP.net

{ window.location='SendMail.aspx';}</script>");

}

catch (Exception ex)

{

Console.WriteLine("{0} Exception caught.", ex);

}

}

Here use your SMTP server address then only it will work for you and check mails in spam also because sometimes mails should be appear in spam folder also.

Demo

Page 9: Email Send in ASP.net

how to send mail with attachement in asp.net By: Suresh Dasari Dec 2, 2010

Categories: Asp.net, Fileupload, SendMail

Introduction:

In this article I will explain how to send email with attachment using asp.net.

Description:

In previous post I explained how to send mail in asp.net now I will explain how to implement mail sending concept with attachment in asp.net.

Page 10: Email Send in ASP.net

To implement this concept first we need to following reference to our application System.Web.Mail namespace 

What is System.Web.Mail

The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.How we can get this reference (System.Web.Mail) 

To add this reference follow below steps.

a        a) On the Project menu, click Add Reference.b) On the .NET tab, locate System.Web.dll, and then click Select.

c) Click OK in the Add References.

After that design your aspx page like this 

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

<head id="Head1" runat="server">

<title>Send Mail using asp.net</title>

</head>

<body>

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

<div>

<table style=" border:1px solid" align="center">

<tr>

<td colspan="2" align="center">

<b>Send Mail with Attachment using asp.net</b>

</td>

</tr>

Page 11: Email Send in ASP.net

<tr>

<td>

From:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

To:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

Subject:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

Attach a file:

Page 12: Email Send in ASP.net

</td>

<td>

<asp:FileUpload ID="fileUpload1" runat="server" />

</td>

</tr>

<tr>

<td valign="top">

Body:

</td>

<td>

<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>

</td>

</tr>

<tr>

<td>

</td>

<td>

<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

Now add following namcespace in codebehind

Page 13: Email Send in ASP.net

using System.Web.Mail;

After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e)

{

try

{

MailMessage Msg = new MailMessage();

// Sender e-mail address.

Msg.From = txtFrom.Text;

// Recipient e-mail address.

Msg.To = txtTo.Text;

// Subject of e-mail

Msg.Subject = txtSubject.Text;

if (fileUpload1.HasFile)

{

// File Upload path

String FileName = fileUpload1.PostedFile.FileName;

//Getting Attachment file

MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64);

//Attaching uploaded file

Msg.Attachments.Add(mailAttachment);

}

Msg.Body = txtBody.Text;

// your remote SMTP server IP.

Page 14: Email Send in ASP.net

SmtpMail.SmtpServer = "10.120.0.21";

SmtpMail.Send(Msg);

Msg = null;

Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMailWithAttachment.aspx';}</script>");

}

catch (Exception ex)

{

Console.WriteLine("{0} Exception caught.", ex);

}

}

Demo

Page 15: Email Send in ASP.net

C# - Send Email using Gmail in Asp.net with Gmail SMTP By: Suresh Dasari Dec 10, 2010

Categories: Asp.net, SendMail

Introduction:

 In this article I will explain how to send email using Gmail account or credentials in asp.net using c#.

Description:

In our system if we don’t have smtp server for sending mails at that time we can send mails by using gmail smtp server in asp.net. I will show to how to implement mail sending concept

Page 16: Email Send in ASP.net

using Gmail credentials in asp.net. To implement this concept first you need to enable POP enable in your Gmail account for this SettingsForwarding and POP/IMAP

To implement this mail concept in your asp.net application first we need to add this reference to our application System.Web.Mail namespace

What is System.Net.Mail

The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

How we can get this reference (System.Net.Mail)

for that we need to add System.web.dll reference to our application.

a)    On the Project menu, click Add Reference.

b)    On the .NET tab, locate System.Web.dll, and then click Select.

c)    Click OK in the Add References.

After that design your aspx page like this

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

<head id="Head1" runat="server">

<title>Send Mail using with gmail crendentials in asp.net</title>

</head>

<body>

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

<div>

<table style=" border:1px solid" align="center">

<tr>

<td colspan="2" align="center">

<b>Send Mail using gmail credentials in asp.net</b>

</td>

Page 17: Email Send in ASP.net

</tr>

<tr>

<td>

Gmail Username:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

Gmail Password:

</td>

<td>

<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>

</td>

</tr>

<tr>

<td>

Subject:

</td>

<td>

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

</td>

</tr>

<tr>

<td>

Page 18: Email Send in ASP.net

To:

</td>

<td>

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

</td>

</tr>

<tr>

<td valign="top">

Body:

</td>

<td>

<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>

</td>

</tr>

<tr>

<td>

</td>

<td>

<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />

</td>

</tr>

</table>

</div>

</form>

</body>

</html>

Page 19: Email Send in ASP.net

After that add this namcespace in your codebehind

using System.Net.Mail;

After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e)

{

try

{

MailMessage Msg = new MailMessage();

// Sender e-mail address.

Msg.From = new MailAddress(txtUsername.Text);

// Recipient e-mail address.

Msg.To.Add(txtTo.Text);

Msg.Subject = txtSubject.Text;

Msg.Body = txtBody.Text;

// your remote SMTP server IP.

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.Port = 587;

smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);

smtp.EnableSsl = true;

smtp.Send(Msg);

Msg = null;

Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert)

Page 20: Email Send in ASP.net

{ window.location='SendMail.aspx';}</script>");

}

catch (Exception ex)

{

Console.WriteLine("{0} Exception caught.", ex);

}

}

                   

Here we used smtp.Port=587 this is the port number which is used by gmail that’s why we used this port number. In some situations if you get any error regarding security remove smtp.EnableSsl = true;

Demo