pinalbhatt
9/20/2014 - 1:55 PM

SendEmail

SendEmail

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";

System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);

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

SmtpServer.Send(mail);
using System.Net.Mail;

public static bool Send(MailMessage email)
        {
            var result = false;
            
            if (email != null && email.To.Count > 0)
            {
                using (SmtpClient smtp = new SmtpClient())
                {
                    try
                    {
                        email.From = new MailAddress("<from email address>");
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new System.Net.NetworkCredential("<from email id>", "<password>");
                        smtp.EnableSsl = true;
                        smtp.Send(email);
                        result = true;
                    }
                    catch(Exception ex)
                    {
                        //throw new Exception("Error in EmailMgr.Send(MailMessage). " +  ex.Message, ex);
                        result = false;
                    }
                }
            }
            return result;
        }
//using System.Net;
//using System.Net.Mail;

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("<smtp host address>");
 
                mail.From = new MailAddress("me@mydomain.com");
                mail.To.Add("u@urdomain.com");
                mail.Subject = "Subject";
                mail.Body = "body";

                SmtpServer.Port = 25; // Or whatever port your smpt server is supporting
                SmtpServer.Credentials = new System.Net.NetworkCredential("me", "password");
                SmtpServer.EnableSsl = true;
 
                SmtpServer.Send(mail);