Send e-mail from SharePoint Object Model (SMTP)

Send e-mail from SharePoint Object Model (SMTP)

From Visual Studio add reference to System.Net.Mail....


Use following code to send e-mail.
                
               MailMessage mMailMessage = new MailMessage();

                mMailMessage.From = new MailAddress(fromeMailAddress,"Mail From Title");

                mMailMessage.To.Add(new MailAddress(to));

                if ((bcc != null) && (bcc != string.Empty))
                {
                    mMailMessage.Bcc.Add(new MailAddress(bcc));
                }
                if ((replyTo != null) && (replyTo != string.Empty))
                {
                    mMailMessage.ReplyTo = new MailAddress(replyTo);
                }
                if ((cc != null) && (cc != string.Empty))
                {
                    mMailMessage.CC.Add(new MailAddress(cc));
                }

                mMailMessage.Subject = subject;

                mMailMessage.Body = body;

                mMailMessage.IsBodyHtml = true;

                mMailMessage.Priority = MailPriority.Normal;

                SmtpClient mSmtpClient = new SmtpClient();
                System.Net.ServicePointManager.ServerCertificateValidationCallback =    delegate(object s, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

               mSmtpClient.Credentials = new System.Net.NetworkCredential(fromeMailAddress, password);

                mSmtpClient.Host = ConfigurationManager.AppSettings["MailHostName"];

                string strPort = ConfigurationManager.AppSettings["YourMailHostPort"];

                mSmtpClient.Port = Convert.ToInt32(strPort);
                mSmtpClient.EnableSsl = true;
                mSmtpClient.Send(mMailMessage);

The above code is in general targeted for gmail SMTP, in some cases you may not require couple of configuration.

Hope this is helpful.

Comments

Popular posts from this blog

SharePoint log error "Cannot find site lookup info for request Uri http://"

Session Id changes in ASP.NET MVC on every new page request