Tuesday, June 26, 2012

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;

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

Hope this is helpful.

Visual Studio Build Error - The "GenerateResource" task failed unexpectedly

Error: "The GenerateResource task failed unexpectedly" This error is quite common while building the project in Visual Studio....