windows 8上使用SmtpClient.Send()發送郵件失敗的原因分析。


更新內容。

最終的問題還是SMTP服務器不正確引起的,而非telnet。

另外,不在同一個domain的兩個機器相互溝通需要使用全名,可以通過ping -4 <hostname>獲得。

-----------------------------------------------------------------------

最近在寫一個應用程序,但是stuck在發送郵件的地方。

提示錯誤信息是unable to connect remove server。

一開始以為是smtp的服務器地址寫錯了,上網搜索了很多內容,各種方法都嘗試,卻也沒有解決我的問題。

 

不過還好,在某某論壇上看到了用telnet進行驗證是否smtp服務器依舊工作良好。

方知曉,默認狀態下windows 8和windows 7是一樣的,默認為關閉狀態。

所以,最終在我這里是因為win8上沒有開啟telnet服務引起的。

打開的辦法是turn on windows feature,並且只能通過這種方式。(至少我沒有發現可以通過開啟服務的方式實現)

具體操作如下:

控制面板->Programs and Features->Turn on windows features.

找到telnet,勾選“telnet client”;這個是允許你連接到其他電腦的上,注意是通過tcp方式建立連接。

回到Visual Studio 11編輯器里面,重新編譯運行,郵件順利發出去了。

using System;
using System.Net;
using System.Net.Mail;

namespace EmailTestApp
{
class Program
{
static void Main(string[] args)
{
SmtpClient smtp = new SmtpClient();
MailMessage mailMsg = new MailMessage();
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

string username = "<your user name>";
string password = "<your password";

// initialize smtp
smtp.Host = "smtp.163.com";
smtp.Port = 25;
smtp.Credentials = new NetworkCredential(username, password);

// initialize mail
mailMsg.From = new MailAddress("<your-account>@163.com");
mailMsg.To.Add(new MailAddress("your-account@163.com"));
mailMsg.Subject = "Test Email";

// we want send html code so that we can see the rich text, i.e css style
mailMsg.IsBodyHtml = true;

mailMsg.Body = @"<tb>
<tr>
<td>Column #1</td>
<td>Column #2</td>
</tr>
<tr>
<td>Lucas Luo</td>
<td>Bill Gates</td>
</tr>
";

// here we send the email, you can also use SendAsync to give contro back to your caller
smtp.Send(mailMsg);

Console.WriteLine("Finish email sending......");
Console.ReadLine();
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM