从控制台应用程序调用的类的邮件消息
本文关键字:消息 应用程序 调用 控制台 | 更新日期: 2023-09-27 18:17:16
我在整个程序中使用共享资源,我正在制作一个循环并进行更新的新工具。我有很多地方,我从发送电子邮件的控件调用一个类,其余的代码都写好了,所以我不想改变它。然而,我无法找出传递给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);
}
您不需要那个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() );
}
}