【发布时间】:2026-01-03 19:30:01
【问题描述】:
我一直在寻找解决这个错误的方法。在 VS2015 的调试和发布模式下,我没有收到任何错误,并且代码会生成所需的结果。一旦程序被编译和安装,当按下电子邮件按钮生成代码时,应用程序就像它开始工作一样,然后在大约 20 秒后给出错误“长度不能小于零参数名称:长度”但是应用程序继续,但未完成相关代码。
我已禁用并尝试引用长度的一行代码,这没有任何区别。
我对可能导致这种情况的原因有限,所以我正在寻求帮助以在大海捞针。
下面是有问题的代码:
try
{
// Create the Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
// Create a new mail item.
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.HTMLBody = DictMailParam["Body"].ToString();
//Add an attachment.
String sDisplayName = DictMailParam["AttachmentName"].ToString();
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
//now attached the file
//Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add
// (@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
foreach (string strFile in DictMailParam["Attachments"].ToString().Split(',').ToList())
{
Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add
(strFile, iAttachType, iPosition, sDisplayName);
}
//Subject line
oMsg.Subject = DictMailParam["Subject"].ToString();
// Add a recipient.
Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
Microsoft.Office.Interop.Outlook.Recipient oRecip = null;
// Change the recipient in the next line if necessary.
foreach (var MailId in DictMailParam["ToAddress"].ToString().Split(';').ToArray())
{
if (!string.IsNullOrEmpty(MailId))
{
oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(MailId);//DictMailParam["ToAddress"].ToString()
oRecip.Resolve();
// Send.
}
}
oMsg.Send();
MessageBox.Show("Purchase Order has been sent to your email." + "\n" + "Please check your mail.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}//end of catch
}//end of Email Method
【问题讨论】:
-
哪一行会抛出异常?
-
尝试从异常中写出你的堆栈跟踪。我看不出有任何明显错误,但堆栈跟踪会提供更多信息。
-
正如 Ben 所说,也写出你的堆栈跟踪(
ex.ToString(),而不是ex.Message)。 -
大家好,我都试过了,但它仍然给我同样的错误,没有额外的信息。关于下一步尝试解决此问题的任何想法?