Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Sending Email with Attachment using ASP.Net
WhatsApp
Pintoo Yadav
Apr 23
2015
1.3
k
0
0
<
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
Up Next
Sending Email with Attachment using ASP.Net