我如何静默发送Outlook电子邮件?

时间:2016-03-03 00:16:51

标签: c# email outlook silent unattended-processing

我已经获得了发送附件[s]的电子邮件的代码:

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

但是,它会在准备好时弹出电子邮件窗口,用户必须通过发送或取消来响应。由于这是一个应用程序,它根据生成报告的计时器发送电子邮件,我不能依靠在场的人来点击发送&#34;发送&#34;按钮。

可以发送Outlook电子邮件&#34;静默&#34;?如果是这样,怎么样?

我可以使用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密码,而我真的不想这样做(在源代码中公开我的密码)。

那么,我怎样才能获得gmailing(沉默)和Outlook(保密我的密码)的好处?

2 个答案:

答案 0 :(得分:4)

如果您想要最短的方式:

with tf.name_scope(some_unique_name):
  ...
  _ = tf.histogram_summary('weights', kernel_weights)

答案 1 :(得分:-1)

这是我从另一个项目中重复使用的代码,其中我想要显示发送对话框,并且只有当用户点击“发送”按钮时才会发送电子邮件。出于这个原因,它没有调用“发送”

要让电子邮件无声地/无人值守地发送,我只需要像这样添加对“mailItem.Send()”的调用:

mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
mailItem.Send(); // This was missing