发送电子邮件:asp.net mvc4

时间:2012-11-29 05:26:41

标签: asp.net email asp.net-mvc-4

我正在尝试将应用程序中的邮件发送给用户输入的不同收件人。

我的模特课,

public class Mail
 {
    public int MailId { get; set; }

    public string Mail_To { get; set; }

    public string Mail_From { get; set; }

    public string Mail_Subject { get; set; }

    public string Mail_Contents { get; set; }

    public DateTime Mail_Date { get; set; }

    [ForeignKey("Anganwadi")]
    public int AnganwadiId { get; set; }
    public virtual Anganwadi Anganwadi { get; set; }
}

我的控制器看起来像,

[HttpPost]
    public ViewResult SendMail(Mail mail) 
    {
        try
        {
if (ModelState.IsValid)
            {
                mail.Mail_Date = System.DateTime.Now;
                mail.AnganwadiId = 1;
                mail.Mail_From = "k@gmail.com";
                mail.Mail_Subject = "From Anganwadi";
                MailMessage message = new MailMessage();
                message.From = new MailAddress(mail.Mail_From);
                message.To.Add(new MailAddress("v@gmail.com"));
                message.Subject = mail.Mail_Subject;
                message.Body = mail.Mail_Contents;

                SmtpClient client = new SmtpClient();
                client.Send(message);
                return View("Thanks", mail);
            }
            else
            {
                return View();
            }
        }
        catch (Exception e)
        {
            return View(e);
        }
    }

在我的网页配置中,我添加了以下

<system.net>
  <mailSettings>
    <smtp from="test@foo.com">
      <network host="smtp.efernssolutions.com" port="587" defaultCredentials="true" />
    </smtp>
  </mailSettings>
</system.net>

获取错误

Mailbox unavailable. The server response was: authsmtp12: Host 117.196.165.214: No unauthenticated relaying permitted

我不确定这是否正确。请帮帮我。

谢谢,

1 个答案:

答案 0 :(得分:1)

您在smtp配置中有from="test@foo.com",而不是使用代码中的地址。 此电子邮件在其域(http://www.digwebinterface.com/?hostnames=foo.com%0D%0A&type=ANY&useresolver=8.8.4.4&ns=auth&nameservers=

中包含无效的MX记录

您应该更好地使用现有的电子邮件进行测试。