C# 使用MailMessage类

C# 使用MailMessage类,c#,asp.net,mailmessage,C#,Asp.net,Mailmessage,我正在使用MailMessage类发送电子邮件 MailMessage msg = new MailMessage(fromAddr, toAddr); 当我创建新的MailMessage对象时,它会使用fromAddr自动获取主机chamara@pindoc.com.au它假定主机为pindoc.com.au,但我有一个不同的主机名。因此主机名是错误的 {“邮箱不可用。服务器响应为:5.7.1无法中继”}System.Exception{System.Net.Mail.SmtpFailed

我正在使用MailMessage类发送电子邮件

 MailMessage msg = new MailMessage(fromAddr, toAddr);
当我创建新的MailMessage对象时,它会使用fromAddr自动获取主机chamara@pindoc.com.au它假定主机为pindoc.com.au,但我有一个不同的主机名。因此主机名是错误的

{“邮箱不可用。服务器响应为:5.7.1无法中继”}System.Exception{System.Net.Mail.SmtpFailedRecipientException}

我怎么解决这个问题呢?

你检查过你的电脑了吗?示例web.config如下:

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="no-reply@yourdomain.com">
      <network defaultCredentials="true" host="mail.yourdomain.com" port="25"/>
   </smtp>
 </mailSettings>
</system.net>

您可以在创建SmtpClient对象的实例时指定邮件服务器(以及端口号和身份验证等其他详细信息)

您还可以指定您的,SmtpClient将自动拾取这些

SmtpClient client = new SmtpClient();
client.Send(msg);
通常,我将使用发送消息。它的构造函数接受主机和端口:

SmtpClient mailClient = new SmtpClient("mail.domain.com", 25);
mailClient.Send(msg);
SmtpClient mailClient = new SmtpClient("mail.domain.com", 25);
mailClient.Send(msg);