TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Sending Email with Attachment using ASP.Net
Pintoo Yadav
Apr 23
2015
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<
table
border
=
"0"
cellpadding
=
"0"
cellspacing
=
"0"
>
<
tr
>
<
td
style
=
"width: 80px"
>
To:
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtTo"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
>
Subject:
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtSubject"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
valign
=
"top"
>
Body:
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtBody"
runat
=
"server"
TextMode
=
"MultiLine"
Height
=
"150"
Width
=
"200"
><
/
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
>
File Attachment:
</
td
>
<
td
>
<
asp:FileUpload
ID
=
"fuAttachment"
runat
=
"server"
/>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
>
Gmail Email:
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtEmail"
runat
=
"server"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
>
Gmail Password:
</
td
>
<
td
>
<
asp:TextBox
ID
=
"txtPassword"
runat
=
"server"
TextMode
=
"Password"
>
</
asp:TextBox
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
</
tr
>
<
tr
>
<
td
>
</
td
>
<
td
>
<
asp:Button
Text
=
"Send"
OnClick
=
"SendEmail"
runat
=
"server"
/>
</
td
>
</
tr
>
</
table
>
using
System.IO;
using
System.Net;
using
System.Net.Mail;
protected
void
SendEmail(
object
sender, EventArgs e)
{
using
(MailMessage mm =
new
MailMessage(txtEmail.Text, txtTo.Text))
{
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if
(fuAttachment.HasFile)
{
string
FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(
new
Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml =
false
;
SmtpClient smtp =
new
SmtpClient();
smtp.Host =
"smtp.gmail.com"
;
smtp.EnableSsl =
true
;
NetworkCredential NetworkCred =
new
NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials =
true
;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(),
"alert"
,
"alert('Email sent.');"
,
true
);
}
}
Sending Email using ASP.NET
ASP.NET
Sending Email with Attachment