首先环境搭建:
1、下载安装JDK,并且配置环境变量
2、下载Apache James ,下载解压之后的目录如图
双击bin下边的run.bat批处理文件安装James 服务,提示如下信息说明安装成功:
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
安装之后如图所示:
修改E:\james-2.3.2.1\apps\james\SAR-INF\config.xml文件
将配置文件中的
········ <postmaster>Postmaster@localhost</postmaster> ··········· <servernames autodetect="false" autodetectIP="false"> <!-- CONFIRM? --> <servername>localhost</servername> </servernames>
中的localhost修改为自己的域名,这里假设修改为star.com,如果开通账号为zsf的话那么邮件地址就是zsf@star.com,并且将autodetectIP修改为false。修改结果如下:
····· <postmaster>Postmaster@star.com</postmaster> ······ <servernames autodetect="false" autodetectIP="false"> <!-- CONFIRM? --> <servername>star.com</servername> </servernames>
找到配置项:
<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet>
注释掉该配置项
找到下面的配置项,去掉注释:
<authRequired>true</authRequired>
这样的话访问邮箱就需要账号验证才行。
在控制台中输入命令行telnet localhsot 4555 进入James的控制台
提示输入Login ID和Password
Login ID 和Password在之前修改的配置文件中设置,找到节点
<account login="root" password="!changeme!"/>
root就是Login ID,可以在此处修改登录的账号密码。登录成功之后提示:
Welcome root. HELP for a list of commands
输入 help 就可以查看James的相关命令
输入 adduser liemei 123456 回车就可以添加一个新的用户,用户的邮箱就是liemei@star.com,
账号就是liemei 密码是123456。
到目前为止基本的Apache James配置已经完成,但是要向外网如163邮箱发送邮件,还有一些问题,因为James的SMTP 服务默认在 25 端口启动,POP3 服务默认在 110 端口启动, NNTP 服务默认在 119 端口启动,所以如果想要在外网正常使用需要配置防火墙允许这些端口通过,配置的位置在防火墙高级设置,添加入站规则,如图
下一步就是在路由器中配置对应的外网映射,将域名star.com对应的外网IP地址映射到邮件服务器上,端口一一对应即可。
使用C#发送邮件测试
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();
发送邮件类:
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()); } } }
发送完邮件之后登陆163邮箱就可以看到已经收到了刚刚发送的邮件,如图