ASP.NET Cafe
new tricks every week

MailEnable - How to add new mailbox from code

Tuesday, 10 February 2009 03:49 by dmitriy

Mailenable is quite good Mail Server for windows. It has a free version ( and that's version works good if you don't need WebMail ).

Here is some code to add mailbox to PostOffice.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
/// <summary>
/// Summary description for MailEnableConnector
/// </summary>
public class MailEnableConnector
{
    public MailEnableConnector()
    {
    }
            /// <summary>
    /// Gets Path to config folder of MailEnable
    /// Add this to appSettings in your web.config:
    /// add key="MailEnableConfigRoot" value="C:\Program Files\Mail Enable\Config\" 
    /// </summary>
    /// <returns>Path string</returns>
    protected string GetConfigRoot()
    {
        return System.Configuration.ConfigurationManager.AppSettings.Get("MailEnableConfigRoot");
    }
 
 
    /// <summary>
    /// Registers Mailbox
    /// </summary>
    /// <param name="username">Username</param>
    /// <param name="domain">Domain</param>
    /// <param name="postoffice">Postoffice</param>
    /// <param name="FriendlyName">Friendly Name (for example Tyler Durden)</param>
    /// <param name="password">Password</param>
    /// <returns>True if ok. False if failed.</returns>
    public bool RegisterMailbox(string username, string domain, string postoffice,string FriendlyName, string password)
    {
        bool res = true;
        try
        {
            MailEnable.Administration.Mailbox mb = new MailEnable.Administration.Mailbox();
            mb.Postoffice = postoffice;
            mb.Mailbox = username;
            mb.Host = domain;
            mb.Limit = 5000;
            mb.RedirectAddress = "";
            mb.RedirectStatus = 0;
            mb.Status = 1;
            
            mb.AddMailbox();
            
            MailEnable.Administration.Login login = new MailEnable.Administration.Login();
            login.Account = postoffice;
            
            login.Description = username + " at " + domain;
            login.Host = domain;
            login.Rights = "USER";
            login.Status = 1;
            login.Password = password;
            login.UserName = username+ "@" + postoffice;
            
            login.AddLogin();
 
            MailEnable.Administration.AddressMap map = new MailEnable.Administration.AddressMap();
            map.Account = postoffice;
            map.DestinationAddress = "[SF:"+postoffice+"/"+username +"]";
            map.SourceAddress = "[SMTP:" + username + "@" + domain + "]";
            map.Scope = "";
 
 
            if (map.AddAddressMap() == 0)
            {
                throw new Exception("Failed address map");
            }
 
            // Add Friendly name //
            SetFriendlyName(username, postoffice, username + "@" + domain, FriendlyName);
            
        }
        catch (Exception e)
        {
            res = false;
        }
        return res;
    }
 
    /// <summary>
    /// Adss a Friendly name to mailbox by direct writing into config file
    /// </summary>
    /// <param name="username"></param>
    /// <param name="postoffice"></param>
    /// <param name="replyaddress"></param>
    /// <param name="frinedlyname"></param>
    protected void SetFriendlyName(string username, string postoffice,string replyaddress,string frinedlyname)
    {
        string SysFilePath = System.IO.Path.Combine(GetConfigRoot(), "postoffices\\" + postoffice + "\\mailboxes\\" + username + ".sys");
        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(SysFilePath, false))
        {
            /*      [General]
                    ReplyAddress=
                    DisplayName=
                    */
            sw.WriteLine("[General]");
            sw.WriteLine("ReplyAddress=" + replyaddress);
            sw.WriteLine("DisplayName=" + frinedlyname);
            sw.Close();
        }
    }
 
    
}
 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   ASP.NET | C# | Main
Actions: E-mail | del.icio.us | Kick it! | DZone it! | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading