Send Mail With Attachment and Without Attachment

Send Mail With Attachment

protected void btnSubmit_Click(object sender, EventArgs e)

{

    try

   {

       MailMessage mail = new MailMessage();

       SmtpClient _smtpClient = new SmtpClient("smtpServer IP or Name", PortNumber);

       mail.From = new MailAddress(txtFrom.Text.Trim());

       mail.To.Add("First Recipient Address");

       mail.To.Add("Second Recipient Address ");

       mail.Subject = txtSubject.Text.Trim();

       mail.Body = txtBody.Text.Trim();

       System.Net.Mail.Attachment attachment;

       attachment = new System.Net.Mail.Attachment("D://Test.txt");

       mail.Attachments.Add(attachment);

       _smtpClient.Credentials = new System.Net.NetworkCredential("Email ID", "password");

       _smtpClient.EnableSsl = true;

      _smtpClient.Send(mail);

      ClientScript.RegisterStartupScript(typeof(Page), "Success", "<script type=\"text/javascript\"> alert('Mail Sent Successfully !!!');</script>");

   }

   catch (Exception ex)

   {

         ClientScript.RegisterStartupScript(typeof(Page), "Exception", "<script type=\"text/javascript\"> alert('Exception: " + ex.Message.Trim() + "');</script>");

    }

} 

Send Mail Without Attachment

SmtpClient _smtpClient = new SmtpClient("smtpServer IP or Name", PortNumber);

_smtpClient.Credentials = new System.Net.NetworkCredential("Email ID", "password");

string from, to, sub, body;

from = txtFrom.Text.Trim();

to = txtTo.Text.Trim();

sub = txtSubject.Text.Trim();

body = txtBody.Text.Trim();

_smtpClient.Send(from, to, sub, body);