-2

这是我正在尝试使用的代码。但它实际上并没有按照我的意愿迭代数组来填充身体。我想要做的是使用 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;
    }           
}
4

1 回答 1

2

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

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

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
于 2013-10-29T12:46:32.990 回答