C# ASP.Net应用程序外部的邮件定义

C# ASP.Net应用程序外部的邮件定义,c#,C#,我想写一个C#console应用程序,在运行过程中可以发送电子邮件。我只需通过以下方式发送电子邮件: MailMessage message = new MailMessage("foo@foo.com", "bar@bar.com", "Test message", "Test message content"); message.IsBodyHtml = true; message.Body = "<a href=\"http://www.daringfi

我想写一个C#console应用程序,在运行过程中可以发送电子邮件。我只需通过以下方式发送电子邮件:

     MailMessage message = new MailMessage("foo@foo.com", "bar@bar.com", "Test message", "Test message content");
     message.IsBodyHtml = true;
     message.Body = "<a href=\"http://www.daringfireball.net\">DaringFireball.net</a>";
     SmtpClient client = new SmtpClient("localhost"); // Your host here

     try
     {
        client.Send(message);
     }
     catch (Exception e)
     {
        Console.WriteLine("There was an error trying to send the message: " + e.ToString());
     }
MailMessage=newmailmessage(“foo@foo.com", "bar@bar.com“,”测试消息“,”测试消息内容“);
message.IsBodyHtml=true;
message.Body=“”;
SmtpClient客户端=新的SmtpClient(“本地主机”);//你的主人在这里
尝试
{
客户端。发送(消息);
}
捕获(例外e)
{
Console.WriteLine(“尝试发送消息时出错:+e.ToString());
}
我试图用MailDefinition找到这样做的方法,因为这些电子邮件可能更长,但在这样做的过程中,我遇到了一个小问题。CreateMailMessage方法需要System.Web.UI.Control,我没有该控件,因为我不是ASP.Net应用程序

有人遇到过这个问题吗?还是找到了更好的方法


谢谢

我不知道
MailDefinition
在任何方面都是优秀的。从MSDN:

“使用MailDefinition类 简化创建预定义的电子邮件 控件要发送的消息。如果 您希望不使用电子邮件发送电子邮件 控件,请参阅 类别。”()


但是,假设您有充分的理由选择
MailDefinition
而不是常规的
MailMessage
,然后将System.Web.dll的引用添加到您的项目中。您不必在IIS中运行即可使用该程序集中的类。

请确保使用了正确的.Net Framework 4而不是.Net Framework 4客户端作为目标框架。 将System.Web添加到项目的引用中

然后将邮件消息创建为:
MailMessage msgHtml=message.CreateMailMessage(收件人、替换、新系统.Web.UI.Control())

MailDefinition
MailMessage
不能做的一方带来了什么?@JimMischel-
MailDefinition
CreateMailMessage
方法提供了一种从文本文件创建电子邮件的简洁方法
MailMessage
没有类似的内容。此解决方案似乎可以工作: