I have the following console application, which connect to our SharePoint online site collection and send emails:-
using (ClientContext context = new ClientContext(scurl))
{
SecureString passWord = new SecureString();
foreach (var cc in s)
passWord.AppendChar(cc);
context.Credentials = new SharePointOnlineCredentials("admin@****.onmicrosoft.com", passWord);
List oList = context.Web.Lists.GetByTitle(listname);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='Contract_x0020_Review_x0020_Date'/>" +
"<Value Type='DateTime'><Today /></Value></Eq><Eq><FieldRef Name='Contract_x0020_Status'/><Value Type='String'>Active</Value></Eq></AND></Where></Query><RowLimit>5000</RowLimit></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
context.Load(collListItem,
items => items.Include(
item => item["Contract_x0020_Status"],
item => item["Person_x0020_Responsible"],
item => item["Title"],
item => item["Id"]
));
context.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
oListItem["Contract_x0020_Status"] = "Needs Review";
oListItem.Update();
context.ExecuteQuery();
var userValue = (FieldUserValue)oListItem["Person_x0020_Responsible"];
var user = context.Web.GetUserById(userValue.LookupId);
context.Load(user, x => x.Email);
context.Load(user, x => x.Title);
context.ExecuteQuery();
//item.RoleAssignments.RemoveById(spgroup.ID);
MailMessage mail = new MailMessage("no-reply@sharepointonline.com", user.Email);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Host = "smtp.office365.com";
mail.Subject = "The Contract has revowed";
//mail.To = user.Email[0];
mail.Bcc.Add(ConfigurationManager.AppSettings["bccemail"].ToString());
mail.Body = "Hi <span><b>" + user.Title + "</b></span><br/>The Review Date for the following Contract has been reached <a href='https://***.sharepoint.com/sites/businessfunctions/operations/Lists/Supplier%20Contracts/DispForm.aspx?ID=" + oListItem.Id.ToString() + "'>" + oListItem["Title"]+ "</a><br><span style='color=Blue'>TDM Group Operations</span>";
client.Send(mail);
}
// Now update the list.
}
but the client.Send(mail);
will raise this exception:-
System.Net.Mail.SmtpException was unhandled
HResult=-2146233088
Message=Failure sending mail.
Source=System
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at ConsoleApplication1.Program.Main(String[] args) in c:\Users\***Program.cs:line 94
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Net.WebException
HResult=-2146233079
Message=Unable to connect to the remote server
Source=System
StackTrace:
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
Message=No connection could be made because the target machine actively refused it 52.97.133.226:587
Source=System
ErrorCode=10061
NativeErrorCode=10061
StackTrace:
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
InnerException:
so can anyone advice on this?