邮件正文中的附加字符串未使用SMTPClient()和StringBuilder()通过电子邮件发送

时间:2018-11-08 16:34:54

标签: c# .net email stringbuilder smtpclient

首先,我对C#还是很陌生,并且可以肯定我的大部分代码都可以清除,因此除非您也提供有关我的问题的帮助,否则请不要提出建议。

我正在通过SmtpClient()发送电子邮件。我正在尝试使用从循环中的函数返回的字符串构建电子邮件的正文。我的问题是,琴身的琴弦没有建立起我认为应该的样子。

当前,我正在创建一个新的StringBuilder(),其中包含一些默认文本。然后,我运行一些功能,并尝试通过StringBuilder()将结果添加到StringBuilder.AppendLine()对象中。

这是我的一些代码:     

// Setup SMTP Client for sending mail updates
//-----------------------------------
String from_addr_text = "<removed>";
String to_addr_text = "<removed>";
String msg_subject = "Updates to USPTO searches";
StringBuilder msg_body = new StringBuilder("The following searches have received updated results:" + Environment.NewLine);
SmtpClient AlertMail = new SmtpClient
{
    Port = 587,
    Host = "<removed>",
    EnableSsl = true,
    Timeout = 10000,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new System.Net.NetworkCredential("<removed>", "<removed>")
};
MailMessage update = new MailMessage(from_addr_text, to_addr_text, msg_subject, msg_body.ToString())
{
    BodyEncoding = UTF8Encoding.UTF8,
    IsBodyHtml = false,
    DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
};

            ...

// Process data from api for Assignor
//-----------------------------------
bool isUpdated = false;
foreach (String url in searchesByAssignorUSPTO)
{
    try
    {
        String longName = <removed>); 
        String name = <removed>;
        String thisHash = await GetSearchData(url, name, "Assignor");
        if (DoesHashExist(thisHash))
        {
            Debug.WriteLine(thisHash + " already exists. No update to " + name + " search.");
        }
        else
        {
        Debug.WriteLine(thisHash + " does not exist. There is an update to " + name + " search.");
        isUpdated = true;
        msg_body.AppendLine(name + " as " + "Assignor" + Environment.NewLine);
        }
    }
    catch
    {
        Console.WriteLine("something is broken with ASSIGNOR search dummy!");
    }
}
// Process data from api for Assignee
foreach (String url in searchesByAssigneeUSPTO)
{
    try
    {
        String longName = <removed>;
        String name = <removed>;
        String thisHash = await GetSearchData(url, name, "Assignee");
        if (DoesHashExist(thisHash))
        {
            Debug.WriteLine(thisHash + " already exists. No update to " + name + " search.");
        }
        else 
        {
            Debug.WriteLine(thisHash + " does not exist. There is an update to " + name + " search.");
            isUpdated = true;
            msg_body.AppendLine(name + " as " + "Assignee" + Environment.NewLine);
        }
    }
    catch
    {
        Console.WriteLine("something is broken with ASSIGNEE search dummy!");
    }
}
// Send email is search results are updated
if (isUpdated)
{
    AlertMail.Send(update);
    Debug.WriteLine(msg_body.ToString());
}

程序运行并且循环返回结果时,msg_body将正确打印到输出窗口,但是,在收到电子邮件时,正文仅是:“以下搜索已收到更新的结果:”

我尝试过:

  • isBodyHtml的值更改为true并使用<br /> 而不是Environment.NewLine
  • 在字符串结尾处添加\n,并删除Environment.NewLine
  • msg_body更改为键入String,然后使用msg_body将结果串联到=+
  • 使用Append()方法而不是AppendLine()

这是输出窗口的片段: Output window showing strings appended to SB

2 个答案:

答案 0 :(得分:1)

请务必注意代码中变量的分配。当您将msg_body分配给update MailMessage对象时,它只输入在电子邮件中返回的提到的一行,并且不包含API生成的信息。

尝试将SmtpClientMailMessage变量的初始化位置移到if (isUpdated)块的正前方,您应该会做得很好。

答案 1 :(得分:0)

每个@ tym32167,我需要在循环和函数完成后将MailMessage()的实例化移动到。我在调用AppendLines()方法之前创建了对象,因此没有包含它们。