0

Ok, so I am no an administrator so i cannot just remote into the box and grab the necessary .dlls

What I want to do is use the Sharepoint mailing system to send an email.

I installed the Nuget Package MicrosoftSharepoint2010 which installed

  • Microsoft.SharePoint
  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime
  • Microsoft.SharePoint.Linq
  • Microsoft.SharePoint.Publishing
  • Microsoft.SharePoint.Security
  • Microsoft.SharePoint.Taxanomy
  • Microsoft.SharePoint.Taxanomy.Intl
  • Microsoft.SharePoint.WorkflowActions

I have used this code successfully to read and write to lists..

 Business.Utility.loadConfigurations();
 Uri uri = new Uri(SharePointConnector.sp_site_address);

 using (ClientContext clientContext = new ClientContext(uri))
 {
  //do stuff
 }

However if I use this code to send an email it tells me I need the assembly for Utilities.EmailProperties

 User sendToUser = clientContext.Web.EnsureUser("[email protected]");
 clientContext.Load(sendToUser);
 clientContext.ExecuteQuery();
 string email = Microsoft.SharePoint.Client.Utilities.Utility.GetCurrentUserEmailAddresses(clientContext).Value;
 Microsoft.SharePoint.Client.Utilities.EmailProperties properties = new Microsoft.SharePoint.Client.Utilities.EmailProperties();
 properties.To = new string[] { sendToUser.Email };
 properties.Subject = "subject";
 properties.Body = "body";

 Microsoft.SharePoint.Client.Utilities.Utility.SendEmail(clientContext, properties);
 clientContext.ExecuteQuery();

and If i instead try something like the below I instead get this error

'Could not load file or assembly 'Microsoft.SharePoint.Library, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies.

'

    using (SPSite oSPSite = new SPSite(SharePointConnector.sp_site_address))  //Site collection URL
    {
        using (SPWeb oSPWeb = oSPSite.OpenWeb("DGPA"))  //Subsite URL
        {
            StringDictionary headers = new StringDictionary();

            headers.Add("from", "[email protected]");
            headers.Add("to", "[email protected]");

            headers.Add("subject", "Welcome to the SharePoint");
            headers.Add("fAppendHtmlTag", "True"); //To enable HTML format

            System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
            strMessage.Append("Message from CEO:");

            strMessage.Append("<span style='color:red;'> Make sure you have completed the survey! </span>");
            SPUtility.SendEmail(oSPWeb, headers, strMessage.ToString());

        }
    }
3
  • sounds like you are using SSOM assemblies in the mix. check this: sharepoint.stackexchange.com/questions/164377/… Commented Sep 12, 2017 at 8:17
  • "Utilities.EmailProperties" is part of "Microsoft.SharePoint.Client" assembly which you have added already. The "Could not load file or assembly" error may be because of "Microsoft.SharePoint" dll. If you are using CSOM then there is no need of "Microsoft.SharePoint" dll. so remove it & try. Commented Sep 12, 2017 at 11:27
  • The type or namespace name 'EmailProperties' does not exist in the namespace 'Microsoft.SharePoint.Client.Utilities' (are you missing an assembly reference?) Commented Sep 12, 2017 at 11:57

1 Answer 1

0

You install by using Manage NuGet packages as shown in below image it refer all the client dlls required

enter image description here

you refer the utilities in class file using Microsoft.SharePoint.Client.Utilities;

copy the below code to send emails and test it will send emails. Make sure you give in house(with in domain) email address not out side emails address

    ClientContext ctxSite = new ClientContext("Siteurl");
    string UserID = "[email protected]";
    string Pwd = "password";

    SecureString _pwd = new SecureString();
    foreach (char c in Pwd.ToCharArray())
    {
        _pwd.AppendChar(c);
    }
     ctxSite.Credentials = new SharePointOnlineCredentials(UserID, _pwd);
    ctxSite.ExecuteQuery();
    var emailp = new EmailProperties();
    emailp.BCC = new List<string> { "[email protected]" };
    emailp.To = new List<string> { "[email protected]" };
    emailp.Body = "<b>Test Email</b>";
    emailp.Subject = "Test subject";
    Utility.SendEmail(ctxSite, emailp);
    ctxSite.ExecuteQuery();
2
  • This causes this error - Additional information: Specified argument was out of the range of valid values. Parameter name: SendEmail Commented Sep 12, 2017 at 12:38
  • are you trying for which version of SharePoint ? Commented Sep 13, 2017 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.