来自控制台应用程序的类的MailMessage

时间:2014-08-10 17:24:54

标签: c#

我正在整个程序中使用共享资源,而我正在创建一个循环并进行更新的新工具。我有很多地方可以从一个发送电子邮件的控件中调用一个类,其余的代码都是写的,所以我真的不想改变它。但是,当从控制台应用程序调用类时,我无法确定要传递给CreateMailMessage以替换System.web.ui.control的内容(从.net Web应用程序正常工作)

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

1 个答案:

答案 0 :(得分:1)

您不需要该MailDefinition实例。请参阅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() );              
      }
    }