Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘email’

Configuring Email for Development Server

Posted by JP on October 14, 2012

In this article we can explore how to configure Email for Development Server. In real world development scenarios you need to work with Email enabled lists / workflows / code.

Email

Scenario

Your customer reported a problem with Email sending from a web part you have developed. You need to ensure the piece of code works fine in your development machine. As there is no email server configured in your machine – How to test this code?

Solution

We can set up a Development Server with emailing enabled. Setting up Email for the Development Server along with a Receiver tool you can ensure that emailing code is working perfect.

Steps

Following are the steps involved:

1. Configure outgoing SMTP server

2. Set Email property for User Profiles

3. Install smtp4dev tool

4. Test Email Code

Please note that I am using a Windows 7 64-bit machine for working with this. The same configurations should work for Windows Server development machine.


Configure outgoing SMTP server

Open Central Administration and click on the System Settings category from the left.

image

In the appearing page click on the Configure outgoing e-mail settings link highlighted above. You should get the page given below.

image

Enter the Outbound SMTP server as your machine name. Please enter a name instead of IP Address or localhost.

Enter the From and Reply-To address.

Click the OK button to save the changes.


Set Email property for User Profiles

As the next step we need to set the user profile property E-mail for testing the feature. You can set this through:

1. My Profile of each user

2. Central Administration for all users

Let us use the Central Administration way as we can set for multiple users. Open Central Administration > Manage service applications > Select User Profile Service Application > Click Manage button from toolbar.
image

In the appearing page click on the Manage User Profiles link as highlighted below:

image

In the appearing page search for user and from the result choose Edit menu item.
image

In the Edit profile page set the Work-email property and save changes.

image

Install smtp4dev tool

Now we can try installing the smtp4dev tool (a wonderful tool) that captures the Port 25 of your machine. You can download the tool from following location:

http://smtp4dev.codeplex.com/

Click the Download button on the appearing page.

image

Run the downloaded file and you should see the following screen.

image

By default the tool started to listen on Port 25. You can minimize the tool and it is available in the system tray.

Note Running another instance of the smtp4dev tool cannot listen to the same port 25. You need to invoke the previous copy of the tool from system tray to view any email messages.

Test Email Code

Now we are ready to test the email code. Start a new SharePoint Console application, change the project properties to .Net Framework 3.5 and replace the Program class content with following code.

using System.Collections.Specialized;

class Program

{

static void Main(string[] args)

{

using (SPSite site = new SPSite("http://localhost"))

{

StringDictionary messageHeaders = new StringDictionary();

messageHeaders.Add("to", "to@server.com");

messageHeaders.Add("cc", "cc@server.com");

messageHeaders.Add("from", "from@server.com");

messageHeaders.Add("subject", "Email Subject");

messageHeaders.Add("content-type", "text/html");

SPUtility.SendEmail(site.OpenWeb(), messageHeaders, "Email Body");

}

}

}

For the time being we are using dummy email address. Try executing the code. You can see the email being captured by smtp4dev tool.

image

References

http://tinyurl.com/sp2010-confemail

Summary

In this article we have explored how to configure email server for a development machine and use smtp4dev tool to capture the emails generated from the machine. You can use the same configuration to test other emails through Workflows, Web Parts etc. that are generated through SharePoint.

Please note that here we are setting up a development machine email server and for configuring the actual email service you need to check the link from References section.

Advertisements

Posted in SharePoint | Tagged: , , , , , | 6 Comments »

Code to extract Email addresses from text

Posted by JP on February 27, 2012

public List<string> GetEmailsFromText(string text)
{
            Regex regex = new Regex(@”[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}”, RegexOptions.IgnoreCase);
            Match match = null;

            List<string> results = new List<string>();
            for (match = regex.Match(text); match.Success; match = match.NextMatch())
                if (!(results.Contains(match.Value)))
                    results.Add(match.Value);

            return results;
}

// Try using GetEmailsFromText(“email@yahoo.com, email2@gmail.com, email3@hotmail.com”);

Posted in C# | Tagged: , , | Leave a Comment »