2017-09-25 8 views
0

これは私がemailを送信する必要がある私のアプリケーションです。それはgmail.isでうまく動作し、yahooメールでも同じことが可能ですか? (テキストボックスから@ gmail.comがある場合は、コントローラのsmtp.gmail.comステップに行く必要があります。テキストボックスに@ yahoo.comがある場合はsmtp.mail.yahoo.comに行く必要がありますか? )mvcのアプリケーションからgmailとyahooの両方を使ってメールを送信する方法は?

私のコントローラ:

using emailtest1.Models; 
    using System.IO; 
    using System.Net; 
    using System.Net.Mail; 
    using System.Web.Mvc; 
    namespace emailtest1.Controllers 
    { 

     public class HomeController : Controller 
     { 
      // GET: Home 
      public ActionResult Index() 
      { 
       return View(); 
      } 
      [HttpPost] 
      public ActionResult Index(EmailModel model) 
      { 
       using (MailMessage mm = new MailMessage(model.Email, model.To)) 
       { 
        mm.Subject = model.Subject; 
        mm.Body = model.Body; 
        if (model.Attachment.ContentLength > 0) 
        { 
         string fileName = Path.GetFileName(model.Attachment.FileName); 
         mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName)); 
        } 
        mm.IsBodyHtml = false; 
        using (SmtpClient smtp = new SmtpClient()) 
        { 
         smtp.Host = "smtp.gmail.com"; 
         smtp.Host = "smtp.mail.yahoo.com"; 
         smtp.EnableSsl = true; 
         NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password); 
         smtp.UseDefaultCredentials = false; 
         smtp.Credentials = NetworkCred; 
         smtp.Port = 587; 
         smtp.Port = 25; 
         smtp.Send(mm); 
         ViewBag.Message = "Email sent."; 
        } 
       } 
       return View(); 
      } 
     } 
    } 

私のモデル:

using System.Web; 
namespace emailtest1.Models 
{ 
     public class EmailModel 
     { 
      public string To { get; set; } 
      public string Subject { get; set; } 
      public string Body { get; set; } 
      public HttpPostedFileBase Attachment { get; set; } 
      public string Email { get; set; } 
      public string Password { get; set; } 
     } 
} 

私の見解:

@model emailtest1.Models.EmailModel 
@{ 
    Layout = null; 
} 
<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
    <style type="text/css"> 
     body { 
      font-family: Arial; 
      font-size: 10pt; 
     } 

     table th, table td { 
      padding: 5px; 
     } 
    </style> 
</head> 
<body> 
    <div> 
     @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) 
     { 
      <table border="0" cellpadding="0" cellspacing="0"> 
       <tr> 
        <td style="width: 80px"> 
         To: 
        </td> 
        <td> 
         @Html.TextBoxFor(model => model.To) 
        </td> 
       </tr> 
       <tr> 
        <td> 
         Subject: 
        </td> 
        <td> 
         @Html.TextBoxFor(model => model.Subject) 
        </td> 
       </tr> 
       <tr> 
        <td valign="top"> 
         Body: 
        </td> 
        <td> 
         @Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" }) 
        </td> 
       </tr> 
       <tr> 
        <td> 
         File Attachment: 
        </td> 
        <td> 
         @Html.TextBoxFor(model => model.Attachment, new { type = "file" }) 

        </td> 
       </tr> 
       <tr> 
        <td> 
         Gmail: 
        </td> 
        <td> 
         @Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { id = "my_custom_id" } }) 
        </td> 
       </tr> 
       <tr> 
        <td> 
         Gmail Password: 
        </td> 
        <td> 
         @*@Html.TextBoxFor(model=>model.Password)*@ 
         @Html.TextBoxFor(model => model.Password, new { type = "password" }) 
        </td> 
       </tr> 
       <tr> 
        <td></td> 
        <td> 
         <input type="submit" value="Send" /> 
        </td> 
       </tr> 
      </table> 
      <br /> 
      <span style="color:green">@ViewBag.Message</span> 
     } 
    </div> 
</body> 
</html> 
+0

を行う。その後、この

smtp.Port = 587; if (textbox.value.contains("@gmail.com")) smtp.Host = "smtp.gmail.com"; ELSE (textbox.value.contains("@yahoo.com")) smtp.Host = "smtp.mail.yahoo.com"; 

で、どのように私は両方行うのですか」ヤフーとの電子メールを送信すると問題がある、または問題です"?もしそうなら、あなたが書いたステートメントのif/else条件を試しましたか? ( 'テキストボックスから@ gmail.comを持っている場合は、テキストボックスから@ yahoo.comを持っていれば、smtp.gmail.comに行く必要があります。smtp.mail.yahoo.com'に行く必要があります)? –

+0

@ashwanthトピックを解決できますか? – GGO

+0

私は解決策を得ました@GGOありがとう – ashwanth

答えて

6

ホストとポートのプロパティを順番に上書きすることはできません。あなたは条件を入れてあります。

if(model.Email.contains("@gmail.com")){ 
    smtp.Host = "smtp.gmail.com"; 
    smtp.Port = 587; 
} else if(model.Email.contains("@yahoo.fr")) { 
    smtp.Host = "smtp.mail.yahoo.com"; 
    smtp.Port = 587; 
} 
1

はいしかし、あなたがやっているとき、あなたはそれをやった方法を好まない:

smtp.Host = "smtp.gmail.com"; 
smtp.Host = "smtp.mail.yahoo.com"; 

あなたはSMTPを上書きしています。あなたは何ができるか

を明確にする作業...

関連する問題