Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

how to send email with attachment from local machine

0.00/5 (No votes)
10 Nov 2011 1  
using System.Net.Mail;public partial class SUPER_mail : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {    }   

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

using System.Net.Mail;

public partial class SUPER_mail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
      
      
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("your gmail id");
        msg.To.Add(TextBox2.Text);
        msg.Subject = TextBox3.Text;
        msg.Body = TextBox4.Text;
 if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs("C:\\Program Files (x86)\\Common Files\\microsoft shared\\DevServer\\10.0\\" + FileUpload1.FileName);
            Label3.Text = FileUpload1.FileName;
  msg.Attachments.Add(new Attachment(Label3.Text));
        }

     
        SmtpClient smt = new SmtpClient();
        smt.Send(msg);
        Label2.Text = "mail sucessfully transfer";
    }
}

 

//inside the web.config file

<system.net >
    <mailSettings >
      <smtp >
        <network host ="smtp.gmail.com"  password ="your gmail password" userName ="your gmail id"/>
      </smtp>
    </mailSettings>
  </system.net>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here