2016-06-25 9 views
0

私はASP.NET MVCを使用してWebサイトで作業していましたが、このWebサイトでは特定のメールアドレスに直接メールを送信できます。正常に動作していますが、電子メールで送信される情報(名前、電子メールアドレスなど)にはデータベースがありません。だから私はそれのためのデータベースを追加しようとしたが、何とかそれは動作していないと私はいくつかのエラーを抱えている。私はこの種のものでちょうど新しいので、電子メールを送信すると同時にデータベースに保存することが可能かどうかはわかりません。私がやっていることに何か問題があることを知っているので、誰かが私を助けてください。ありがとうございました。ここで電子メールを送信してデータベースに保存する(ASP.NET MVC)

は、電子メールの送信のための私のコントローラの:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(TalentInfo model, IEnumerable<HttpPostedFileBase> files) 
    { 
     if (ModelState.IsValid) 
     { 
      RegisterRepository regRepo = new RegisterRepository(); 

      if (regRepo.Register(model)) 
      { 
       List<string> paths = new List<string>(); 

       foreach (var file in files) 
       { 
        if (file.ContentLength > 0) 
        { 
         var fileName = Path.GetFileName(file.FileName); 
         var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); 
         file.SaveAs(path); 
         paths.Add(path); 
        } 
       } 

       var message = new MailMessage(); 
       foreach (var path in paths) 
       { 
        var fileInfo = new FileInfo(path); 
        var memoryStream = new MemoryStream(); 
        using (var stream = fileInfo.OpenRead()) 
        { 
         stream.CopyTo(memoryStream); 
        } 
        memoryStream.Position = 0; 
        string fileName = fileInfo.Name; 
        message.Attachments.Add(new Attachment(memoryStream, fileName)); 
       } 

       //Rest of business logic here 
       string EncodedResponse = Request.Form["g-Recaptcha-Response"]; 
       bool IsCaptchaValid = (ReCaptcha.Validate(EncodedResponse) == "True" ? true : false); 
       if (IsCaptchaValid) 
       { 

        var body = "<p><b>Email From:</b> {0} ({1})</p><p><b>Message:<b></p><p>{2}</p>"; 
        message.To.Add(new MailAddress("***@gmail.com")); // replace with valid value 
        message.From = new MailAddress("***@ymailcom"); // replace with valid value 
        message.Subject = "YesDubai.org (REGISTRATION)"; 
        message.Body = string.Format(body, model.Talent_Name, model.Talent_Email, model.Talent_SelfPromotion); 
        message.IsBodyHtml = true; 
        using (var smtp = new SmtpClient()) 
        { 
         var credential = new NetworkCredential 
         { 
          UserName = "***@gmail.com", // replace with valid value 
          Password = "***" // replace with valid value 
         }; 
         smtp.Credentials = credential; 
         smtp.Host = "smtp.gmail.com"; 
         smtp.Port = 587; 
         smtp.EnableSsl = true; 
         smtp.SendCompleted += (s, e) => 
         { 
          //delete attached files 
          foreach (var path in paths) 
           System.IO.File.Delete(path); 
         }; 
         await smtp.SendMailAsync(message); 
         ViewBag.Message = "Your message has been sent!"; 

         ModelState.Clear(); 
         return View("Register"); 
        } 
       } 
       else 
       { 
        TempData["recaptcha"] = "Please verify that you are not a robot!"; 
       } 

      } return View(model); 

     } 
    } 

そしてここでモーダルクラスです:

public partial class TalentInfo 
{ 
    [Display(Name = "ID")] 
    public int TalentID { get; set; } 
    [Display(Name = "Talent's Name")] 
    public string Talent_Name { get; set; } 
    [Display(Name = "Email Address")] 
    public string Talent_Email { get; set; } 
    [Display(Name = "Self Promotion")] 
    public string Talent_SelfPromotion { get; set; } 
} 

そして、ここでは、リポジトリの:

​​
+1

いただきましエラー?貼り付けます –

+0

@AbdulRehmanSayed ** TalentInfo、System.Collections.Generic.IEnumerable ) ':すべてのコードパスが値を返すわけではありません。 –

+2

(ModelState.IsValid)がfalseと評価された場合、登録メソッドは値を返しません。 –

答えて

2

これは単にコンパイルですエラー。コードのすべてのパスで結果を返す必要があります。 (ModelState.IsValid)はfalseと評価されている場合とき

1)何かを返します:

は、あなたのコード内の2つのエラーがあるここ

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Register(TalentInfo model, IEnumerable<HttpPostedFileBase> files) 
{ 
    if (ModelState.IsValid) 
    { 
     //you have lots of code here.... 
    } 
    else 
    { 
     //you need to return something here....because (ModelState.IsValid) might be false 
    } 
} 
+0

ありがとう、エラーが修正されました。電子メールは正しく送信されましたが、何らかの理由でデータベースに保存されません。あなたが私を助けてくれることを願っています。ありがとうございました。 –

1

returnが欠落しています。 レジスタ方法で

2)、この行を削除:

throw new NotImplementedException(); 
+0

私はすでにこのサーをしましたが、実行しようとすると**ローカルホストが接続を拒否しました**エラーが発生します。 –

+0

これは異なるエラーです。 –

+0

あなたはそのエラーを解決する方法を知っていますか?ありがとうございました。 –

関連する問題