当我尝试使用各自的附件向不同的收件人发送电子邮件时出现错误。我使用循环来更改收件人和附件但收到错误。请帮助解决此问题 我的代码是
private void BtnEmail_Click(object sender, EventArgs e)
{
try
{
string[] fileEntries = System.IO.Directory.GetFiles(txtPdfFiles.Text, "*.pdf");
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
if (RtxtBox.Text == "")
{
MessageBox.Show("Please set Mail body text");
GrpMailBody.Visible = true;
}
else
{
oMsg.HTMLBody = RtxtBox.Text;
}
if (RtxtSubject.Text == "")
{
MessageBox.Show("Please Enter Mail Subject");
GrpMailBody.Visible = true;
}
else
{
oMsg.Subject = RtxtSubject.Text;
}
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
for (int i = 0; i <= grvExcelData.RowCount; i++)
{
string EmaildID = grvExcelData.Rows[i].Cells[3].Value.ToString();
string sFileName = grvExcelData.Rows[i].Cells[5].Value.ToString()+".pdf";
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(EmaildID);
foreach (string fileName in fileEntries)
{
string fileN = "";
string xfileName;
xfileName=System.IO.Path.GetFileName(fileName);
if (xfileName == sFileName)
{
Outlook.Attachment oAttach = oMsg.Attachments.Add(@fileName, iAttachType, iPosition, sDisplayName); //getting error in this line
}
else
{
}
}
oRecip.Resolve();
oMsg.Send();
oRecip = null;
//oRecips = null;
oMsg = null;
oApp = null;
}
答案 0 :(得分:0)
尝试按此处所述添加Outlook Inspector: https://groups.google.com/forum/#!msg/microsoft.public.outlook.program_vba/lLJwbwwl-XU/gRuQYRpJtxEJ
using Outlook = Microsoft.Office.Interop.Outlook;
try
{
string[] fileEntries = System.IO.Directory.GetFiles(txtPdfFiles.Text, "*.pdf");
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMsg.GetInspector;
// ...
}
答案 1 :(得分:0)
添加此内容 添加参考Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
public void sendEMailThroughOUTLOOK()
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.HTMLBody = "Hello!!";
//Add an attachment.
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//now attached the file
Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
//Subject line
oMsg.Subject = "Your Subject will go here.";
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("EmailAddress");
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
}//end of catch
}//end of Email Method