/** * * This method will permanently delete an email from Outlook, similar to * the use of the shift+delete manually on an email in Outlook. * Surprise, surprise...Microsoft does not have a method to easily * perform a PERMANENT DELETE of an email...you literally have to move * the phish eMail to the Deleted Items folder first, and then delete it * from there. This is the only way, at least since the writing of this * comment, that we can permanently delete an email out of Outlook in * Visual C# * */ private void permanentlyDeleteEmail( Microsoft.Office.Interop.Outlook.MailItem currMail) { Microsoft.Office.Interop.Outlook.Explorer currExplorer = Globals.ThisAddIn.Application.ActiveExplorer(); Microsoft.Office.Interop.Outlook.Store store = currExplorer.CurrentFolder.Store; Microsoft.Office.Interop.Outlook.MAPIFolder deletedItemsFolder = store.GetRootFolder().Folders[DELETED_ITEMS_FOLDER_NAME]; // The move here will retain a reference to the MailItem entity that // is moved to the Deleted items folder...this is good because we // don't need to search for this email in Deleted Items...thank the // Maker! Microsoft.Office.Interop.Outlook.MailItem movedMail = currMail.Move(deletedItemsFolder); // Stupid Microsoft action here...need to change a value to trigger a // Save...otherwise, upcoming Delete will NOT OCCUR!!!! movedMail.Subject = movedMail.Subject + " "; // Need to save it... movedMail.Save(); // Now, permanently delete it! movedMail.Delete(); }
public void read() { Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = outlook.GetNamespace("Mapi"); object _missing = Type.Missing; ns.Logon(_missing, _missing, false, true); Microsoft.Office.Interop.Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); string foldername = inbox.Name; int iMailCount = inbox.Items.Count; TraceService("Inside read method"); for (int iCount = 1; iCount <= iMailCount; iCount++) { Object mail1 = inbox.Items[iCount]; if (((mail1 as Microsoft.Office.Interop.Outlook.MailItem) != null) && ((mail1 as Microsoft.Office.Interop.Outlook.MailItem).UnRead == true)) { TraceService("Inside unread mail"); Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)inbox.Items[iCount]; SqlConnection con; string connection = @"server=CSM-DEV\SQL2008;database=TerexBest;uid=sa;pwd=rimc@123"; TraceService("Connection with database done1"); con = new SqlConnection(connection); SqlCommand cmd; cmd = new SqlCommand(); con.Open(); cmd.Connection = con; TraceService("Connection assigned to sql command"); string subject = mail.Subject.ToString(); TraceService("mail subject written" + subject); string body = mail.Body.ToString(); cmd.Parameters.Add("@subject", SqlDbType.NVarChar).Value = subject; TraceService(subject); //writing subject cmd.Parameters.Add("@body", SqlDbType.NVarChar).Value = body; TraceService(body); //writing subject cmd.Parameters.Add("@recievedtime", SqlDbType.DateTime).Value = mail.ReceivedTime; TraceService(mail.ReceivedTime.ToString()); //writing subject cmd.Parameters.Add("@mailfrom", SqlDbType.NVarChar).Value = mail.SenderEmailAddress; TraceService(mail.SenderEmailAddress); //writing subject TraceService("Before Inventory saved"); cmd.CommandText = "insert into storemail(subject,body,createddatetime,mailfrom,isActive) values(@subject,@body,@recievedtime,@mailfrom,1)"; TraceService("Inventory saved"); try { cmd.ExecuteNonQuery(); mail.Delete(); iMailCount = iMailCount - 1; } catch (SqlException ex) { ex.ToString(); } con.Close(); cmd.Dispose(); con.Dispose(); } } GC.Collect(); inbox = null; ns = null; outlook = null; }
public void CreateNewDistributionList(string[] tabAdresse) { Microsoft.Office.Interop.Outlook._Application OutlookApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = null; Microsoft.Office.Interop.Outlook.MAPIFolder folderContacts = null; Microsoft.Office.Interop.Outlook.Items contactItems = null; Microsoft.Office.Interop.Outlook.MailItem mail = null; Microsoft.Office.Interop.Outlook.Recipients listRecipients = null; Microsoft.Office.Interop.Outlook.DistListItem distributionList = null; try { string addresseToAdd; ns = OutlookApp.GetNamespace("MAPI"); folderContacts = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts); contactItems = folderContacts.Items; // create a new e-mail message to access the recipients collection mail = contactItems.Add(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as Microsoft.Office.Interop.Outlook.MailItem; mail.To = ""; listRecipients = mail.Recipients; //création de la liste d'adresse foreach (string addresse in tabAdresse) { if (addresse != null) { addresseToAdd = RemoveDiacritics(addresse); listRecipients.Add(addresseToAdd); } } if (!listRecipients.ResolveAll()) { System.Windows.Forms.MessageBox.Show("There are no such contact names. " + "Please make sure that you have corresponding records in your address book", "Add-in Express", MessageBoxButtons.OK, MessageBoxIcon.Warning); } // create a new distribution list item distributionList = contactItems.Add(Microsoft.Office.Interop.Outlook.OlItemType.olDistributionListItem) as Microsoft.Office.Interop.Outlook.DistListItem; distributionList.DLName = "List judo"; distributionList.AddMembers(listRecipients); distributionList.Display(true); } catch (Exception e) { throw new Exception("Erreur Metier.cs/CreateNewDistributionList():\r\n" + e.Message, e); } finally { if (distributionList != null) { Marshal.ReleaseComObject(distributionList); } if (listRecipients != null) { Marshal.ReleaseComObject(listRecipients); } if (mail != null) { mail.Delete(); Marshal.ReleaseComObject(mail); } if (contactItems != null) { Marshal.ReleaseComObject(contactItems); } if (folderContacts != null) { Marshal.ReleaseComObject(folderContacts); } if (ns != null) { Marshal.ReleaseComObject(ns); } } }