I am trying to come up with a working logic that would be used in a receiver that would be triggered off to send of emails (along with document attached) when documents are added to a specific directory. So far, I have come up with this code...
public override void ItemAdded(SPItemEventProperties properties) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite currentSite = new SPSite(properties.Web.Url)) { using (SPWeb currentWeb = currentSite.OpenWeb(properties.Web.Url)) { SPFolder mylibrary = properties.Web.Folders["My Docs"]; properties.Web.AllowUnsafeUpdates = true; SmtpClient smtpClient = new SmtpClient("smtp.mail.yahoo.com"); smtpClient.Send(BuildMailMessage(properties.ListItem.File)); } } }); } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Category", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace); } finally { } } private MailMessage BuildMailMessage(SPFile file) { MailMessage message = new MailMessage(); message.From = new MailAddress("myemail@yahoo.com"); message.To.Add(new MailAddress("myemail@mycompany.com")); message.IsBodyHtml = true; message.Body = "Please review the following attachment for details"; message.Subject = "Word document received"; message.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name)); return message; }
But each time I run this code, I get errors. The first error it returns is "Failure sending mail." and when I view the error details, I also notice this Inner Exception error message "The remote name could not be resolved: 'smtp.mail.yahoo.com'".
Am I missing something? Does anyone know why this is happening and how I can get past this?
Thanks for the help
Here's the stack trace for the inner exception:
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
And the one for the outer exception:
at System.Net.Mail.SmtpClient.Send(MailMessage message)at SendEmailSampleEventReceiver.EventReceiver1.EventReceiver1.<>c__DisplayClass2.<ItemAdded>b__0()
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)