-2

I've got this code that sends an email with attachment[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;
}

However, it pops up the email window when ready, which the user must respond to by either sending or canceling. As this is in an app that sends email based on a timer generating reports to be sent, I can't rely on a human being present to hit the "Send" button.

Can Outlook email be sent "silently"? If so, how?

I can send email silently with 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);
    }
}

...but when I do that, I have to supply my gmail password, and I don't really want to do that (expose my password in the source code).

So, how can I gain the benefits of gmailing (silence) and Outlook (keeping my password private)?

4

2 回答 2

4

如果你想要最短的方式:

System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText");
于 2016-03-03T00:36:30.053 回答
0

这是我从另一个项目中重用的代码,我希望在该项目中显示发送对话框,并且仅在用户点击“发送”按钮时发送电子邮件。因此,它没有调用“发送”

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

mailItem.Importance = OlImportance.olImportanceHigh;
mailItem.Display(false);
mailItem.Send(); // This was missing
于 2016-03-03T16:21:18.470 回答