C# 如何从Windows服务发送电子邮件?

C# 如何从Windows服务发送电子邮件?,c#,.net,email,windows-services,smtp,C#,.net,Email,Windows Services,Smtp,我需要按计划发送大量电子邮件(可能一天发送数百封)。我想这样做的方式如下,但问题是我的身体区域会变得很大,如果我把它作为一个字符串加起来,它会变得丑陋 SmtpClient client = new SmtpClient(); //host and port picked from web.config client.EnableSsl = true; MailMessage message = new MailMessage(); message.Body = "test fro

我需要按计划发送大量电子邮件(可能一天发送数百封)。我想这样做的方式如下,但问题是我的身体区域会变得很大,如果我把它作为一个字符串加起来,它会变得丑陋

SmtpClient client = new SmtpClient(); //host and port picked from web.config
client.EnableSsl = true;

MailMessage message = new MailMessage();

message.Body = "test from winservice"; // HERE IS MY PROBLEM
message.IsBodyHtml = true;
message.From = new MailAddress("donotreply@abcde.com");
message.Subject = "My subject";
message.To.Add(new MailAddress("piniusha@abcde.com"));
try
{
    client.Send(message);
}
catch (Exception)
{
   
}
当我必须从aspx页面执行此操作时,我使用:

MailDefinition message = new MailDefinition();  
        
message.BodyFileName = @"~\EmailTemplate\Template1.htm";
ListDictionary replacements = new ListDictionary();
replacements.Add("<% Name %>", this.txtName.Text);
replacements.Add("<% PhoneOrEmail %>", this.txtPhoneOrEmail.Text);
replacements.Add("<% Message %>", this.txtMessage.Text);
MailMessage msgHtml = message.CreateMailMessage(RECIPIENTS, replacements, new LiteralControl());
MailDefinition message=newmaildefinition();
message.BodyFileName=@“~\EmailTemplate\Template1.htm”;
ListDictionary replacements=新建ListDictionary();
replacements.Add(“,this.txtName.Text”);
replacements.Add(“,this.txtPhoneOrEmail.Text”);
replacements.Add(“,this.txtMessage.Text”);
MailMessage msgHtml=message.CreateMailMessage(收件人、替换、新LiteralControl());
我认为这是一个优雅的解决方案,但我不想引用
System.Web.UI.WebControls.MailDefinition
,因为我使用的是Winservice

  • 如何从Winservice发送批量电子邮件
  • 可以从Gmail帐户发送电子邮件吗?或者他们会在一段时间后阻止我

  • 一个简单的答案是:

    message.Body = File.ReadAllText(@"~\EmailTemplate\Template1.htm");
    
    我不确定您的具体观点,但我相信其他人会回答这些问题:)

    使用“messageBody.Replace”(“,curr.Name”)。Replace(…)

    "messageBody".Replace("<% Name %>", curr.Name).Replace(....)...
    至于gMail的sendign,你可以通过SMTP发送,但他们不允许你伪造另一个“发件人”地址,因此收件人会认为它来自gMail地址。

    我会使用一些模板引擎。例如。

    为什么不使用与MailDefinition完全相同的概念?从模板文件加载正文,用另一个列表-邮件合并样式中的文本替换一些标记

    您所做的只是对要与模板合并的信息数据集进行foreach。加载合并数据,循环合并数据,用当前合并记录替换模板中的标记。将消息正文设置为当前生成的消息。将消息附加到消息队列或立即发送,以您选择的为准

    这不是火箭科学。您已经有了创建消息的代码,因此只需加载合并数据并在其中循环即可。我简化了这个概念,并将CSV用于合并数据,并假设没有字段包含任何逗号:

    message.IsBodyHtml = true;
    message.From = new MailAddress("MailSender@MyCompany.com");
    message.Subject = "My bogus email subject";
    
    string[] lines = File.ReadAllLines(@"~\MergeData.csv");
    string originalTemplate = File.ReadAllText(@"~\Template.htm");
    
    foreach(string line in lines)
    {
        /* Split out the merge data */
        string[] mergeData = line.Split(',');
    
        /* Reset the template - to revert changes made in previous loop */
        string currentTemplate = originalTemplate;
    
        /* Replace the merge tokens with actual data */
        currentTemplate = currentTemplate.Replace("[[FullNameToken]]", mergeData[0]); 
        currentTemplate = currentTemplate.Replace("[[FirstNameToken]]", mergeData[1]);
        currentTemplate = currentTemplate.Replace("[[OtherToken]]", mergeData[2]);
    
        /*... other token replacements as necessary.
         * tokens can be specified as necessary using whatever syntax you choose
         * just make sure that there's something denoting the token so you can
         * easily replace it */
    
        /* Transfer the merged template to the message body */
        message.Body = currentTemplate;
    
        /* Clear out the address from the previous loop before adding the current one */
        message.To.Clear();
        message.To.Add(new MailAddress(mergeData[3]));
        client.Send(message);
    }
    

    我相信GMail在24小时内最多只能有500个收件人。然而,我不鼓励使用GMail发送大量电子邮件;这可能违反了他们的TOS。谢谢Ryan,这里有一篇帖子详细解释了这一点,所以我想我将不得不停止使用gmail发送电子邮件:(哇,那个网站让人看不顺眼。我在想有另一种方法代替加载htm作为字符串和替换令牌。但我想没有。谢谢你的回答。@UshaP-加载htm的方法有很多,但并不总是归结为“做某事最聪明的方法是什么?”.通常最简单的方法是最好的。你可以(如其他人所建议的)将htm作为XML文档加载,并使用XPath查询和设置值。我想说,这完全是矫枉过正,对于这种特殊情况来说,太聪明了。也许这是一个很小的问题,但我不知道如何动态使用htm路径。我有一个引用BL类库项目a的windows服务然后,我将我的htm文件.file.ReadAllText放在“C:\Windows\system32\”中。设置app.config文件中的路径,这样您就不会将其硬编码到预先指定的位置。如果它在本地目录中,只需使用文件名,即“MyTemplate.htm”,也可以使用相对位置“\Templates\MyTemplate.htm”或“.\Templates\MyTemplate.htm”或绝对路径,如“C:\MyTemplates\MyTemplate.htm”。您获得C:\Windows\System32\…的原因是因为前缀“~ \”。在表示Windows服务应用程序中的应用程序根.IIRC的ASP.NET应用程序中,它引用System32目录(?)感谢您的回复。这就是我最后要做的:我将复制到输出目录的副本设置为在我的模板文件夹上复制(如果更新)。然后字符串executablePath=Process.GetCurrentProcess().MainModule.FileName;字符串templatePath=string.Format(“{0}\\EmailTemplate\\MatchupsReady.htm”,Path.GetDirectoryName(executablePath));