私は簡単な連絡フォームを作成し、コードを書いてフォームを送信します。受信しますが、空白、件名、本文はありません。ボディやテーマを送信していない連絡先
ここでstackoverlowで同様の問題を確認したところ、私のシナリオでは役に立つと思われる多くのPHPソリューションがありましたが、asp.netは見つかりませんでした。ここ
はコントローラです:
public ActionResult Send(Contact c)
{
if (ModelState.IsValid)
{
try
{
MailAddress sender = new MailAddress(c.Email, c.Name);
MailAddress recipient = new MailAddress("[email protected]");
MailMessage Message = new MailMessage();
Message.From = sender;
Message.To.Add(recipient);
Message.Subject = c.Subject;
Message.Body = c.Msg;
Message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.Port = 587;
client.Credentials = new NetworkCredential("[email protected]", "password");
client.Send(Message);
return Redirect("/Success.html");
}
catch (Exception)
{
return Redirect("/Error.html");
}
}
else
{
return Redirect("www.google.com");
}
}
HTML:
<body>
<form action="/Mail/Send" method="post">
<div id="glavni">
<p>
<label for="TextName">Name:</label>
<input id="TextName" type="text" name="Name" required autofocus />
</p>
<p>
<label for="TextSubject">Subject:</label>
<input id="TextSubject" type="text" name="Subject" required />
</p>
<p>
<label for="TextMail">Email:</label>
<input id="TextMail" type="email" name="Email" required />
</p>
<p>
<label for="TextMsg">Unesite Poruku:</label>
<textarea id="TextMsg" type="text" name="Msg" rows="12" cols="20" ></textarea>
</p>
<p>
<button type="submit">Send</button>
</p>
</div>
</form>
MODEL:
public class Contact
{
public string Name { get; set; }
public string Email { get; set; }
public string Subject { get; set; }
public string Msg {get; set;}
}
接触モデルクラスの小道具は、HTML name
属性と同じ名前を持ち、何らかの理由で空のエマを送るils。 誰かが光を当てることができますように!
完全な例は示していません。ビューコードも表示する必要があります。 – mason
@mason実践ページ以来、私はMVCを使った単純な空のページに行きました。私はビューを使用せず、単なるHTMLページでした。私はそれを一瞬でリンクします! – MicroDev
また、Contactクラスの定義を提供する必要があります。 – mason