C#-更改MailItem的返回路径

时间:2018-09-07 18:15:24

标签: c# outlook smtp return-path

我有一个过程,该过程将坐在自动电子邮件发送机上,以基于平面文件发送电子邮件。该文件提供了收件人,抄送,主题,正文,附件和发件人。发送电子邮件后,我希望所有失败的电子邮件或退回邮件都发送给发件人,而不是自动化服务。我尝试了两种有效的解决方案...部分地。

首先使用SMTP,我可以设置“返回路径”和“发件人”地址,并获得退回的电子邮件以返回发件人。但是,公司政策是我不能对用户ID和/或密码进行硬编码。我尝试使用“ UseDefaultCredentials”标志没有运气。我们的AD登录名和Exchange登录名是不同的,密码是相同的。 这是代码:

    private void SendEmail(EmailProperties pEmail)
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.Headers.Add("Return-Path", AddressLookup(pEmail.Sender));
            foreach (string strTo in pEmail.To)
            {
                mail.To.Add(AddressLookup(strTo));
            }
            foreach (string strCC in pEmail.CC)
            {
                mail.CC.Add(AddressLookup(strCC));
            }
            mail.Subject = pEmail.Subject;
            mail.Body = pEmail.Body;
            mail.From = new MailAddress(AddressLookup(pEmail.Sender));
            //mail.ReplyToList.Add(AddressLookup(pEmail.ReplyTo));
            SmtpClient smtpClient = new SmtpClient("server", 25);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = true;
            //smtpClient.Credentials = new NetworkCredential("user", "pw");
            smtpClient.EnableSsl = false;
            smtpClient.Send(mail);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private string AddressLookup(string pstrAddr)
    {
        try
        {
            if (pstrAddr.Contains("@")) return pstrAddr;

            Outlook.Application OutlookApp = new Outlook.Application();
            Outlook.AddressLists addrLists = OutlookApp.Session.AddressLists;
            Outlook.AddressList gal = addrLists["Global Address List"];
            Outlook.AddressEntry entry = gal.AddressEntries[pstrAddr];
            return entry.GetExchangeUser().PrimarySmtpAddress;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

第二: 我已经尝试过使用Outlook Interop,并且可以正常发送电子邮件,但是任何退回邮件都可以使用自动服务。这是代码:

    private void SendEmail(EmailProperties pEmail)
    {
        try
        {
            Outlook.Application OutlookApp = new Outlook.Application();
            Outlook.MailItem mail = OutlookApp.CreateItem(
                Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            Outlook.AddressEntry currentUser = 
            OutlookApp.Session.CurrentUser.AddressEntry;
            if (currentUser.Type == "EX")
            {
                mail.To = pEmail.To;
                mail.CC = pEmail.CC;
                mail.Subject = pEmail.Subject;
                mail.Body = pEmail.Body;
                mail.ReplyRecipients.Add(pEmail.ReplyTo);
                mail.Sender = AddressLookup(pEmail.Sender);
                mail.Recipients.ResolveAll();
                mail.ReplyRecipients.ResolveAll();

                foreach (string e in pEmail.Attachments)
                {
                    mail.Attachments.Add(e, Outlook.OlAttachmentType.olByValue,
                        Type.Missing, Type.Missing);
                }
                mail.Send();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

有什么我想念的东西吗?我宁愿使用互操作性发送电子邮件,而只是更改电子邮件标题。如果有更好的方法,我欢迎您提出建议。

谢谢!

0 个答案:

没有答案