我正在使用 postal 在我的 asp.net mvc 应用程序中使用邮政服务发送电子邮件,现在问题是我必须仅在特定情况下发送 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);
    }
}

现在我想让 View 中的 CC 字段可选,以便当 CC 为“null”时, View 会忽略“@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

请帮助!谢谢你。

最佳答案

您非常接近,只需更改 View 即可。抄送前需要“@:”:@Model.Cc

例如

@if (Model.Cc != null)
{
    @:CC: @Model.Cc
}

关于c# - 如何使用邮政 asp.net 使 CC 字段可选,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34389931/

10-13 03:17