我使用这个代码在asp.net发送电子邮件:是什么错误? (在asp.net发送电子邮件)
使用System.Net.Mail
public string SendEmail()
{
SmtpClient obj = new SmtpClient();
MailMessage Mailmsg = new MailMessage();
Mailmsg.To.Clear();
Recievers = new MailAddressCollection();
Recievers.Add(txtToAddress.Text);
SenderName = "Info";
SenderEmail = txtFromAddress.Text;
Subject = "subj";
Body = "body";
UseBcc = false;
if (UseBcc)
{
foreach (MailAddress RecieverItem in Recievers)
{
Mailmsg.Bcc.Add(RecieverItem);
}
}
else
{
foreach (MailAddress RecieverItem in Recievers)
{
Mailmsg.To.Add(RecieverItem);
}
}
Mailmsg.From = new MailAddress(SenderEmail, SenderName, System.Text.Encoding.UTF8);
Mailmsg.Subject = Subject;
Mailmsg.SubjectEncoding = Encoding.UTF8;
Mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
Mailmsg.IsBodyHtml = false;
obj.Host = mail.domain.com;
System.Net.NetworkCredential BasicAuthenticationInfo = new System.Net.NetworkCredential("[email protected]", "password");
obj.UseDefaultCredentials = false;
obj.Credentials = BasicAuthenticationInfo;
Mailmsg.Body = Body;
Mailmsg.IsBodyHtml = true;
try
{
obj.Send(Mailmsg);
return "sent";
}
catch (Exception ex)
{
return ex.ToString();
}
}
它正确地发送电子邮件给其定义在recievers我的域名(如[email protected]),但我无法将电子邮件发送到其他邮件服务器(如[email protected])。
我的代码有什么问题?
(它可能涉及到SmtpClient属性呢?我已经smtpclient.host设置为mail.mydomain.com 并使用它们在我的域中定义我的邮件帐户之一的用户名和密码)
谢谢
这个问题是否引发任何异常?您是否检查过SMTP服务器管理员(他们是否阻止了SMTP服务器上的程序以防止将它发送到外部?) –
是的例外是:System.Net.Mail.SmtpFailedRecipientException:邮箱不可用。服务器响应是:System.Net.Mail.SmtpClient.Send(MailMessage消息)位于System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,MailAddressCollection收件人,String deliveryNotify,SmtpFailedRecipientException和异常)位于_Default.SendEmail () –
您可以检查异常SmtpFailedRecipientException中的属性“FailedRecipient”。你确定这是一个正确的收件人地址? –