Sunday, August 18, 2013

Email with attachment

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.    }

No comments:

Post a Comment