Friday, August 31, 2012

sending email with attachment file in .net


First Step, we have ASP.NET applications you configure this in your application’s web.config file.
Config code
  <system.net>
    <mailSettings>
      <smtp from="anil.singh@gmail.com" deliveryMethod="Network">
        <network host="smtp.gmail.com" port="587" userName=anil.singh@gmail.com password="password#123" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
  </system.net>

Second Step, c# code
using System.Net.Mail;
protected void btnMailSend_Click(object sender, EventArgs e)
        {
            try
            {
                string contributionFile = string.Empty;
                if (flAttachment.HasFile && Path.GetExtension(flAttachment.FileName) == ".xls")
                {
                    flAttachment.SaveAs(Server.MapPath("~/FolderPath/") + flAttachment.FileName);
contributionFile = Server.MapPath("~/FolderPath/") + flAttachment.FileName;
                }
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress("anil.singh@gmail.com");

                mm.To.Add(txtMailTo.Text);
                mm.Subject = "This is your File" + monthName;
                mm.Body = txtMailMessage.Text;
                mm.IsBodyHtml = true;
                mm.Priority = MailPriority.High;
                Attachment attach = new Attachment(contributionFile);
                mm.Attachments.Add(attach);
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential("anil.singh@gmail.com", "password#123");
                client.Send(mm);
                client.Timeout = 120;
            }
            catch (Exception ex)
            {

            }
        }




If you want to send this mail to Different person at a time, then we write to this..

using System.Net.Mail;
protected void btnMailSend_Click(object sender, EventArgs e)
        {
            try
            {
                string contributionFile = string.Empty;
                if (flAttachment.HasFile && Path.GetExtension(flAttachment.FileName) == ".xls")
                {
                    flAttachment.SaveAs(Server.MapPath("~/FolderPath/") + flAttachment.FileName);
contributionFile = Server.MapPath("~/FolderPath/") + flAttachment.FileName;
                }
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress("anil.singh@gmail.com");

  mm.To.Add(new MailAddress("hrTeam@gmail.com"));
  mm.To.Add(new MailAddress("adminTeam@gmail.com"));
  mm.To.Add(new MailAddress("Manager@gmail.com")); 
  mm.CC.Add(new MailAddress("Boss@gmail.com"));
                mm.Subject = "This is your File" + monthName;
                mm.Body = txtMailMessage.Text;
                mm.IsBodyHtml = true;
                mm.Priority = MailPriority.High;
                Attachment attach = new Attachment(contributionFile);
                mm.Attachments.Add(attach);
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                client.EnableSsl = true;
                client.Credentials = new NetworkCredential("anil.singh@gmail.com", "password#123");
                client.Send(mm);
                client.Timeout = 120;
            }
            catch (Exception ex)
            {

            }
        }


Thanks,
Anil

No comments: