Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help

ADO.Net

How to Send Email with Attachment using ASP.NET 2.0 and VB.NET
This tutorial will show you how to send a simple email message with an attachment using ASP.NET 2.0 and VB.NET

Sending a email with an attachment using ASP.NET 2.0 and VB.NET is actually very simple.

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment.

Imports System.Net.Mail

We use the btnSubmit_Click event to do the work.

We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page.

The txtAttachmentPath.Text Texbox provides the path to the file to attach to the email message.

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Try

Dim SendFrom As MailAddress = New MailAddress(txtFrom.Text)
Dim SendTo As MailAddress = New MailAddress(txtTo.Text)

Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

MyMessage.Subject = txtSubject.Text
MyMessage.Body = txtBody.Text

Dim attachFile As New Attachment(txtAttachmentPath.Text)
MyMessage.Attachments.Add(attachFile)

Dim emailClient As New SmtpClient(txtSMTPServer.Text)
emailClient.Send(MyMessage)

litStatus.Text = "Message Sent"

Catch ex As Exception
litStatus.Text = ex.ToString()
End Try

End Sub



The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;To</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox>
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;From</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">&nbsp;SMTP Server</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Subject</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">&nbsp;Attachment</td>
<td bgcolor="#FFFFFF">
<asp:TextBox ID="txtAttachmentPath" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Body</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Action</td>
<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Status</td>
<td bgcolor="#FFFFFF" class="basix"><asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
</tr>
</table>



The flow for the code behind page is as follows.

Imports System.Net.Mail

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Try

Dim SendFrom As MailAddress = New MailAddress(txtFrom.Text)
Dim SendTo As MailAddress = New MailAddress(txtTo.Text)

Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

MyMessage.Subject = txtSubject.Text
MyMessage.Body = txtBody.Text

Dim attachFile As New Attachment(txtAttachmentPath.Text)
MyMessage.Attachments.Add(attachFile)

Dim emailClient As New SmtpClient(txtSMTPServer.Text)
emailClient.Send(MyMessage)

litStatus.Text = "Message Sent"

Catch ex As Exception
litStatus.Text = ex.ToString()
End Try

End Sub
End Class


Published Saturday, February 02, 2008 4:54 PM by admin

Filed under: , , , , , ,

Comments

No Comments
Anonymous comments are disabled
 
Powered by Community Server, by Telligent Systems