日期:2014-05-18  浏览次数:20677 次

有关发送邮件的问题:SmtpFailedRecipientException
代码:
try
                {
                        MailAddress   from   =   new   MailAddress( "miracle@miracle.com ");
                        MailAddress   to   =   new   MailAddress( "sunnxxy@yahoo.com.cn ");
                        MailMessage   message   =   new   MailMessage(from,   to);
                        message.Subject   =   "Using   the   SmtpClient   class. ";
                        message.Body   =   @ "Using   this   feature,   you   can   send   an   e-mail   message   from   an   application   very   easily. ";
                        SmtpClient   client   =   new   SmtpClient( "000.000.0.000 ");
                        client.Send(message);
                }
                catch   (Exception   f)
                {
                        throw   f;
                }
        }
错误:
System.Net.Mail.SmtpFailedRecipientException:   邮箱不可用。   服务器响应为:   5.7.1   Unable   to   relay   for   sunnxxy@yahoo.com.cn
我第一次用这个东西。。不知道哪里出错了。。希望知道的大哥大姐可以帮帮我。谢谢了!

------解决方案--------------------

邮件类:
public bool SendMail(string to)
{
MailMessage sm=new MailMessage();
sm.From=ConfigurationSettings.AppSettings[ "mailfrom "];
sm.To=to;
sm.Subject=ConfigurationSettings.AppSettings[ "mailsubject "];
sm.Body=ConfigurationSettings.AppSettings[ "mailbody "];
sm.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ",
"1 ");
sm.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername ",ConfigurationSettings.AppSettings[ "mailsendusername "]);
sm.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword ",ConfigurationSettings.AppSettings[ "mailsenduserpwd "]);
SmtpMail.SmtpServer=ConfigurationSettings.AppSettings[ "smtpserver "];
try
{
SmtpMail.Send(sm);
return true;
}
catch
{
return false;
}
}

web.config的设置:

<appSettings>
<add key= "mailfrom " value= " "/>
<add key= "mailsubject " value= " "/>
<add key= "mailbody " value= " "/>
<add key= "mailsendusername " value= " "/>
<add key= "mailsenduserpwd " value= " "/>
<add key= "smtpserver " value= " "/>
</appSettings>