如何在 MVC 中使用 LINQ 结果发送电子邮件
How to send email with LINQ result in MVC
美好的一天
我有一个控制器,我通过 ID 查询记录,在查询时,它会向我显示记录
使用该 ID 存储,但如何通过单击提交按钮通过电子邮件发送这些结果?
这是我的控制器
public ActionResult Search(decimal? reqid, object sender, EventArgs e)
{
Entities db = new Entities();
var req = from s in db.TB_CS_TEST
join st in db.TB_CS_TEST2 on s.REQ_NO equals st.REQ_NO
where s.REQ_NO.Equals(st.REQ_NO)
select new SearchModels
{
GetTB1 = s,
GetTB2 = st
};
if (reqid.HasValue)
{
req = req.Where(s => s.GetTB1.REQ_NO == reqid);
}
return View(req);
}
编辑:
我知道使用了 SMPT 客户端,但我的问题是如何发送视图在电子邮件正文中抛出的结果。
这是我的电子邮件功能
public void SendEmail()
{
SmtpClient SmtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
SmtpServer.Credentials = new System.Net.NetworkCredential("send@email.com", ""); //This is an example user
SmtpServer.Port = 25;
SmtpServer.Host = "154.127.1.150"; // This is an example Host
mail = new MailMessage();
mail.From = new MailAddress("user@email.com", "email");
mail.To.Add("user2@email.com");
mail.Subject = "SUB";
mail.Body = ""; // HERE IS THE BODY THAT I WANT TO FILL WITH VIEW RESULT
mail.Priority = MailPriority.High;
SmtpServer.Send(mail);
}
这是我的看法
@model IEnumerable<MvcApplication31.Models.SearchModels>
@{
ViewBag.Title = "Search";
}
<h2>Search</h2>
@using (Html.BeginForm()){
<p> Request Number: @Html.TextBox("reqid") <br />
<input type="submit" value="Filter" /></p>
}
<table class="table table-striped">
<tr>
<td>Req No</td>
<td>Item Number</td>
<td>Item Name</td>
<td>Quantity</td>
<td>UOM</td>
<td>Event Date</td>
<td>Event Place</td>
<td>Purpose</td>
<td>Approver 1</td>
<td>Approver 2</td>
<td>Status</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.GetTB1.REQ_NO</td>
<td>@item.GetTB1.SEQ_NO</td>
<td>@item.GetTB1.ITEM_NAME</td>
<td>@item.GetTB1.QUANTITY</td>
<td>@item.GetTB1.UOM</td>
<td>@item.GetTB2.EVENT_DATE</td>
<td>@item.GetTB2.EVENT_PLACE</td>
<td>@item.GetTB2.PURPOSE</td>
<td>@item.GetTB2.APPROVER1</td>
<td>@item.GetTB2.APPROVER2</td>
<td>@item.GetTB2.STATUS</td>
<td></td>
</tr>
}
</table>
如您所见,我的目标是能够在电子邮件正文中发送@item.GetTB1 和@item.GetTB2 的值
谢谢
只有我需要一个 StringWriter 然后用作电子邮件正文
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
//public void SendEmail(IQueryable<TB_RS_PROD> s)
public ActionResult SendEmail(string s,string ss)
{
decimal sl = (decimal)TempData["rt"];
string acceptable = "a|tr|td|table|html|img|br|th|thead|tbody"; //Tags OK
string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*??)?)*\s*/?>";
var htmlp = RenderPartialViewToString("~/Views/ActionApprovers/Action.cshtml", TempData["email-act"]);
htmlp = Regex.Replace(htmlp, stringPattern, "");
var link = "<a href='http://localhost:62357/"
+ @Url.Action("Action", "ActionApprovers", new { idprod = sl })
+ "'>Click here to review request</a>";
SmtpClient SmtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
SmtpServer.Credentials = new System.Net.NetworkCredential("send@email.com", "");
SmtpServer.Port = 25;
SmtpServer.Host = "154.127.1.150";
mail = new MailMessage();
mail.From = new MailAddress("send@email.com", "admin");
mail.To.Add(s);
//mail.To.Add(ss);
mail.Subject = "Request ";
mail.IsBodyHtml = true;
mail.Body += "<style>";
mail.Body += "*, *:before, *:after { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Open Sans', sans-serif; } table { background: #ffffff; border: 0.25px solid black; border-radius: 0.25em; border-collapse: collapse; margin: 1em; } th { border-bottom: 1px solid #364043; color: #ffffff; background-color: #a50034; font-size: 0.85em; font-weight: 600; padding: 0.5em 1em; text-align: left; } td { color: #000; font-weight: 400; padding: 0.65em 1em; } .disabled td { color: #000; } tbody tr { transition: background 0.25s ease; } tbody tr:hover { background: #000 } tr:hover td { color: #fff;} p { color: #000000; font-family: 'Open Sans', sans-serif; } p.thick { color: #000000; font-family: 'Open Sans', sans-serif; font-weight: bold; }";
mail.Body += "</style>";
mail.Body += "<p> CAFETERIA SERVICE REQUEST: " + "<b>#" + sl + "</b></p>";
mail.Body += "<br/>";
mail.Body += "<br/>";
mail.Body += htmlp;
mail.Body += "<br/>";
mail.Body += "<br/>";
mail.Body += "<p>You have received a cafeteria special service approval, Please access below link to review it <p>";
mail.Body += link;
mail.Priority = MailPriority.High;
SmtpServer.Send(mail);
return View("~/Views/Home/About.cshtml");
}
美好的一天
我有一个控制器,我通过 ID 查询记录,在查询时,它会向我显示记录 使用该 ID 存储,但如何通过单击提交按钮通过电子邮件发送这些结果?
这是我的控制器
public ActionResult Search(decimal? reqid, object sender, EventArgs e)
{
Entities db = new Entities();
var req = from s in db.TB_CS_TEST
join st in db.TB_CS_TEST2 on s.REQ_NO equals st.REQ_NO
where s.REQ_NO.Equals(st.REQ_NO)
select new SearchModels
{
GetTB1 = s,
GetTB2 = st
};
if (reqid.HasValue)
{
req = req.Where(s => s.GetTB1.REQ_NO == reqid);
}
return View(req);
}
编辑:
我知道使用了 SMPT 客户端,但我的问题是如何发送视图在电子邮件正文中抛出的结果。
这是我的电子邮件功能
public void SendEmail()
{
SmtpClient SmtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
SmtpServer.Credentials = new System.Net.NetworkCredential("send@email.com", ""); //This is an example user
SmtpServer.Port = 25;
SmtpServer.Host = "154.127.1.150"; // This is an example Host
mail = new MailMessage();
mail.From = new MailAddress("user@email.com", "email");
mail.To.Add("user2@email.com");
mail.Subject = "SUB";
mail.Body = ""; // HERE IS THE BODY THAT I WANT TO FILL WITH VIEW RESULT
mail.Priority = MailPriority.High;
SmtpServer.Send(mail);
}
这是我的看法
@model IEnumerable<MvcApplication31.Models.SearchModels>
@{
ViewBag.Title = "Search";
}
<h2>Search</h2>
@using (Html.BeginForm()){
<p> Request Number: @Html.TextBox("reqid") <br />
<input type="submit" value="Filter" /></p>
}
<table class="table table-striped">
<tr>
<td>Req No</td>
<td>Item Number</td>
<td>Item Name</td>
<td>Quantity</td>
<td>UOM</td>
<td>Event Date</td>
<td>Event Place</td>
<td>Purpose</td>
<td>Approver 1</td>
<td>Approver 2</td>
<td>Status</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.GetTB1.REQ_NO</td>
<td>@item.GetTB1.SEQ_NO</td>
<td>@item.GetTB1.ITEM_NAME</td>
<td>@item.GetTB1.QUANTITY</td>
<td>@item.GetTB1.UOM</td>
<td>@item.GetTB2.EVENT_DATE</td>
<td>@item.GetTB2.EVENT_PLACE</td>
<td>@item.GetTB2.PURPOSE</td>
<td>@item.GetTB2.APPROVER1</td>
<td>@item.GetTB2.APPROVER2</td>
<td>@item.GetTB2.STATUS</td>
<td></td>
</tr>
}
</table>
如您所见,我的目标是能够在电子邮件正文中发送@item.GetTB1 和@item.GetTB2 的值
谢谢
只有我需要一个 StringWriter 然后用作电子邮件正文
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
//public void SendEmail(IQueryable<TB_RS_PROD> s)
public ActionResult SendEmail(string s,string ss)
{
decimal sl = (decimal)TempData["rt"];
string acceptable = "a|tr|td|table|html|img|br|th|thead|tbody"; //Tags OK
string stringPattern = @"</?(?(?=" + acceptable + @")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:(["",']?).*??)?)*\s*/?>";
var htmlp = RenderPartialViewToString("~/Views/ActionApprovers/Action.cshtml", TempData["email-act"]);
htmlp = Regex.Replace(htmlp, stringPattern, "");
var link = "<a href='http://localhost:62357/"
+ @Url.Action("Action", "ActionApprovers", new { idprod = sl })
+ "'>Click here to review request</a>";
SmtpClient SmtpServer = new SmtpClient();
MailMessage mail = new MailMessage();
SmtpServer.Credentials = new System.Net.NetworkCredential("send@email.com", "");
SmtpServer.Port = 25;
SmtpServer.Host = "154.127.1.150";
mail = new MailMessage();
mail.From = new MailAddress("send@email.com", "admin");
mail.To.Add(s);
//mail.To.Add(ss);
mail.Subject = "Request ";
mail.IsBodyHtml = true;
mail.Body += "<style>";
mail.Body += "*, *:before, *:after { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Open Sans', sans-serif; } table { background: #ffffff; border: 0.25px solid black; border-radius: 0.25em; border-collapse: collapse; margin: 1em; } th { border-bottom: 1px solid #364043; color: #ffffff; background-color: #a50034; font-size: 0.85em; font-weight: 600; padding: 0.5em 1em; text-align: left; } td { color: #000; font-weight: 400; padding: 0.65em 1em; } .disabled td { color: #000; } tbody tr { transition: background 0.25s ease; } tbody tr:hover { background: #000 } tr:hover td { color: #fff;} p { color: #000000; font-family: 'Open Sans', sans-serif; } p.thick { color: #000000; font-family: 'Open Sans', sans-serif; font-weight: bold; }";
mail.Body += "</style>";
mail.Body += "<p> CAFETERIA SERVICE REQUEST: " + "<b>#" + sl + "</b></p>";
mail.Body += "<br/>";
mail.Body += "<br/>";
mail.Body += htmlp;
mail.Body += "<br/>";
mail.Body += "<br/>";
mail.Body += "<p>You have received a cafeteria special service approval, Please access below link to review it <p>";
mail.Body += link;
mail.Priority = MailPriority.High;
SmtpServer.Send(mail);
return View("~/Views/Home/About.cshtml");
}