c# send email with attachment code example

Example 1: c# send email

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("[email protected]");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

Example 2: c# send email with attachment

using System.Net;
using System.Net.Mail;

...
  
using (SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"))
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress("[email protected]");
                    mail.To.Add("[email protected]");
                    mail.Subject = "Test Mail - 1";
                    mail.Body = "Mail with attachment";
                    mail.IsBodyHtml = false;

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment("C:/Users/picture.jpg");
                    mail.Attachments.Add(attachment);

                    SmtpServer.Port = 587;
                    SmtpServer.UseDefaultCredentials = false;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "emailpassword1234");
                    SmtpServer.EnableSsl = true;

                    SmtpServer.Send(mail);
                }
            }

Example 3: c# send email

using System.Net;
using System.Net.Mail;
using System.Net.Mime;

...
try
{

   SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");

    // set smtp-client with basicAuthentication
    mySmtpClient.UseDefaultCredentials = false;
   System.Net.NetworkCredential basicAuthenticationInfo = new
      System.Net.NetworkCredential("username", "password");
   mySmtpClient.Credentials = basicAuthenticationInfo;

   // add from,to mailaddresses
   MailAddress from = new MailAddress("[email protected]", "TestFromName");
   MailAddress to = new MailAddress("[email protected]", "TestToName");
   MailMessage myMail = new System.Net.Mail.MailMessage(from, to);

   // add ReplyTo
   MailAddress replyTo = new MailAddress("[email protected]");
   myMail.ReplyToList.Add(replyTo);

   // set subject and encoding
   myMail.Subject = "Test message";
   myMail.SubjectEncoding = System.Text.Encoding.UTF8;

   // set body-message and encoding
   myMail.Body = "Test Mail
using HTML."; myMail.BodyEncoding = System.Text.Encoding.UTF8; // text or html myMail.IsBodyHtml = true; mySmtpClient.Send(myMail); } catch (SmtpException ex) { throw new ApplicationException ("SmtpException has occured: " + ex.Message); } catch (Exception ex) { throw ex; }

Tags:

Misc Example