gpt4 book ai didi

asp.net-mvc - MailDefinition/MailMessage 和基本路径不能为空

转载 作者:行者123 更新时间:2023-12-03 02:19:25 27 4
gpt4 key购买 nike

我正在使用以下代码创建 MailMessage,但在调用 mail.CreateMailMessage 时收到此错误。

Value cannot be null. Parameter name: basepath

有人知道可能出了什么问题吗?

public void SendReciept(string reciever)
{
MailDefinition mail = new MailDefinition();
mail.IsBodyHtml = true;

mail.BodyFileName = "~/file.txt";
mail.Subject = "Subject";
mail.From = "noreply@xxx.com";
mail.Priority = System.Net.Mail.MailPriority.Normal;

MailMessage message = mail.CreateMailMessage(reciever, RecieptReplacements, new System.Web.UI.Control());

...
}

ListDictionary RecieptReplacements
{
get
{
ListDictionary replacements = new ListDictionary();

replacements.Add("<%Name%>", "Name");

return replacements;
}
}

最佳答案

您需要创建一个 Web 用户控件(.ascx 文件),然后加载该控件并将其传递给 CreateMailMessage

// Create a dummy page and use it to load a dummy control so that CreateMailMessage() will not complain
Page page = new Page();
DummyControl control = (DummyControl)page.LoadControl("~/DummyControl.ascx");
MailMessage message = mail.CreateMailMessage(reciever, RecieptReplacements, dummycontrol);

看看这个 answer ,它解释了原因并提供了上述解决方案。

关于asp.net-mvc - MailDefinition/MailMessage 和基本路径不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com