日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/7/14 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在windows平台使用Apache James搭建邮件服务器以及使用C#向外网发送邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

首先環境搭建:

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.1james 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對應的外網IPstring 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 ExpressmSmtpClient.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郵箱就可以看到已經收到了剛剛發送的郵件,如圖

?

轉載于:https://www.cnblogs.com/liemei/p/8119496.html

總結

以上是生活随笔為你收集整理的在windows平台使用Apache James搭建邮件服务器以及使用C#向外网发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。