c# send email with attachments code example

Example: 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);
                }
            }