我通过外部SMTP服务器发送电子邮件.使用以下代码处理发送电子邮件:
try
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress(froma);
mail.To.Add(toc);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
SmtpClient smtp = new SmtpClient(ClientServer);
DataSet ds = new DataSet();
int retCode = Email.getSmtp(ref ds, DatabaseName);
string User="";
string Password="";
if (ds.Tables["Value"].Rows.Count > 0)
{
User = ds.Tables["Value"].Rows[0]["UserName"].ToString();
Password = ds.Tables["Value"].Rows[0]["PasswordName"].ToString();
}
else
{
MessageBox.Show("Invalid SMTP settings!");
return;
}
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(User, Password);
smtp.EnableSsl = false;
smtp.Send(mail);
}
catch (System.Web.HttpException exHttp)
{
System.Console.WriteLine("Exception occurred:" + exHttp.Message);
}
在我的服务器上测试此代码,在同一网络上使用我自己的SMTP服务器,这将返回我的所有电子邮件.但是,使用外部SMTP服务器会导致错误:
Mailbox unavailable. The server response was: relay not permitted.
我已经阅读过,看来SMTP的管理员必须允许我的服务器的中继.但是,使用提供的身份验证凭据,我似乎无法连接,并仍然收到中继错误.