Wednesday, February 15, 2012

Send Email With Attachment in asp.net

In this example i am going to describe how to send email with attachment in ASP.NET using fileUpload Control.
I am saving the uploaded file into memory stream rather then saving it on server.




And for this example i m using Gmail SMTP server to send mail, this code also works fine with any SMTP Server.

For sending Email in ASP.NET , first of all we need to add Syatem.Net.Mail namespace in code behind of aspx page. 

In my previous article i describe how to send mail using gmail in asp.net 





Create the page as shown in the image above, put textbox for message and FileUpload Control for adding the file attachment.

Write below mentioned code in click event of Send button in Code behind of page

C# Code Behind
01using System;
02using System.Data;
03using System.Configuration;
04using System.Web;
05using System.Web.Security;
06using System.Web.UI;
07using System.Web.UI.WebControls;
08using System.Web.UI.WebControls.WebParts;
09using System.Web.UI.HtmlControls;
10using System.Net.Mail;
11 
12public partial class _Default : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16 
17    }
18    protected void btnSend_Click(object sender, EventArgs e)
19    {
20        MailMessage mail = new MailMessage();
21        mail.To.Add(txtTo.Text);
22        //mail.To.Add("amit_jain_online@yahoo.com");
23        mail.From = new MailAddress(txtFrom.Text);
24        mail.Subject = txtSubject.Text;
25        mail.Body = txtMessage.Text;
26        mail.IsBodyHtml = true;
27 
28        //Attach file using FileUpload Control and put the file in memory stream
29        if (FileUpload1.HasFile)
30        {
31            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
32        }
33        SmtpClient smtp = new SmtpClient();
34        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
35        smtp.Credentials = new System.Net.NetworkCredential
36             ("YourGmailID@gmail.com", "YourGmailPassword");
37        //Or your Smtp Email ID and Password
38        smtp.EnableSsl = true;
39        smtp.Send(mail);
40 
41    }
42}

VB.NET Code Behind
01Imports System
02Imports System.Data
03Imports System.Configuration
04Imports System.Web
05Imports System.Web.Security
06Imports System.Web.UI
07Imports System.Web.UI.WebControls
08Imports System.Web.UI.WebControls.WebParts
09Imports System.Web.UI.HtmlControls
10Imports System.Net.Mail
11 
12Public Partial Class _Default
13    Inherits System.Web.UI.Page
14    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
15 
16    End Sub
17    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
18        Dim mail As New MailMessage()
19        mail.[To].Add(txtTo.Text)
20        'mail.To.Add("amit_jain_online@yahoo.com");
21        mail.From = New MailAddress(txtFrom.Text)
22        mail.Subject = txtSubject.Text
23        mail.Body = txtMessage.Text
24        mail.IsBodyHtml = True
25 
26        'Attach file using FileUpload Control and put the file in memory stream
27        If FileUpload1.HasFile Then
28            mail.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName))
29        End If
30        Dim smtp As New SmtpClient()
31        smtp.Host = "smtp.gmail.com"
32        'Or Your SMTP Server Address
33        smtp.Credentials = New System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword")
34        'Or your Smtp Email ID and Password
35        smtp.EnableSsl = True
36 
37        smtp.Send(mail)
38    End Sub
39End Class
Build and run the application to test the code

Hope this helps

0 comments:

Post a Comment