Advance Email Sending(With Attachment) Code(C#.NET with ASP.NET) using gmail

 Advance Email Sending Option , with attachement.
Task: Sending Email with attachment using c# code in website/ web application.
Description: Some time we need to a website viewer can send a enquiry with some attachment as  some kind of career option where a candidate can submit his/her resume and the website administrator receive the same within email.

As in my previous post I discuss about a email notification without attachment option , Now we require a email with attachment, I use below code for sending email with attachment . Here I use two fileupload control for uploading attachment file (here I am not checking the extension of file / as you can check it first before sending email ).

// Below Function can be used for sending a email with attachment option using c# code
//  using gmail crdential
using System.Net.Mail;
public bool mailattachement2(string to, string replyto, string body, string subject, FileUpload f1, FileUpload f2)
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("","Client Enquiry");
        mail.Subject = subject;
        mail.Body = body;
        MailAddress rt = new MailAddress(replyto);
        mail.ReplyTo = rt;
        //mail.Sender = rt;
        mail.IsBodyHtml = true;
        if (f1.HasFile)
            mail.Attachments.Add(new Attachment(f1.PostedFile.InputStream, f1.FileName)); //add the attachment
        if (f2.HasFile)
            mail.Attachments.Add(new Attachment(f2.PostedFile.InputStream, f2.FileName)); //add the attachment

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "";
        smtp.Credentials = new System.Net.NetworkCredential("", "password");
// Set the one email id and its password for authentication )
// email goes via using above email id...

        smtp.Port = 587;
        smtp.EnableSsl = true;
            return (true);
        catch (Exception ex)
            return (false);


  1. Very Good Code ...
    Thank You So Much Buddy ... :-)

  2. Replies
    1. This blog is for developer, & I think above code is not a complicated code for explanation...

  3. Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct

    1. If you just copy & paste this code then it will be give you error...:-)
      you have to add some namespaces also & a developer should be know that how can add these stuff...


  4. please any idea on how i an create birthday update reminder in c#?

    1. In for webapplication is not found any solution, But with window application using Task Schedular you can create any kind of reminder service or any automated Task.

  5. how to give the username & password in web.config