使用outlook以c#格式发送带有附件的邮件

使用outlook以c#格式发送带有附件的邮件,c#,.net,email,outlook,C#,.net,Email,Outlook,当我试图向不同的收件人发送带有各自附件的电子邮件时,出现错误。我正在使用循环更改收件人和附件,但出现错误。请帮助解决这个问题 我的代码是 private void BtnEmail_Click(object sender, EventArgs e) { try { string[] fileEntries = System.IO.Directory.GetFiles(txtPdfFiles.Text,

当我试图向不同的收件人发送带有各自附件的电子邮件时,出现错误。我正在使用循环更改收件人和附件,但出现错误。请帮助解决这个问题 我的代码是

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;

                }
private void BtnEmail\u单击(对象发送者,事件参数e)
{
尝试
{
string[]fileEntries=System.IO.Directory.GetFiles(txtpffiles.Text,“*.pdf”);
Outlook.Application oApp=新建Outlook.Application();
Outlook.MailItem oMsg=(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
如果(RtxtBox.Text==“”)
{
MessageBox.Show(“请设置邮件正文文本”);
GrpMailBody.Visible=true;
}
其他的
{
oMsg.HTMLBody=RtxtBox.Text;
}
如果(RtxtSubject.Text==“”)
{
MessageBox.Show(“请输入邮件主题”);
GrpMailBody.Visible=true;
}
其他的
{
oMsg.Subject=RtxtSubject.Text;
}
字符串sDisplayName=“MyAttachment”;
int i位置=(int)oMsg.Body.Length+1;
int iAttachType=(int)Outlook.OlAttachmentType.olByValue;
Outlook.Recipients oRecips=(Outlook.Recipients)oMsg.Recipients;

对于(int i=0;i请尝试添加Outlook Inspector,如下所述:

添加此-- 添加引用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

错误消息是什么?它给出了错误{“对象引用未设置为对象的实例。”}以及代码的哪行引发了该错误?
 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