发送电子邮件时出错
问题描述:
我正在使用this问题中描述的代码。但发送电子邮件时出现以下错误。发送电子邮件时出错
邮箱不可用。服务器响应是:请验证到 使用此邮件服务器
任何想法可能是错的什么?
UPATE:以下是代码
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="[email protected]">
<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("[email protected]");
mail.To.Add("[email protected]");
//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);
}
+0
感谢您的提示。我会尝试 – imak 2012-03-27 14:46:21
答
是的,smtp服务器告诉你,为了给你转发电子邮件,你需要在尝试发送电子邮件之前进行身份验证。如果您拥有smptp服务器的帐户,则可以相应地在SmtpClient对象上设置凭据。根据smtp服务器支持的身份验证机制,端口等会有所不同。从MSDN
例子:
public static void CreateTestMessage1(string server, int port)
{
string to = "[email protected]";
string from = "[email protected]";
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服务器,否则你不会得到这个错误。
身份验证声音,您需要提供凭据?用户密码? – gbianchi 2012-03-27 14:41:19
我已经提供了这些配置文件。我加倍检查,这些都是正确的值。 – imak 2012-03-27 14:42:12
向我们显示您的代码。我们希望看到您所做的任何修改。 – Msonic 2012-03-27 14:42:18