Send Email with Attachment using ASP.NET 4.0 and VB.NET
In this tutorial you will learn that sending an email with an attachment using ASP.NET 4.0 and VB is actually very simple and easy to accomplish.
First, you will need to import the System.Net.Mail namespace.
We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud hosting!
The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment.
The front end .aspx page will use a table to hold all the information. We will be using input fields to accept all the information and declare the variables for our codebehind:
We use the btnSubmit_Click event to do the work. To access the code behind go into design view and double click on the button. This should allow you to enter the code behind for the button.
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. For this tutorial we chose to simply have the user type the path manually, but you can experiment on different ways to accomplish this effect.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!
In this tutorial we learned about Sending Email with Attachment using ASP.NET 4.0 and VB. While this tutorial was fairly simple, it is left up to you to expand upon the ideas and develop some awesome applications.
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!
SendEmailAttachmentVB2005.zip
First, you will need to import the System.Net.Mail namespace.
We stand behind Server Intellect and their support team. They offer dedicated servers , and they are now offering cloud hosting!
The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment.
Code Block
Default.aspx.vb
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { string strMessage = "Welcome to the Page_Load method!"; Response.Write(strMessage); } }
Code Block
Default.aspx
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">
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">
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">
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">
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">
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">
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>
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. For this tutorial we chose to simply have the user type the path manually, but you can experiment on different ways to accomplish this effect.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!
Code Block
Default.aspx.vb
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
We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!
SendEmailAttachmentVB2005.zip
