2012-11-29 122 views
1

我想從我的應用程序發送郵件給不同的收件人,由用戶輸入。發送電子郵件:asp.net mvc4

我的模型類,

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 = "[email protected]"; 
       mail.Mail_Subject = "From Anganwadi"; 
       MailMessage message = new MailMessage(); 
       message.From = new MailAddress(mail.Mail_From); 
       message.To.Add(new MailAddress("[email protected]")); 
       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); 
     } 
    } 

在我的web配置我添加以下

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]"> 
     <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

您在SMTP配置from="[email protected]"並且在使用它從你的代碼,而不是地址。 此電子郵件在其域名中有無效的MX記錄(http://www.digwebinterface.com/?hostnames=foo.com%0D%0A&type=ANY&useresolver=8.8.4.4&ns=auth&nameservers=

您應該更好地使用已存在的電子郵件,甚至用於測試。

+0

仍然沒有運氣..我試着給真正的電子郵件。現在錯誤是,「無法連接到遠程服務器」 –

+1

然後檢查您用於訪問smtp服務器的憑據 – Dima