7

I'm looking to write a C# console app that will, in the process of running send out emails. I have the emails going out simply by doing something like:

     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());
     }

I was trying to find a way to do this with MailDefinition because these emails might be longer, but in the process of doing that I ran into a little problem. The CreateMailMessage method requires a System.Web.UI.Control which I don't have because I'm not an ASP.Net Application.

Has anyone run into this problem? Or found a better way of doing this?

Thanks

zacharyc
  • 381
  • 1
  • 3
  • 6

2 Answers2

1

Ensure you've used correct .Net Framework 4 and not .Net Framework 4 client as the target framework. Add System.Web to the references of your project.

Then create the Mailmessage as: MailMessage msgHtml = message.CreateMailMessage(recipients, replacements, new System.Web.UI.Control());

zafoxyfox
  • 11
  • 1
  • This solution appears to work: http://stackoverflow.com/questions/7185105/wcf-service-fails-to-send-mail-through-maildefinition – russau Apr 02 '12 at 04:44
0

I'm not familiar with MailDefinition being superior in any way. From MSDN:

"Use the MailDefinition class to simplify creating predefined e-mail messages to be sent by a control. If you want to send e-mail not using a control, see the System.Net.Mail class." (source here)

But suppose you have good reasons to prefer MailDefinition to regular MailMessage then add reference of System.Web.dll to you project. You don't have to run in IIS to use the classes in that assembly.

Imre Pühvel
  • 4,468
  • 1
  • 34
  • 49