如何发送自动电子邮件,如时事通讯

时间:2013-05-08 16:41:56

标签: c#-3.0 windows-applications

我看到管理员用类似的问题关闭了一些问题。 我将尝试详细解释我正在寻找的解决方案:

首先,我正在尝试开发一个Windows控制台应用程序。

发送频率:星期一到星期五

有多少用户:从5到20

数据格式: 一个表,其中行是国家和列,不同类型的产品和单元格表示该国家/地区产品的销售

限制: 服务用户不希望使用pdf或excel或任何类型的附件应该是可以在邮件正文中显示的html格式

目前我创建的报告几乎是手工艺人,如:

.....
var myStringBuilder = new StringBuilder("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
myStringBuilder.Append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
myStringBuilder.Append("</head><body>");
myStringBuilder.AppendLine();
myStringBuilder.Append("<table style=\"font-family: Calibri; text-align: right; font-size: x-small;\">");
myStringBuilder.AppendLine();
var header = new StringBuilder("<tr><th>a</th><th>CATEG1</th><th>CATEG2</th><th>CATEG3</th><th>CATEG4</th><th>TOTAL</th></tr>");
var line = new StringBuilder("<tr><td>a</td><td>b</td><td>c</td><td>z</td><td>e</td><td>f</td></tr>");
foreach (var a in _actuals)
        {
            line.Replace("a", a.CountryName);
            if(a.Segment.ToLowerInvariant().Equals("categ1")) {
               line.Replace("b", "[" + string.Format("{0:0,#}", a.LsvLcFact / 1000) + "]"); }
            else if (a.Segment.ToLowerInvariant().Equals("categ2")) {
               line.Replace("c", "[" + string.Format("{0:0,#}", a.LsvLcFact / 1000) + "]"); }
            else if (a.Segment.ToLowerInvariant().Equals("categ3")) {
               line.Replace("z", "[" + string.Format("{0:0,#}", a.LsvLcFact / 1000) + "]"); } 
            else {
               line.Replace("e", "[" + string.Format("{0:0,#}", a.LsvLcFact / 1000) + "]"); }
          }
 .....

在课堂上发送类似的邮件:

    public void SendResumen(string subject ,StringBuilder content)
    {
        var oMsg = new MailMessage...;
        ... add users
        oMsg.Subject = subject;
        var mimeType = new ContentType("text/html");
        var alternate = AlternateView.CreateAlternateViewFromString(content.ToString(), mimeType);
        oMsg.AlternateViews.Add(alternate);
        oMsg.IsBodyHtml = true;
        var cli = Client();
        cli.Send(oMsg);
    }

最后,我希望你能理解我在做什么,问题是: 是否有一个工具可以用来生成消息体?

2 个答案:

答案 0 :(得分:0)

我有一个VB应用程序可以发送具有HTML有效负载的消息,并且它不使用AlternateView功能。

Dim mail As New MailMessage(fromUser.Email, toUser.Email)
mail.Subject = subject
mail.IsBodyHtml = True
mail.Body = message

虽然对那些无法读取HTML有效负载的电子邮件客户端使用AlternateView功能会很好,但是您的代码无法真正使用它,因为您只提供HTML消息。如果你确实设法让AlternateView功能在这里工作,任何无法读取HTML的客户端都会看到HTML标记,而不仅仅是简单的消息文本。为了真正发挥作用,我认为您需要同时提供文本和HTML电子邮件正文。

答案 1 :(得分:0)

我接受来自@ Jim-Mischel的建议