首先環境搭建: 1、下載安裝JDK,並且配置環境變數 2、下載Apache James ,下載解壓之後的目錄如圖 雙擊bin下邊的run.bat批處理文件安裝James 服務,提示如下信息說明安裝成功: 安裝之後如圖所示: 修改E:\james-2.3.2.1\apps\james\SAR-INF\ ...
首先環境搭建:
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的話那麼郵件地址就是[email protected],並且將autodetectIP修改為false。修改結果如下:
····· <postmaster>[email protected]</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 回車就可以添加一個新的用戶,用戶的郵箱就是[email protected],
賬號就是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 = "[email protected]";
string fromMailAddress = "[email protected]";
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; //發件箱的用戶名(即@符號前面的字元串,例如:[email protected],用戶名為: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">發件箱的用戶名(即@符號前面的字元串,例如:[email protected],用戶名為: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郵箱就可以看到已經收到了剛剛發送的郵件,如圖