zoukankan      html  css  js  c++  java
  • 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();
    }
    }
    }
  • 相关阅读:
    【HDOJ】2267 How Many People Can Survive
    【HDOJ】2268 How To Use The Car
    【HDOJ】2266 How Many Equations Can You Find
    【POJ】2278 DNA Sequence
    【ZOJ】3430 Detect the Virus
    【HDOJ】2896 病毒侵袭
    求奇数的乘积
    平方和与立方和
    求数列的和
    水仙花数
  • 原文地址:https://www.cnblogs.com/lucasluo/p/2381239.html
Copyright © 2011-2022 走看看