How to send an e-mail with attachment from SharePoint

October 2, 2007   11:52


How to send an e-mail with attachment from SharePointOne of the ways to send an e-mail from SharePoint as developer, is to make use of the “SPUtility.SendEmail” classes. But unfortunately I did not find any possibility to include an attachment with the help of these classes.

To be able to send attachment I decided to make use of the normal “System.Net.Mail” classes to send a message. Instead of hard coding the SMTP information into the code, I wanted to use the SMTP settings that are configured with the SharePoint Central Administration. These settings are found in the “SPAdministrationWebApplication” class.

Because SharePoint stores it’s files into the content database, we must do a http request to retrieve the attachment. Using the “System.Net.WebClient” is one way to do it and one of the advantages is that you can supply it with the credentials of the user that is trying to send the mail.

You will receive an unauthorized message if you don’t supply a correct set of credentials. It’s probably not a good idea to hardcode the credentials as it will be possible for users to get access to files they are not supposed to. Supplying the WebClient with the “CredentialCache.DefaultNetworkCredentials” worked fine for me on a standard SharePoint environment.

Below is a brief example on how you can send the mail with attachment from code. If you prefer to use the user’s e-mail address instead of the configured SMTP user you can retrieve it with SPControl.GetContextWeb(Context).CurrentUser.Email.

(more…)

Tags: , , , , , , .





Two custom development examples build for SharePoint 2007

September 30, 2007   19:26


SharePoint provides a lot of functionality out of the box, but of course there are always parts that have to be custom build. In this post I will show you 2 cool examples of what is possible with SharePoint when you add some custom development. Last week I’ve developed these two custom parts at Macaw to extend the functionality of the document library.

File preview
The standard document library allows the user to manage and download documents. One of the things missing in SharePoint is the option to show a preview of the document. To add this functionality we have decided to add a Web Part that streams the file inside an IFrame. The web part is then connected to the normal document list. I have created a small Flash movie that shows the final functionality.

Click to view the preview window demonstration.

Send a file attachment by e-mail
One of the out of the box functionalities that SharePoint provides is the possibility to e-mail a direct link to a document that is hosted inside SharePoint. The receiver of the e-mail can click the link inside the message to connect to SharePoint and view the file. This works fine for internal SharePoint users, but sometimes you must also mail a file to an external user.

It requires custom development to send an e-mail with the file attached. To make SharePoint email the file we have used a SharePoint feature to add a new menu option to the item menu that presents the user with an e-mail page. After filling in the form SharePoints sends out the message with the selected file as attachment. Of course you can see a demo of it in another Flash movie.

Click to see the attachment demonstration.

[UPDATE 12-11-2007]
Changed the flash html files. Because of an error in the script the flash movies where not visible in Firefox.

Tags: , , , , , , .









The content expressed in this blog are those of Edwin Vriethoff and do not represent his employer's view in anyway. The contents of this blog has been carefully put together, but Edwin Vriethoff is not responsible in any way for any direct or indirect harm caused by individuals or organizations using the content of this blog in any way.