在c#中使用outlook发送带有附件的邮件

本文关键字:outlook | 更新日期: 2023-09-27 18:01:29

当我试图发送电子邮件给不同的收件人与他们各自的附件时,我得到错误。我正在使用循环更改收件人和附件,但得到错误。请帮忙解决这个问题我的代码是

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;
                }

在c#中使用outlook发送带有附件的邮件

添加这个—— 添加参考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

尝试添加Outlook Inspector如下所述:https://groups.google.com/forum/!味精/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;
    // ...
}