1

I'm using shared resources throughout my program and I'm making a new tool that loops through and does updates. I've got a lot of places where I call a class from a control that sends emails and the rest of the code is written so I don't really want to change it. However, I can't work out what to pass in to CreateMailMessage to replace System.web.ui.control as it falls over when the class is called from a console application (works fine from the .net web app)

public static void SendMail(String emailTo, String Subject, String Template, ListDictionary Replace)
{
    MailDefinition md = new MailDefinition();
    md.From = "support@jkkkjjk.com";
    md.IsBodyHtml = true;

    md.Subject = Subject;

    string body = DBCommon.getEmailTemplate(Template);
    MailMessage msg = md.CreateMailMessage(emailTo, Replace, body, new System.Web.UI.Control());
    SmtpClient mailer = new SmtpClient();
    mailer.Host = "relay.plus.net";
    mailer.Send(msg);
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
MissCoder87
  • 2,669
  • 10
  • 47
  • 82
  • Why are you passing a control to `CreateMailMessage`? also, i suggest you show us that method, in order to see what you're doing. – Yuval Itzchakov Aug 10 '14 at 17:49

1 Answers1

1

You don't need that MailDefinition instance. See the example on msdn http://msdn.microsoft.com/en-us/library/5k0ddab0(v=vs.110).aspx

public static void CreateTimeoutTestMessage(string server)
    {
        string to = "jane@contoso.com";
        string from = "ben@contoso.com";
        string subject = "Using the new SMTP client.";
        string body = @"Using this new feature, you can send an e-mail message from an application very easily.";
        MailMessage message = new MailMessage(from, to, subject, body);
        SmtpClient client = new SmtpClient(server);
        Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
        client.Timeout = 100;
        // Credentials are necessary if the server requires the client  
        // to authenticate before it will send e-mail on the client's behalf.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

  try {
          client.Send(message);
        }  
        catch (Exception ex) {
          Console.WriteLine("Exception caught in CreateTimeoutTestMessage(): {0}", 
                ex.ToString() );              
      }
    }
Ian
  • 33,605
  • 26
  • 118
  • 198