Outlook 正文的迭代数组

本文关键字:数组 迭代 正文 Outlook | 更新日期: 2023-09-27 18:37:13

这是我尝试使用的代码。但它实际上并没有像我想要的那样迭代数组以填充主体。 我想做的是使用 C# 创建 Outlook 电子邮件,并填充收件人、邮件主题,然后根据数组中包含的内容生成电子邮件的正文。 编辑 - 我移动了 for each 循环以尝试用数组的每个元素填充主体,但我收到无法使用此代码将 int 转换为字符串的编译错误。

      public static string GenerateEmail()
{
    try
    {
        for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++)
        {
            return Global.Variables.GlobalVariables.eName[q];
            Outlook.Application oApp = new Outlook.Application();
            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
            for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++)
            {
                oMsg.HTMLBody = q;
            }
            oMsg.Subject = "Reports Are Ready";
            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
            Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("123123123@testemail.com");
            oRecip.Resolve();
            oMsg.Save();
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;
        }
    }
    catch 
    {
    }
    return null;
    }           
}

Outlook 正文的迭代数组

问题似乎是for循环中的第一行是 return 语句,这会导致函数立即中止。

如果要填充邮件正文,而不是为每个交互创建一封邮件,请将实际电子邮件的声明移到循环之外。然后在循环中将内容附加到消息中:

Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
string content = string.Empty;
for (int q = eName.GetLowerBound(0); q <= eName.GetUpperBound(0); q++)
{
    content += "...";
}
oMsg.HTMLBody = content;
// additional settings