SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming...

13
www.itcube.net [email protected] 1 SharePoint 2013 Incoming Emails and Email File Processing

Transcript of SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming...

Page 1: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

1

SharePoint 2013 Incoming Emails and Email File Processing

Page 2: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

2

Business Problem:

Differe t depart e ts fro i usto er’s e iro e t ere se di g da to da e ails to all the

users of the organization; the department includes Health, Communication etc.

Customer is having there intranet portal hosted on SharePoint, they wanted this day to day

emails which were targeted to end users and save and show them on Intranet portal.

So that new employees as well as current users can see this messages at any point of time

and then can browse through historical emails as well.

SharePoint 2013 Incoming emails and email file processing consist of two measure steps

1. Getting emails in SharePoint 2013

2. Processing emails using SharePoint 2013 visual web part

Getting emails in SharePoint 2013

Let’s see ho to get e ails i “harePoi t ; this pro ess i ol es follo i g ajor steps

1. Install IIS 6.0 Role And SMTP Server Feature

2. Set up SMPT server

3. Configure Central Admin for Incoming mails

4. Configure document library to receive emails

1. Install IIS 6.0 Roles and SMTP Server Feature

Installing IIS 6.0 involves following steps:-

Go to Server Manager -> Select Add Roles and Feature -> Inside Server Roles section ->

“ele t II“ 6 Ma age e t Co pati ilit

o Screenshot for reference

Page 3: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

3

Figure 1:- Install IIS Role

1. O e t s ree u der Features se tio sele t feature “MTP “er er

Screenshot for reference

Figure 2:- Install SMTP Feature

Page 4: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

4

2. Setup SMTP Server

O e ou’re do e ith II“ 6. a d “MTP ser er i stallatio , follo elo steps to set up SMTP server

Ope I ter et I for atio “ ste II“ 6. Ma ager fro i do s start e u. Select your server from IIS 6.0 Manager and right click to setup the properties.

Figure 3:- SMTP Server Properties

I properties i do go to A ess Ta ou ill fi d three utto s Authe ti atio , Co e tio a d Rela , do follo i g settings for this three buttons

a. Authentication: - Select Anonymous access.

Page 5: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

5

Figure 4:- SMTP Anonymous Access

b. Connection: - sele t the radio utto All e ept the list elo .

Figure 5:- SMTP Server Connections

c. Relay: - “ele t O l the list elo a d he k the o asso iated ith Allo all o puters hi h su essfull authe ti ate to rela , regardless of the list a o e .

Page 6: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

6

Figure 6:- SMTP Server Relay

3. Configure Incoming Emails Settings in SharePoint 2013

1. Go to Central Admin -> System Settings -> Configure Incoming E-mail settings section

Figure 7:- CA Incoming E-mail settings

2. On the Incoming E-Mail settings configuration page, make all settings as per below screen.

Page 7: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

7

Figure 8:- Incoming settings in detail

4. Configure SharePoint document library to Receive Emails

1. Go to your SharePoint Site -> Any Document Library -> Library Settings.

2. I side li rar setti gs page, u der Co u i atio se tio li k I o i g E- ail setti gs link.

3. Do settings as per below screenshot

Page 8: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

8

Figure 9:- Email id for SharePoint repository

4. Now you can send an email to above configured email address.

Page 10: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

10

Processing emails using SharePoint 2013 visual web part Using above steps you will be able to receive the emails in SharePoint 2013, the next task is to read

these emails and display them to end users in proper (special) format. You can show this email

messages using Document library web part also, I will explain a different method to handle/read this

email files.

Following steps are involved in processing emails in SharePoint

1. Get Email document Library

2. Get Email File

3. Convert Email file to Email message

4. Upload embedded Images

5. Update Image Source in Email

6. Display Email Content

Below are the code samples to perform above steps

1. Get Email Document Library SPDocumentLibrary objEmailDocLib =

(SPDocumentLibrary)SPContext.Current.Web.Site.RootWeb.Lists["listName"];

Above server object modal code will give you access to SharePoint document library

named listName .

1. Get specific email file

There are multiple ways to get file from document library, one of them is below

SPFile objFile = objWeb.GetFile(objEmailDocLib.GetItemById(itemId).UniqueId);

In above code objWeb is SPWeb object and itemId is the Unique identifier associated with

the file.

2. Convert Email file to Email message

Once we get the SPFile from SharePoint, we need to convert it to Email message for further

processing

Stream emailStream = objEmailFile.OpenBinaryStream();

SPEmailMessage mail = new SPEmailMessage(emailStream, "stream");

Now we got the required mail message for further processing.

Page 11: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

11

3. Upload embedded images

No e ha e ail essage as a o je t e a a ess different properties of mail such as

mail.Headers["subject"]; -> This will give us email subject line.

mail.HtmlBody; this will give us email body as a html

mail.Attachments; this will give us email attachments

The most important thing to find embedded images in email, we need to find a

content type image as ("content-type: image").

Below is the code to find embedded images and upload them in SharePoint

private void ExtratAllImages(SPFile objEmailFile, string listName, bool isImagesUploaded) { string[] AllEmbededImages = new string[] { };

StreamReader reader = null; try { reader = new StreamReader(objEmailFile.OpenBinaryStream());// Get email file stream string line; while ((line = reader.ReadLine()) != null) { //Read email line by line and search for the pattern content-type: image , this pattern indicates there is embedded image in an email if (line.ToLower().StartsWith("content-type: image")) { //Upload this embedded image to SharePoint so that we can change the embedded image references to live images in SahrePoint

UploadeImages(reader, GetImage(reader, line),

listName, objEmailFile.Name, isImagesUploaded); }

} } catch (Exception ex) { } finally { if (reader != null) reader.Close(); } } //Function to extract file name from the line

private static string GetFilename(TextReader reader, string line) { string filename; int filenameStart = line.IndexOf('"') + 1;

if (filenameStart > 0)

{ filename = line.Substring(filenameStart, line.Length - filenameStart - 1); } else // filename does not have quote { filenameStart = line.IndexOf('=') + 1; filename = line.Substring(filenameStart, line.Length - filenameStart); }

Page 12: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

12

AdvanceToEmptyLine(reader); return filename; }

//Function to get actual image content private static string GetImage(TextReader reader, string line) {

if (!line.Contains("name")) { line = reader.ReadLine(); // Thunderbird: filename start atsecond line } if (!line.Contains("name")) // embedded image does not have name { AdvanceToEmptyLine(reader); return "image" + imageCount++ + ".jpg"; // default to jpeg

} return GetFilename(reader, line); } private static void AdvanceToEmptyLine(TextReader reader) { string line;

while ((line = reader.ReadLine()) != null) { if (String.IsNullOrEmpty(line)) break; } }

4. Update image source in email content

In this step we will replace all embedded image source with live one, i.e. from our SharePoint

site. To do this we will use StringBuilder and find out the image tag pattern and replace same

//Function to replace the embedded image locations in email body

private StringBuilder UpdateImageURLs(StringBuilder sbTemp, string imagePath)

{

//Sample replace format

//cid:[email protected]

int startIndex = 0;

int endindex = 0;

string strTemp = string.Empty;

foreach (string imgName in allImages)

{

string strFormatedImgName = "src=\"cid:" + imgName;

startIndex = sbTemp.ToString().IndexOf(strFormatedImgName);

if (startIndex != -1)

{

strTemp = imagePath + imgName;

startIndex = startIndex + 5;

endindex = imgName.Length + 22;

sbTemp = sbTemp.Remove(startIndex, endindex);

sbTemp.Replace("src=\"\"", "src=\"" + strTemp + "\"");

}

}

}

Page 13: SharePoint 2013 Incoming Emails and Email File … Papers/SharePoint 2013...SharePoint 2013 Incoming emails and email file processing consist of two measure st eps ... Install IIS

www.itcube.net

[email protected]

13

5. Display email content

This is the last step where we will bind the email content i.e. StringBuilder content to label. e.g.

lblMailContent.Text = sbTemp.ToString();

10: Sample Email Viewer webpart

11: Email message in detail view

Subject Attachment

Body with

embedded images