发送电子邮件时出错
本文关键字:出错 电子邮件 | 更新日期: 2023-09-27 18:29:04
我正在使用本问题中描述的代码。但是,在发送电子邮件时会出现以下错误。
邮箱不可用。服务器响应为:请向进行身份验证使用此邮件服务器
有什么想法吗?
更新:这是代码
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>
发布在那里的代码应该可以工作。如果没有,您可以尝试在代码隐藏中设置用户名和密码,而不是从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);
}
是的,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服务器,否则你就不会收到这个错误。