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
Build and run the application to test the code
Hope this helps
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 stream29 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 Address35 smtp.Credentials = new System.Net.NetworkCredential36 ("YourGmailID@gmail.com", "YourGmailPassword");37 //Or your Smtp Email ID and Password38 smtp.EnableSsl = true;39 smtp.Send(mail);40 41 }42}VB.NET Code Behind
01Imports System02Imports System.Data03Imports System.Configuration04Imports System.Web05Imports System.Web.Security06Imports System.Web.UI07Imports System.Web.UI.WebControls08Imports System.Web.UI.WebControls.WebParts09Imports System.Web.UI.HtmlControls10Imports System.Net.Mail11 12Public Partial Class _Default13 Inherits System.Web.UI.Page14 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)15 16 End Sub17 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.Text23 mail.Body = txtMessage.Text24 mail.IsBodyHtml = True25 26 'Attach file using FileUpload Control and put the file in memory stream27 If FileUpload1.HasFile Then28 mail.Attachments.Add(New Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName))29 End If30 Dim smtp As New SmtpClient()31 smtp.Host = "smtp.gmail.com"32 'Or Your SMTP Server Address33 smtp.Credentials = New System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword")34 'Or your Smtp Email ID and Password35 smtp.EnableSsl = True36 37 smtp.Send(mail)38 End Sub39End ClassHope this helps
0 comments:
Post a Comment