在windows平台使用Apache James搭建邮件服务器以及选拔C#向外网发送邮件

图片 1

出殡完邮件之后登陆163邮箱就足以观察曾经接收了正要发送的邮件,如图

 

图片 2

string senderServerIp = "xxx.xx.xx.x";  //域名star.com对应的外网IP

                string toMailAddress = "liushuaichao159@163.com";

                string fromMailAddress = "liemei@star.com";

                string subjectInfo = "Test sending e_mail";

                string bodyInfo = "Hello Eric, This is my first testing e_mail";

                string mailUsername = "liemei";

                string mailPassword = "123456"; //发送邮箱的密码()

                string mailPort = "25";

                string attachPath = "";



                MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);

                email.AddAttachments(attachPath);

                email.Send();

② 、下载Apache James ,下载解压之后的目录如图

 

图片 3

 

 

Using PHOENIX_HOME:   C:\james
Using PHOENIX_TMPDIR: C:\james\temp
Using JAVA_HOME:

Phoenix 4.0.1

james 2.3.2.1
Remote Manager Service started plain:4555
POP3 Service started plain:110
SMTP Service started plain:25
NNTP Service Disabled
Fetch POP Disabled

找到上边的配置项,去掉注释:

双击bin底下的run.bat批处理公事安装James 服务,提示如下新闻验证安装成功:

 

<authRequired>true</authRequired>

下一步正是在路由器中配置相应的外网映射,将域名star.com对应的外网IP地址映射到邮件服务器上,端口一一对应即可。

 <account login="root" password="!changeme!"/>

诠释掉该配置项

在控制苏州输入指令行telnet localhsot 4555 进入James的控制台

① 、下载安装JDK,并且安插环境变量

那样的话访问邮箱就必要账号验证才行。

 

提示输入Login ID和Password

 

使用C#发送邮件测试

········

<postmaster>Postmaster@localhost</postmaster>

···········

 <servernames autodetect="false" autodetectIP="false">

<!-- CONFIRM? -->

         <servername>localhost</servername>

      </servernames>

图片 4

·····

<postmaster>Postmaster@star.com</postmaster>

······

<servernames autodetect="false" autodetectIP="false">

<!-- CONFIRM? -->

         <servername>star.com</servername>

      </servernames>

将安顿文件中的

找到配置项:

到如今甘休基本的Apache
James配置已经到位,可是要向外网如163信箱发送邮件,还有局地题目,因为詹姆斯的SMTP
服务暗中同意在 25 端口运行,POP3 服务默许在 110 端口运行, NNTP 服务默许在
119
端口运维,所以就算想要在外网寻常使用供给配置防火墙允许这么些端口通过,配置的岗位在防火墙高级设置,添出席站规则,如图

 

Welcome root. HELP for a list of commands

 class MyEmail
    {
        private MailMessage mMailMessage;   //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
        private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
        private int mSenderPort;   //发送邮件所用的端口号(htmp协议默认为25)
        private string mSenderServerHost;    //发件箱的邮件服务器地址(IP形式或字符串形式均可)
        private string mSenderPassword;    //发件箱的密码
        private string mSenderUsername;   //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
        private bool mEnableSsl;    //是否对邮件内容进行socket层加密传输
        private bool mEnablePwdAuthentication;  //是否对发件人邮箱进行密码验证

        ///<summary>
        /// 构造函数
        ///</summary>
        ///<param name="server">发件箱的邮件服务器地址</param>
        ///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
        ///<param name="fromMail">发件人地址</param>
        ///<param name="subject">邮件标题</param>
        ///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
        ///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param>
        ///<param name="password">发件人邮箱密码</param>
        ///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
        ///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
        ///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
        public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
        {
            try
            {
                mMailMessage = new MailMessage();
                mMailMessage.To.Add(toMail);
                mMailMessage.From = new MailAddress(fromMail);
                mMailMessage.Subject = subject;
                mMailMessage.Body = emailBody;
                mMailMessage.IsBodyHtml = true;
                mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                mMailMessage.Priority = MailPriority.Normal;
                this.mSenderServerHost = server;
                this.mSenderUsername = username;
                this.mSenderPassword = password;
                this.mSenderPort = Convert.ToInt32(port);
                this.mEnableSsl = sslEnable;
                this.mEnablePwdAuthentication = pwdCheckEnable;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        ///<summary>
        /// 添加附件
        ///</summary>
        ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
        public void AddAttachments(string attachmentsPath)
        {
            if (string.IsNullOrEmpty(attachmentsPath))
                return;
            try
            {
                string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
                Attachment data;
                ContentDisposition disposition;
                for (int i = 0; i < path.Length; i++)
                {
                    data = new Attachment(path[i], MediaTypeNames.Application.Octet);
                    disposition = data.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(path[i]);
                    disposition.ModificationDate = File.GetLastWriteTime(path[i]);
                    disposition.ReadDate = File.GetLastAccessTime(path[i]);
                    mMailMessage.Attachments.Add(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        ///<summary>
        /// 邮件的发送
        ///</summary>
        public void Send()
        {
            try
            {
                if (mMailMessage != null)
                {
                    mSmtpClient = new SmtpClient();
                    //mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
                    mSmtpClient.Host = this.mSenderServerHost;
                    mSmtpClient.Port = this.mSenderPort;
                    mSmtpClient.UseDefaultCredentials = false;
                    mSmtpClient.EnableSsl = this.mEnableSsl;
                    if (this.mEnablePwdAuthentication)
                    {
                        System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                        //NTLM: Secure Password Authentication in Microsoft Outlook Express
                        mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
                    }
                    else
                    {
                        mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
                    }
                    mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    mSmtpClient.Send(mMailMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

安装之后如图所示:

<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor">   
    <processor> relay-denied </processor>   
    <notice>550 - Requested action not taken: relaying denied</notice>   
</mailet> 

root正是Login ID,可以在那里修改登录的账号密码。登录成功今后提示:

Login ID 和Password在前头修改的布署文件中设置,找到节点

 修改E:\james-2.3.2.1\apps\james\SAR-INF\config.xml文件

图片 5

账号正是liemei 密码是123456。

输入 help 就能够查阅詹姆斯的相干命令

出殡邮件类:

首先环境搭建:

输入 adduser liemei 1234陆十二遍车就足以添加三个新的用户,用户的信箱便是liemei@star.com

 

中的localhost修改为团结的域名,这里假诺修改为star.com,假使开通账号为zsf的话那么邮件地址正是zsf@star.com,并且将autodetectIP修改为false。修改结果如下:

相关文章