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
01
using
System;
02
using
System.Data;
03
using
System.Configuration;
04
using
System.Web;
05
using
System.Web.Security;
06
using
System.Web.UI;
07
using
System.Web.UI.WebControls;
08
using
System.Web.UI.WebControls.WebParts;
09
using
System.Web.UI.HtmlControls;
10
using
System.Net.Mail;
11
12
public
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
01
Imports
System
02
Imports
System.Data
03
Imports
System.Configuration
04
Imports
System.Web
05
Imports
System.Web.Security
06
Imports
System.Web.UI
07
Imports
System.Web.UI.WebControls
08
Imports
System.Web.UI.WebControls.WebParts
09
Imports
System.Web.UI.HtmlControls
10
Imports
System.Net.Mail
11
12
Public
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
39
End
Class
Hope this helps
0 comments:
Post a Comment