2014-03-03 35 views
0

您好我知道如何從smpt通過下面的mailcode發送郵件。但不知道如何從用戶的outlook.so發送郵件,用戶可以在他的郵件中找到他的郵件文件夾 請幫助我..如何從用戶的Outlook郵箱發送郵件

下面是web配置代碼發送郵件

  <mailSettings> 
     <smtp> 
      <network host="11.111.111.1" port="25" defaultCredentials="true"/> 
     </smtp> 
    </mailSettings> 

,這是我發送郵件的方法:

 public static void SendMessage(string sbj, string bd, string bccadd, string attachFile1,string buyeremail) 
    { 
     MailMessage message = new MailMessage(); 
     SmtpClient client = new SmtpClient(); 
     message.From = new MailAddress(buyeremail); 
     message.To.Add(new MailAddress(bccadd)); 
     message.Subject = sbj.Trim(); 
     message.Body = bd.Trim(); 
     SmtpClient mysmptclient = new SmtpClient(); 
     mysmptclient.DeliveryMethod = SmtpDeliveryMethod.Network; 


      message.Attachments.Add(new Attachment(attachFile1)); 
      try 
      { 
       message.Attachments.Add(new Attachment(attachFile5)); 
      } 
      catch 
      { 
      } 
       mysmptclient.Send(message); 

    } 

我剛剛修改我的代碼如下:

 try 
     { 


      Outlook.Application oApp = new Outlook.Application(); 
      Outlook.NameSpace oNamespace = new Outlook.NameSpace("MAPI"); 
      Outlook.MailItem oMailItem = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      oMailItem.HTMLBody = bd.Trim(); 

      oMailItem.Subject = sbj.Trim(); 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients; 
      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(bccadd); 
      oRecip.Resolve(); 
      oMailItem.Send(); 
      oRecip = null; 
      oRecips = null; 
      oMailItem = null; 
      oApp = null; 
     } 
     catch (Exception ex) 
     { 
      Response.Write("<script>alert('" + ex.Message + "');</script>"); 
      //string script = "<script>alert('" + ex.Message + "');</script>"; 

     } 

卜現在它顯示錯誤: 檢索COM類工廠CLSID組件{0006F03A-0000-0000-C000-000000000046}失敗,原因是以下錯誤:80070005次訪問被拒絕。 (來自HRESULT的異常:0x80070005(E_ACCESSDENIED))。 請幫助我

+0

我瘦g我能想到的是BCC到預期的「發件人」,並且該用戶有一個Outlook規則將這些郵件移動到已發送郵件。我很想看看其他人提出了什麼,但 – jdphenix

回答

3

您可以使用Microsoft Exchange Web服務(EWS)託管API來創建和發送電子郵件。 MSDN上顯示

http://msdn.microsoft.com/en-us/library/office/dd633628(v=exchg.80).aspx

代碼:

// Create an email message and identify the Exchange service. 
EmailMessage message = new EmailMessage(service); 

// Add properties to the email message. 
message.Subject = "Interesting"; 
message.Body = "The merger is finalized."; 
message.ToRecipients.Add("[email protected]oso.com"); 

// Send the email message and save a copy. 
message.SendAndSaveCopy(); 
-1

我相信你需要使用Outlook的API來執行,由於MailObject和SMTP將在內部使用上述PARAMS發送郵件,這樣的事情應該幫助http://www.codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr

可能重複:Can only send email via Outlook if Outlook is open

+0

運行服務器進程時使用interop,如ASP.NET,強烈建議微軟不鼓勵。 –

+0

@PatrickHofman:不知道MS爲什麼要這麼做,我的代碼很少,幾乎現在運行良好,請記住它的桌面應用程序。 – Vishal

+1

問題中的標籤顯示ASP.NET。請參閱[this](http://support.microsoft.com/kb/257757)爲什麼不使用interop。 –

相關問題