Skip to main content

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

Visual Studio Build Issue : Build: 0 succeeded or up-to-date, 0 failed, 1 skipped

"Unable to build or clean the solution in Visual Studio (applicable to almost all versions), output window message is : Build: 0 succeeded or up-to-date, 0 failed, 1 skipped". If you are facing the above issue, you are at the right place. Perhaps following can help you. BTW, the above image is self explanatory, let me still brief if in case the image is not getting downloaded on your Internet connection. In VS menu, go to Tools -> Options. Select -> Projects and Solutions -> Build and Run. Uncheck - "Only build startup projects and dependencies on Run." Your problem shall be resolved. Thank you for visitng my blog.

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

There are many reasons for which you can have this error in your SharePoint server logs. One of the scenerio is explained below. Main Reason: One of the major reason for this error is; the SharePoint is not able to resolve the URL. There is something wrong with the alternate access mapping. Please make sure the alternate access mapping is configured properly. For more details on alternate access mapping in SharePoint, please visit: Alternate Access Mapping We had sharepoint 2016 and all website under that were https:// from the firewall. When we tried to load the web site, error occured with a Correlation Id. After checking the code I found the error message "Cannot find site lookup info for request Uri http://". I URL was able to authenticate the user but on page load the error occurs. My alternate access mapping was: https://mysite                                   ...

SharePoint PowerShell to Manage Term Store / Add Terms using PowerShell

SharePoint PowerShell to Manage Term Store / Add Terms //get the term store $mysite = get-spsite “ http://yoursite:portnumber ” $taxonomySession = get-taxonomySession -site $mysite $termStore = $taxonomySession.TermStores[“Managed Metadata Service”] //now we will create the Term Store groups. Before we create any group we will check if the group already exists Example 1: if($termstore.groups["News Keys"] -eq $null) { $termstoregroup = $termstore.creategroup("News Keys");  $termstore.description = "News Group";  $termstore.addgroupmanager ("KS\spadmin"); $termstore.addcontributor ("ks\k.singh");$termStore.CommitAll();} This is not working as “ description ” is not an identified property. Example 2: if($termstore.groups["Main News Keys"] -eq $null) { echo "group is not there";  echo "group is there"; $termstoregroup = $termstore.creategroup("Main News Keys"); $terms...