0

We have a list on our SharePoint 2010 server that we'd like to host some documents. We're able to log in to SharePoint externally with VPN, but the users for whom these documents are intended are not able to log on with VPN.

Presently, an Email notification is sent out upon a new file upload, but we don't necessarily want these links made public. What we want to do instead is attach the uploaded file to the notification that goes out, instead of including a link to the file, when it's uploaded to that list.

A search on this topic has yielded no useful results. Can this be done? If so, how?

2

This is not possible OOB you will need to deploy some custom code, for example you could create a document library event receiver that fires on item created/updated which will do what you require.

The example below shows how to do this, it is functional but will need additional work to make it suitable for deployment in a live environment.

using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace EmailAttachment.EmailAttachment
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EmailAttachment : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            SendEmailWithAttachment(properties);
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            SendEmailWithAttachment(properties);

        }

        private static void SendEmailWithAttachment(SPItemEventProperties properties)
        {
            // No need to dispose this SPWeb as we get it from the event receiver properties
            SPWeb web = properties.Web; 

            // Check there is an email server configured
            if (SPUtility.IsEmailServerSet(web)) 
            {
                // Get the web app so we can get the email server SP is configured to use
                SPWebApplication webApp = web.Site.WebApplication; 

                // Get the mail server details
                string smtpServerAddress = webApp.OutboundMailServiceInstance.Server.Address;
                string fromAddress = webApp.OutboundMailSenderAddress;

                var email = new MailMessage();
                email.From = new MailAddress(fromAddress);

                SPList list = properties.List;

                // Get all alerts in the web then filter this to the alerts on the list we are dealing with
                SPAlertCollection allAlerts = web.Alerts;
                foreach (SPAlert alert in allAlerts)
                {
                    if (alert.ListID == list.ID)
                    {
                        // Add each user with an alert to the email recipients list 
                        email.To.Add(alert.User.Email);
                    }
                }

                // Grab the list item that has triggered the event and add the associated document as an email attachement
                SPFile file = properties.ListItem.File;
                Stream contentStream = file.OpenBinaryStream();
                var attachment = new Attachment(contentStream, file.Name);
                email.Attachments.Add(attachment);

                email.Subject = "Your email subject";
                email.Body = "Your email body text";

                // Set up the mail server and sent the email
                SmtpClient mailServer = new SmtpClient(smtpServerAddress);
                mailServer.Credentials = CredentialCache.DefaultNetworkCredentials;
                mailServer.Send(email);
            }
        }    
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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