我正在创建一个电子邮件表单,此表单正在运行。现在我想为主题字段添加选项。如果主题取消,则应在消息(正文)字段中显示取消消息,例如您的服务已取消。如果主题是欢迎,那么它应该在消息(正文)字段中显示欢迎来到我们的团队消息。
<div class="container" style="background-color:powderblue; position:center">
<form method="post" action="Form" style="background-color:powderblue; color:indianred">
<span class="form-control-static" style="color:black"><h1>Email Form</h1></span>
<br />
<span class="form-control-static">Receiver Email:</span>
<input class="form-control" type="text" name="receiverEmail" />
<span class="form-control-static"> Subject:</span>
<select name="SelectSubject" value="Select Subject" id="ViewBy" class=" form-control">
<option name="cancel" value="Cancel">Cancel</option>
<option name="welcome" value="Welcome">Welcome</option>
</select>
<span class="form-control-static">Message</span>
<textarea class="form-control" cols="8" rows="9" name="message"></textarea>
<br />
<br />
<button class="btn btn-primary" type="submit">Send Email</button>
</form>
</div class="container">
这是我的电子邮件表单代码
[HttpPost]
public ActionResult Form(string receiverEmail, string subject, string message)
{
try
{
if (ModelState.IsValid)
{
var senderemail = new MailAddress("test57697@gmail.com","Test Email");
var receiveremail = new MailAddress(receiverEmail, "Receiver");
var password = "Test111222";
//From view
var sub = subject;
var body = message;
//var sub = subject;
//var body = message;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(senderemail.Address,password)
};
using (var mess = new MailMessage(senderemail, receiveremail)
{
Subject = sub,
Body = body
})
{
smtp.Send(mess);
}
Response.Write("Message sent successfully!");
return View();
}
}
catch (Exception)
{
ViewBag.Error = "Couldn't send email.";
}
return View();
}
}
}
嗨!我现在有另一个问题。我使用gmail服务器发送电子邮件,但现在我想使用 django服务器,C#而不是gmail。有什么建议吗?
答案 0 :(得分:1)
根据我的理解,你在这里编写的代码是不合适的,但是根据我的理解,它们会为你的场景提供一个解决方案。
//来自视图
var sub = subject;
if(sub.ToLower()=="cancel")
{
var body= "your service is cancelled";
}
else
{
var body= "Welcome Message";
}
答案 1 :(得分:0)
谢谢Rajesh。这是正确的答案而且有效。我刚刚删除了最后一行。
var sub = subject;
var body = message;
if (sub.ToLower() == "cancel")
{
body = "your service is cancelled";
}
else
{
body = "Welcome Message";
}