我在我的 asp.net mvc 应用程序中使用 postal 使用邮政服务发送电子邮件现在问题是我必须仅在特定情况下发送 CC 电子邮件,并且我必须将此字段设为可选,但到目前为止我还没有做到这一点。以下是我的代码:
查看文件:
@model OAC.Helpers.EmailHelper
To: @Model.To
Form: @Model.From
CC: @Model.CC
Subject: @Model.Subject
Hello,
@Model.Body
@Model.Link
public class EmailHelper : Email
{
public string To { get; set; }
public string CC { get; set; }
public string Body { get; set; }
public string Subject { get; set; }
public string From { get; set; }
public string Link { get; set; }
public static void SendEmailNotification(string emailAddress, string body, string link, string subject, string from = "OAC Support", string CC = null)
{
// Prepare Postal classes to work outside of ASP.NET request
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var emailService = new EmailService(engines);
var email = new EmailHelper
{
To = emailAddress,
CC = CC,
Body = body,
Subject = subject,
From = from,
Link = link
};
emailService.Send(email);
}
}
现在我想让视图中的 CC 字段可选,这样当 CC 为“null”时,视图会忽略“@Model.CC”字段。如下所示:
@model OAC.Helpers.EmailHelper
To: @Model.To
Form: @Model.From
if(Model.CC != null)
{
CC: @Model.CC
}
Subject: @Model.Subject
Hello,
@Model.Body
@Model.Link
请帮助!谢谢你。