Custom E-mail Provider

  Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic! Mail us feedback on this topic!  

The custom e-mail provider allows you to use third-party e-mail components for sending e-mails or add custom actions when an e-mail is sent (e.g. logging the sent e-mails to some file for auditing purposes). All e-mails sent by Kentico CMS and its modules will use your custom e-mail provider.

 

Example

 

In this example, we will create a custom e-mail provider that will log every e-mail into a file.

 

1. Copy the CustomEmailProvider project from Kentico CMS installation (typically C:\Program Files\KenticoCMS\<version>\CodeSamples\CustomEmailProvider) to some development folder (not under the web project).
 

2. Open the CMS web project using the WebProject.sln file. Click File -> Add -> Existing Project and select the CustomEmailProvider.csproj file in the folder where you copied the CustomEmailProvider project. Your Solution Explorer window will look like this:

 

devguide_clip1019

 

3. Unfold the References section of the CustomEmailProvider project and delete invalid references.

 

4. Right-click the CustomEmailProvider project and choose Add Reference. Choose the Browse tab and locate the bin folder of your CMS web project on the disk. Choose to add a reference to the following libraries:

 

CMS.EmailProvider.dll
CMS.IEmailEngine.dll
CMS.SettingsProvider.dll  
 

5. Unfold the bin folder in the CMS web project. Right-click the bin folder and choose Add Reference. Choose the Projects tab and click OK to add the CustomEmailProvider project reference:
 
devguide_clip1020

 

6. Now you can modify the CustomEmailProvider library and place your code to the SendEmail method. Enter the following code into the EmailProvider.SendEmail method:
 
[C#]
 

// send the e-mail using the standard e-mail provider

CMS.EmailProvider.EmailProvider standardEmailProvider = new CMS.EmailProvider.EmailProvider();

standardEmailProvider.SendEmail(siteName, message);

 

// log e-mail in the log file

System.IO.StreamWriter sw;

sw = System.IO.File.AppendText("C:\\_test\\EmailLogFile.txt"); // use a custom valid path

sw.WriteLine(DateTime.Now.ToString() + ": " + message.Subject);

sw.Close();

 

7. Set the following value in your web.config file:
 

<add key="CMSEmailProviderAssembly" value="CMS.CustomEmailProvider" />

 

8. Click Build -> Rebuild solution. Go to the site and subscribe to a newsletter or use some other e-mail-related feature. When the e-mail is sent, it's logged in the file EmailLogFile.txt located in the path you specified in your code.

 

Page url: http://devnet.kentico.com/docs/devguide/index.html?custom_e_mail_provider.htm