首页 > 解决方案 > 通过 Outlook 发送电子邮件

问题描述

我正在使用 C# 通过 Outlook 发送电子邮件(不幸的是,我没有可以用来发送电子邮件的 SMTP 服务器,也无法通过 SQL Server 电子邮件发送,因为我们不是开发团队,所以公司没有允许我们使用这些,这是我能想到的绕过这些限制的唯一方法)。

从技术上讲,代码可以工作,但是当它运行时,会出现一个消息框见截图(见屏幕截图),指出“程序正在尝试代表您发送电子邮件。如果这是意外的,请单击拒绝并验证您的防病毒软件是否已启动-迄今为止。” 我可以在代码中做些什么来抑制此消息吗?

我按照屏幕截图的说明单击了“帮助”按钮并按照说明操作(https://support.microsoft.com/en-us/office/i-get-warnings-about-a-program-accessing-email-地址-信息-或-发送-电子邮件-on-my-behalf-86cc5ece-379e-45e3-b8eb-3fefba09946b?ns=outlook&version=90&syslcid=1033&uilcid=1033&appver=zol900&helpid=olmain11.chm553714172&ui=en-us&rs=en-us&ad= us ) 但我无法更改帮助文章中提到的 Programmatic Access 部分。

这是我正在使用的代码(如果我单击弹出窗口上的允许按钮,它在技术上有效):

public static bool SendMailWithOutlook(string subject, string htmlBody, string recipients, string[] 
     filePaths, MailSendType sendType)
        {
            try
            {
                // create the outlook application.
                Outlook.Application outlookApp = new Outlook.Application();
                if (outlookApp == null)
                    return false;

                // create a new mail item.
                Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                // set html body. 
                // add the body of the email
                mail.HTMLBody = htmlBody;

                //Add attachments.
                if (filePaths != null)
                {
                    foreach (string file in filePaths)
                    {
                        //attach the file
                        Outlook.Attachment oAttach = mail.Attachments.Add(file);
                    }
                }

                mail.Subject = subject;
                mail.To = recipients;

                if (sendType == MailSendType.SendDirect)
                    mail.Send();
                else if (sendType == MailSendType.ShowModal)
                    mail.Display(true);
                else if (sendType == MailSendType.ShowModeless)
                    mail.Display(false);

                mail = null;
                outlookApp = null;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

对此的任何帮助将不胜感激。

标签: c#outlookemail

解决方案


推荐阅读