MailEnable – How to add new mailbox from code


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 C# code to add mailbox to PostOffice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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();
        }
    }
 
 
}
Tags: ,