c# - 通过 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;
}
}
对此的任何帮助将不胜感激。
解决方案
推荐阅读
- laravel-5.8 - htmlspecialchars() 期望参数 1 是字符串,数组给定 laravel 5.8
- regex - 用于替换新行的正则表达式
- python-3.x - python return语句的主要问题
- python - 如何使用 Python 获取 Word 文档的修订号?
- npm - 尝试在 Windows 10 中创建 package.json 文件时出错
- java - 使用结果集将大数据从数据库导出到 excel
- google-cloud-platform - 在 Google Cloud SQL 上创建只读副本时出错
- javascript - 我需要在 HTML 表单的隐藏字段中插入 cookie 值
- c# - 检测到布局周期。布局无法完成。检测到布局周期。布局无法完成
- python - Game of Life - 如何通过鼠标点击激活单元格?