Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Event receiver in VS2010 to send an email with attachment

$
0
0

Hi,

I am trying to send an email with an attachment once a new document is added to a document library in sharepoint 2010. I created an event receiver in VS2010 and have following code so far but I am getting an error (shown with the comments below) and code won't compile.  I am trying to use SPListItem the BuildMailMessage method and passing a document library name, not sure if that is the issue. 
If some one has an idea then please let me know.  Thanks

public override void ItemAdded(SPItemEventProperties properties)
       {
         
           SPFolder mylibrary = properties.Web.Folders["My Document Library"];
           properties.Web.AllowUnsafeUpdates = true;
           SmtpClient smtpClient = new SmtpClient("mysmtpserver.com");
           smtpClient.Send(BuildMailMessage(mylibrary)); //ERROR here Method contains invalid arguments
       }


 private MailMessage BuildMailMessage(SPListItem item)
       {
           MailMessage message = new MailMessage();
           SPList list = item.ParentList;
           message.From = new MailAddress("admin@company.com");
           message.To.Add(new MailAddress("user@company.com"));
           message.IsBodyHtml = true;
           message.Body = "Please review the following attachment for details";
           message.Subject = "Word document received";

           for (int attachmentIndex = 0; attachmentIndex < item.Attachments.Count; attachmentIndex++)
           {
               string url = item.Attachments.UrlPrefix + item.Attachments[attachmentIndex];
               SPFile file = list.ParentWeb.GetFile(url);
               message.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
           }

           return message;
       }


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>