ASP.NET Email Attachment

The System.Net namespace comprises a range of classes that facilitate communication with other applications through protocols such as HTTP, TCP, UDP, and sockets. In a previous chapter, we explored how to send SMTP emails from ASP.NET, focusing on sending emails with a text body. In this chapter, our objective is to extend this functionality and enable the sending of emails with attached files.

System.Net.Mail.Attachment

To accomplish this, we utilize the System.Net.Mail.Attachment class in conjunction with the MailMessage class. The Attachment class allows us to associate a file or content with an email message. The content of the attachment can be specified as a String, Stream, or file name, depending on the source and type of content we intend to attach. We have the flexibility to define the attachment content using any of the available constructors provided by the Attachment class.

email-attachment

By employing the Attachment class, we can seamlessly integrate attachments into our email messages. Whether the attachment is a string representing the content, a stream providing direct access to the content, or a file name referencing an external file, the Attachment class provides a versatile and straightforward means of including attachments in our emails.

VB.Net
Dim attachment As System.Net.Mail.Attachment attachment = New System.Net.Mail.Attachment(attachmentFile) mail.Attachments.Add(attachment)
C#
System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(attachmentFile); mail.Attachments.Add(attachment);

The following ASP.NET program shows how to send an email with an attachment from a Gmail address . The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 . Here we are using NetworkCredential for password based authentication.

VB.Net
SmtpServer.Port = 587 SmtpServer.Credentials = New System.Net.NetworkCredential("gamil-username", "gmail-passowrd") SmtpServer.EnableSsl = True
C#
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

The above code you have to replace username and password with you gmail user name and password. Also you have to provide the from address as your gmail address.

VB.Net
mail.From = New MailAddress("your-gamila-ddress@gmail.com")
C#
mail.From = new MailAddress("you-email-address@gmail.com");

There is file upload control using in this program for uploading the file from your system to web server.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label2" runat="server" Text="To : "></asp:Label>             <asp:TextBox ID="to_txt" runat="server" Width="200px"></asp:TextBox> <br /><br /> <asp:Label ID="Label3" runat="server" Text="Subject :"></asp:Label>     <asp:TextBox ID="subject_txt" runat="server" Width="200px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label4" runat="server" Text="Message :"></asp:Label> <br /> <asp:TextBox ID="message_txt" runat="server" Height="117px" TextMode="MultiLine" Width="339px"></asp:TextBox> <br /> <br /> <asp:Label ID="Label5" runat="server" Text="Select Upload File"></asp:Label>    <asp:FileUpload ID="FileUpload1" runat="server" /> <br /> <br /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send Mail" Width="123px" /> <br /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Full Source | C#
using System; using System.Net.Mail; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { string attachmentFile = null ; if (FileUpload1.HasFile) { try { FileUpload1.SaveAs("C:\\files\\" + FileUpload1.FileName); attachmentFile = FileUpload1.PostedFile.FileName; } catch (Exception ex) { Label1.Text = "File Upload Failed !! " + ex.Message.ToString(); } try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("your-gamila-ddress@gmail.com"); //you have to provide your gmail address as from address mail.To.Add(to_txt.Text); mail.Subject = subject_txt.Text; mail.Body = message_txt.Text; System.Net.Mail.Attachment attachment; attachment = new System.Net.Mail.Attachment(attachmentFile); mail.Attachments.Add(attachment); SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("gamil-username", "gmail-passowrd"); //you have to provide you gamil username and password SmtpServer.EnableSsl = true; SmtpServer.Send(mail); Label1.Text = "Email successfully sent."; } catch (Exception ex) { Label1.Text = "Mail Send Failed !! " + ex.Message.ToString(); } } else { Label1.Text = "Please select a file for uploading"; } } }
Full Source | VB.NET
Imports System.Net.Mail Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim attachmentFile As String = Nothing If FileUpload1.HasFile Then Try FileUpload1.SaveAs("C:\files\" + FileUpload1.FileName) attachmentFile = FileUpload1.PostedFile.FileName Catch ex As Exception Label1.Text = "File Upload Failed !! " + ex.Message.ToString() End Try Try Dim mail As New MailMessage() Dim SmtpServer As New SmtpClient("smtp.gmail.com") mail.From = New MailAddress("your-gamila-ddress@gmail.com") 'you have to provide your gmail address as from address mail.[To].Add(to_txt.Text) mail.Subject = subject_txt.Text mail.Body = message_txt.Text Dim attachment As System.Net.Mail.Attachment attachment = New System.Net.Mail.Attachment(attachmentFile) mail.Attachments.Add(attachment) SmtpServer.Port = 587 SmtpServer.Credentials = New System.Net.NetworkCredential("gamil-username", "gmail-passowrd") 'you have to provide you gamil username and password SmtpServer.EnableSsl = True SmtpServer.Send(mail) Label1.Text = "Email successfully sent." Catch ex As Exception Label1.Text = "Mail Send Failed !! " + ex.Message.ToString() End Try Else Label1.Text = "Please select a file for uploading" End If End Sub End Class

Conclusion

The System.Net namespace encompasses classes that enable communication with external applications using various protocols. In this particular chapter, we focus on sending emails with attachments by utilizing the System.Net.Mail.Attachment class in conjunction with the MailMessage class. This combination empowers developers to incorporate diverse attachment types and sources, allowing for the seamless transmission of emails with attached files.