如何以静默方式发送 Outlook 电子邮件
本文关键字:Outlook 电子邮件 方式发 静默 | 更新日期: 2023-09-27 18:37:16
我有这个代码,它发送一封带有附件的电子邮件:
internal static bool EmailGeneratedReport(List<string> recipients)
{
bool success = true;
try
{
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
Recipients _recipients = mailItem.Recipients;
foreach (string recip in recipients)
{
Recipient outlookRecipient = _recipients.Add(recip);
outlookRecipient.Type = (int)OlMailRecipientType.olTo;
outlookRecipient.Resolve();
}
mailItem.Subject = String.Format("Platypus Reports generated {0}", GetYYYYMMDDHHMM());
List<String> htmlBody = new List<string>
{
"<html><body><img src='"http://www.platypus.com/wp-content/themes/duckbill/images/pa_logo_notag.png'" alt='"Pro*Act logo'" ><p>Your Platypus reports are attached.</p>"
};
htmlBody.Add("</body></html>");
mailItem.HTMLBody = string.Join(Environment.NewLine, htmlBody.ToArray());
. . . // un-Outlook-specific code elided for brevity
FileInfo[] rptsToEmail = GetLastReportsGenerated(uniqueFolder);
foreach (var file in rptsToEmail)
{
String fullFilename = Path.Combine(uniqueFolder, file.Name);
if (!File.Exists(fullFilename)) continue;
if (!file.Name.Contains(PROCESSED_FILE_APPENDAGE))
{
mailItem.Attachments.Add(fullFilename);
}
MarkFileAsSent(fullFilename);
}
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
}
catch (System.Exception ex)
{
String exDetail = String.Format(ExceptionFormatString, ex.Message,
Environment.NewLine, ex.Source, ex.StackTrace, ex.InnerException);
MessageBox.Show(exDetail);
success = false;
}
return success;
}
但是,它会在准备就绪时弹出电子邮件窗口,用户必须通过发送或取消来响应该窗口。由于这是在基于生成要发送的报告的计时器发送电子邮件的应用程序中,因此我不能依靠在场的人点击"发送"按钮。
Outlook 电子邮件可以"静默"发送吗?如果是这样,如何?
我可以使用 gmail 静默发送电子邮件:
private void EmailMessage(string msg)
{
string FROM_EMAIL = "sharedhearts@gmail.com";
string TO_EMAIL = "cshannon@platypus.com";
string FROM_EMAIL_NAME = "B. Clay Shannon";
string TO_EMAIL_NAME = "Clay Shannon";
string GMAIL_PASSWORD = "theRainNSpainFallsMainlyOnDonQuixotesHelmet";
var fromAddress = new MailAddress(FROM_EMAIL, FROM_EMAIL_NAME);
var toAddress = new MailAddress(TO_EMAIL, TO_EMAIL_NAME);
string fromPassword = GMAIL_PASSWORD;
string subject = string.Format("Log msg from ReportScheduler app sent
{0}", DateTime.Now.ToLongDateString());
string body = msg;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
。但是当我这样做时,我必须提供我的Gmail密码,而我真的不想这样做(在源代码中公开我的密码)。
那么,我怎样才能获得gmail(静音)和Outlook(保持密码私密)的好处呢?
如果你想要最短的路:
System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText");
这是我从另一个项目中重用的代码,我希望显示发送对话框,并且仅在用户点击"发送"按钮时才发送电子邮件。出于这个原因,它没有调用"发送"
为了让电子邮件静默/无人值守,我只需要添加一个对"mailItem.Send()"的调用,如下所示:
mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
mailItem.Send(); // This was missing