发送电子邮件时出错

时间:2012-03-27 14:39:40

标签: c# email-integration smtpclient

我正在使用this问题中描述的代码。但是,在发送电子邮件时会出现以下错误。

  

邮箱不可用。服务器响应是:请进行身份验证   使用此邮件服务器

任何想法可能出错?

更新:以下是代码

System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient(); 
MailMessage Message = new MailMessage("From", "To", "Subject", "Body"); 
Client.Send(Message); 

在App.config中有以下内容。

 <system.net> 
    <mailSettings> 
      <smtp from="support@MyDomain1.com"> 
        <network host="smtp.MyDomain1.com" port="111" userName="abc" password="helloPassword1" /> 
      </smtp> 
    </mailSettings> 
  </system.net> 

2 个答案:

答案 0 :(得分:2)

那里张贴的代码应该有效。如果没有,您可以尝试在代码隐藏中设置用户名和密码,而不是从web.config中读取它们。

来自systemnetmail.com的代码示例:

static void Authenticate()
{
   //create the mail message
   MailMessage mail = new MailMessage();

   //set the addresses
   mail.From = new MailAddress("me@mycompany.com");
   mail.To.Add("you@yourcompany.com");

   //set the content
   mail.Subject = "This is an email";
   mail.Body = "this is the body content of the email.";

   //send the message
   SmtpClient smtp = new SmtpClient("127.0.0.1");

   //to authenticate we set the username and password properites on the SmtpClient
   smtp.Credentials = new NetworkCredential("username", "secret"); 
   smtp.Send(mail);

}

答案 1 :(得分:1)

是的,smtp服务器告诉您,为了向您转发电子邮件,您需要在尝试发送电子邮件之前进行身份验证。如果您拥有smptp服务器的帐户,则可以相应地在SmtpClient对象上设置凭据。根据smtp服务器支持的身份验证机制,端口等将有所不同。

来自MSDN的示例:

public static void CreateTestMessage1(string server, int port)
{
            string to = "jane@contoso.com";
            string from = "ben@contoso.com";
            string subject = "Using the new SMTP client.";
            string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
            MailMessage message = new MailMessage(from, to, subject, body);
            SmtpClient client = new SmtpClient(server, port);
            // Credentials are necessary if the server requires the client 
            // to authenticate before it will send e-mail on the client's behalf.
            client.Credentials = CredentialCache.DefaultNetworkCredentials;

      try {
              client.Send(message);
      }
            catch (Exception ex) {
              Console.WriteLine("Exception caught in CreateTestMessage1(): {0}", 
                    ex.ToString() );
      }              
}

底线是您的凭据未传递给Smtp服务器,否则您将不会收到该错误。