2014-10-09 82 views
-1

我想在我的Windows系统上创建一个带有身份验证的SMTP服务器。出于测试原因,我不想从互联网下载另一个SMTP工具,所以我尝试直接在代码中发送电子邮件(仅用于调试)。我的代码此刻:通过SmtpClient发送邮件的例外

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.Mail; 

namespace smttest 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     SmtpClient smtpClient = new SmtpClient(); 
     NetworkCredential basicCredential = 
     new NetworkCredential("username", "password"); 
     MailMessage message = new MailMessage(); 
     MailAddress fromAddress = new MailAddress("[email protected]"); 

     smtpClient.Port = 26; 
     smtpClient.Host = "localhost"; 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = basicCredential; 

     message.From = fromAddress; 
     message.Subject = "testmail"; 
     //Set IsBodyHtml to true means you can send HTML email. 
     message.IsBodyHtml = true; 
     message.Body = "testmail from sharp"; 
     message.To.Add(new MailAddress("[email protected]")); 

     try 
     { 
      smtpClient.Send(message); 
     } 
     catch(Exception ex) 
     { 
      //Error, could not send the message 
      Console.Write(ex.Message); 
      Console.ReadLine(); 
     } 


    } 
} 
} 

该cmd只显示我:发送电子邮件时发生错误(德语翻译)。我不知道如何解决这个问题。

+0

我错过了什么吗?所有代码都与SMTP客户端相关,而您的问题可能与您的服务器实现有关...... – 2014-10-09 16:22:48

+0

您想创建一个SMTP *服务器*?你为什么要这样做?对于已经存在于黑桃中的产品来说,这是很多*的工作。另外,您发布的代码不是SMTP服务器。它使用* SMTP服务器。如果您编写该服务器,并且该服务器不发送电子邮件,则该问题将在该服务器上*。不在向其发送请求的代码中。 – David 2014-10-09 16:24:02

+0

它捕捉到异常吗?如果可以的话,你可以给完整的堆栈跟踪吗? – Grice 2014-10-09 16:24:13

回答

0

您正在使用localhost作为您的SMTP服务器。它是电子邮件服务器吗?如果不是,则不能使用SMTPClient发送电子邮件。您需要SMTP服务器,如smtp.google.com

-1

我有点困惑。我需要一个运行在C#中的具有auth(简单用户/密码)的电子邮件(SMTP)服务器,使用名为“MaxBulkMailer”的另一个软件发送电子邮件。 c#中的SMTP服务器应该在cmd上运行,不需要GUI。

+0

请编辑你的问题,因为这不能回答你的问题。 – 2014-10-10 08:55:40