2016-03-03 45 views
-2

我有了這個代碼,發送電子郵件帶有附件[S]:我該如何靜靜發送Outlook電子郵件?

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 = "[email protected]"; 
    string TO_EMAIL = "[email protected]"; 
    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(保持我的密碼私密)的好處?

+1

編譯代碼,cs會被編譯成一個dll。密碼是安全的。 – Kramb

+1

請勿直接使用Outlook,請使用指向Outlook服務器的SendMail。 –

+0

@JohnPeters:怎麼樣? –

回答

4

如果你想在最短的方法:

System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address"; 
System.Web.Mail.SmtpMail.Send("from","To","Subject","MessageText"); 
+0

該代碼如何與Outlook中的MailItem一起使用。 SMTP服務器是否需要憑證。所以如果你不提供證書,你將無法通過smtp服務器對象發送。那麼你的代碼如何工作? –

+0

這取決於Outlook的配置,默認情況下它使用IMAP協議而不是SMTP。但是,如果它支持SMTP,那麼是的,您必須先發送證書才能達到上述要求。 –

-1

這是我從其他項目中,我發送對話框顯示重用代碼,併爲電子郵件只待時發送用戶點擊「發送」按鈕。出於這個原因,它不叫「送」

爲了讓電子郵件發送默默地/無人值守的,我只需要添加一個調用「mailItem.Send()」,例如:

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