2015-06-22 48 views
0
public void sendEMailThroughOUTLOOK() 
{ 
     if(MessageBox.Show(PublicFunction.L_Get_Msg("msg",28),"MessageBox",MessageBoxButtons.OKCancel)== DialogResult.OK) 
     { 
      try 
      { 
       // Create the Outlook application. 
       Outlook.Application oApp = new Outlook.Application(); 
       //set default account send mail 
       Outlook.Account account = GetAccountForEmailAddress(oApp,"[email protected]"); 
       // Create a new mail item. 
       Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
       // Set HTMLBody. 
       //oMsg.HTMLBody= "<html> <p style=font size:200%>"+PublicFunction.L_Get_Msg("msg",29)+"<br/>"+ PublicFunction.L_Get_Msg("msg",30)+"</p> </html>"; 
       oMsg.HTMLBody= txtSubject.Text; 
       if(txtSubject.Text.Length < 1) 
       { 
        MessageBox.Show(PublicFunction.L_Get_Msg("msg",29)); 
        return; 
       } 
       //Add an attachment. 
       String sDisplayName = "MyAttachment"; 
       int iPosition = (int)oMsg.Body.Length + 1; 
       int iAttachType = (int)Outlook.OlAttachmentType.olByValue; 
       //now attached the file 
       string fileName = Application.StartupPath + @"\\Reports\\RPTTienDoCongViec.xls"; 
       Outlook.Attachment oAttach = oMsg.Attachments.Add(fileName, iAttachType, iPosition, sDisplayName); 
       oMsg.Subject = PublicFunction.L_Get_Msg("msg",34)+ DateTime.Now.ToString("yyyy-MM-dd"); 
       // Add a recipient      
       Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
       char [] catkytu = {','}; 
       string ThoaiSendEmail =""; 
       string [] mailsend = txtEmail.Text.Split(catkytu); 

       for(int Thoaiitem = 0; Thoaiitem < mailsend.Length;Thoaiitem++) 
       { 
        ThoaiSendEmail = mailsend[Thoaiitem].ToString(); 

        foreach (string recipient in new string[] {ThoaiSendEmail}) 
        { 

         Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(recipient); 
         oRecip.Resolve(); 
        } 
       } 
       oMsg.SendUsingAccount = account; 
       oMsg.Send(); 
       oRecips = null; 
       oMsg = null; 
       oApp = null; 
       MessageBox.Show(PublicFunction.L_Get_Msg("msg",32)); 

      } 
      catch 
      { 
       MessageBox.Show(PublicFunction.L_Get_Msg("msg",33)); 
      } 
     } 
     else return; 
    } 

    public void sendEMailMTNLLD() 
    {  
     try 
     { 

      // Create the Outlook application. 
      Outlook.Application oApp = new Outlook.Application(); 
      Outlook.NameSpace NS = oApp.GetNamespace("MAPI"); 
      NS.Logon("[email protected]","pass",true,true); 
      // Create a new mail item.    
      Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      // Set HTMLBody. 
      oMsg.HTMLBody= "<html> <p style=font size:200%>Dear Patrick<br/>"+PublicFunction.L_Get_Msg("msg",30)+"</p> </html>"; 
      //Add an attachment. 
      String sDisplayName = ""; 
      int iPosition = (int)oMsg.Body.Length + 1; 
      int iAttachType = (int)Outlook.OlAttachmentType.olByValue; 
      //now attached the file 
      string fileName = Application.StartupPath + @"\\Reports\\RPTTienDoCongViec.xls"; 
      Outlook.Attachment oAttach = oMsg.Attachments.Add(fileName, iAttachType, iPosition, sDisplayName); 
      oMsg.Subject = "Dear Patrick"; 
      // Add a recipient      
      Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;   
      foreach (string recipient in new string[] {"[email protected]"/*"[email protected]"*/}) 
      {           
       Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(recipient); 
       oRecip.Resolve(); 
      } 

      Outlook.Account account = GetAccountForEmailAddress(oApp,"[email protected]"); 
      oMsg.SendUsingAccount = account; 
      oMsg.Send(); 
      oRecips = null; 
      oMsg = null; 
      oApp = null; 
      NS = null; 

     }//end of try block 
     catch 
     { 
      MessageBox.Show(PublicFunction.L_Get_Msg("msg",33)); 
     } 

    } 

    public static Outlook.Account GetAccountForEmailAddress(Outlook.Application oApp, string smtpAddress) 
    { 
     Outlook.Accounts accounts = oApp.Session.Accounts; 
     foreach (Outlook.Account account in accounts) 
     { 
      if (account.SmtpAddress == smtpAddress) 
      { 
       return account; 
      } 
     } 
     throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
    } 
+0

你有什么样的o帐户? POP3/SMTP?代表Exchange账户?太平洋标准时间? –

回答

0

Outlook对象模型不提供用于创建新帐户的任何方法或属性。您可以添加新的商店,而不是帐户。请参阅名称空间类的AddStoreEx方法,该类将指定格式的个人文件夹文件(.pst)添加到当前配置文件。

要创建新配置文件,您需要使用低级API - 扩展MAPI。整个过程在How to create MAPI profiles without installing Outlook文章中描述。有关更多信息,请参阅IProfAdmin::CreateProfile

相关问题