private bool TryAddRecipientInModule(string moduleName, string meetingId, Outlook.Recipient recipient) { bool result; string id = SetCrmRelationshipFromOutlook(meetingId, recipient, moduleName); if (!string.IsNullOrWhiteSpace(id)) { string smtpAddress = recipient.GetSmtpAddress(); this.meetingRecipientsCache[recipient.GetSmtpAddress()] = new AddressResolutionData(moduleName, id, smtpAddress); string accountId = RestAPIWrapper.GetRelationship(ContactSyncing.CrmModule, id, "accounts"); if (!string.IsNullOrWhiteSpace(accountId) && SetCrmRelationshipFromOutlook(meetingId, "Accounts", accountId)) { this.meetingRecipientsCache[smtpAddress] = new AddressResolutionData("Accounts", accountId, smtpAddress); } result = true; } else { result = false; } return(result); }
/// <summary> /// Sets up a CRM relationship to mimic an Outlook relationship /// </summary> /// <param name="meetingId">The ID of the meeting</param> /// <param name="recipient">The outlook recipient representing the person to link with.</param> /// <param name="foreignModule">the name of the module we're seeking to link with.</param> /// <returns></returns> private string SetCrmRelationshipFromOutlook(string meetingId, Outlook.Recipient recipient, string foreignModule) { string foreignId = GetID(recipient.Address, foreignModule); return(SetCrmRelationshipFromOutlook(meetingId, foreignModule, foreignId) ? foreignId : string.Empty); }
/// <summary> /// Sets up a CRM relationship to mimic an Outlook relationship /// </summary> /// <param name="meetingId">The ID of the meeting</param> /// <param name="recipient">The outlook recipient representing the person to link with.</param> /// <param name="foreignModule">the name of the module we're seeking to link with.</param> /// <returns></returns> private string SetCrmRelationshipFromOutlook(string meetingId, Outlook.Recipient recipient, string foreignModule) { string foreignId = GetID(recipient.GetSmtpAddress(), foreignModule); return(!string.IsNullOrWhiteSpace(foreignId) && SetCrmRelationshipFromOutlook(meetingId, foreignModule, foreignId) ? foreignId : string.Empty); }
private void Button_Click_1(object sender, RoutedEventArgs e) { // google form for Curry // outlook Microsoft.Office.Interop.Outlook.Application otApp = new Outlook.Application(); // create outlook object Outlook.MailItem otMsg = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add(EmailUrl); otRecip.Resolve(); // validate recipient address otMsg.Subject = "Test Subject"; otMsg.Body = "Text Message"; String sSource = AppDomain.CurrentDomain.BaseDirectory + "Test.txt"; }
/// <summary> /// Adds the appointment recipient. /// </summary> /// <param name="oAppItem">The o app item.</param> /// <param name="name">The name.</param> /// <returns></returns> public OutlookRecipient AddAppointmentRecipient(Outlook._AppointmentItem oAppItem, string name) { OutlookRecipient retVal = null; if (this.InvokeRequired) { Func <Outlook._AppointmentItem, string, OutlookRecipient> func = AddAppointmentRecipient; retVal = this.Invoke(func, oAppItem, name) as OutlookRecipient; } else { Outlook.Recipient oRecipient = oAppItem.Recipients.Add(name); retVal = _factory.Create <OutlookItem>(oRecipient) as OutlookRecipient; } return(retVal); }
private bool TryAddRecipientInModule(string moduleName, string meetingId, Outlook.Recipient recipient) { bool result; string id = SetCrmRelationshipFromOutlook(meetingId, recipient, moduleName); if (!string.IsNullOrWhiteSpace(id)) { this.meetingRecipientsCache[recipient.Address] = new AddressResolutionData(moduleName, id, recipient.Address); result = true; } else { result = false; } return(result); }
/// <summary> /// Set the meeting acceptance status, in CRM, for this invitee to this meeting from /// their acceptance status in Outlook. /// </summary> /// <param name="meeting">The appointment item representing the meeting</param> /// <param name="invitee">The recipient item representing the invitee</param> /// <param name="acceptance">The acceptance status of this invitee of this meeting /// as a string recognised by CRM.</param> private void AddOrUpdateMeetingAcceptanceFromOutlookToCRM(Outlook.AppointmentItem meeting, Outlook.Recipient invitee, string acceptance) { // We don't know which CRM module the invitee belongs to - could be contacts, users, // or indirected via accounts - see AddMeetingRecipientsFromOutlookToCrm. We // cannot look this up every time. Therefore we use a cache. if (this.meetingRecipientsCache.ContainsKey(invitee.GetSmtpAddress())) { var resolution = this.meetingRecipientsCache[invitee.GetSmtpAddress()]; var meetingId = meeting.UserProperties[CrmIdPropertyName]?.Value; if (resolution != null && meetingId != null) { RestAPIWrapper.AcceptDeclineMeeting(meetingId.ToString(), resolution.moduleName, resolution.moduleId, acceptance); } } else { Log.Warn($"Received {acceptance} to meeting {meeting.Subject} from {invitee.GetSmtpAddress()}, but we have no CRM record for that person"); } }
public OutlookRecipient(OutlookListener listener, Outlook.Recipient oRecipient) : base(listener) { _oRecipient = oRecipient; }
/// <summary> /// Set the meeting acceptance status, in CRM, for this invitee to this meeting from /// their acceptance status in Outlook. /// </summary> /// <param name="meeting">The appointment item representing the meeting</param> /// <param name="invitee">The recipient item representing the invitee</param> /// <param name="acceptance">The acceptance status of this invitee of this meeting /// as a string recognised by CRM.</param> private void AddOrUpdateMeetingAcceptanceFromOutlookToCRM(Outlook.AppointmentItem meeting, Outlook.Recipient invitee, string acceptance) { // OK: we don't know which CRM module the invitee belongs to - could be contacts, users, // or indirected via accounts - see AddMeetingRecipientsFromOutlookToCrm. We // cannot look this up every time. So we're going to have to have some sort of a cache. var resolution = this.meetingRecipientsCache[invitee.Address]; var crmItemId = meeting.UserProperties[CrmIdPropertyName]?.Value; if (resolution != null && crmItemId != null) { RestAPIWrapper.AcceptDeclineMeeting(crmItemId, resolution.moduleName, resolution.moduleId, acceptance); } }
public void ImportMail(Outlook.Explorers explorers, ConfigXML configXML) { ComponentResourceManager resources = new ComponentResourceManager(typeof(OpenKMAddIn)); int mailCount = 0; int mailAttach = 0; for (int y = 1; y <= explorers.Count; y++) { Outlook.Explorer openWindow = explorers.Item(y); String token = ""; OKMAuth authService = new OKMAuth(configXML.getHost()); OKMFolder folderService = new OKMFolder(configXML.getHost()); OKMRepository repositoryService = new OKMRepository(configXML.getHost()); OKMMail mailService = new OKMMail(configXML.getHost()); OKMDocument documentService = new OKMDocument(configXML.getHost()); try { if (configXML.getUser().Equals("") || configXML.getPassword().Equals("") || configXML.getHost().Equals("")) { throw new Exception(resources.GetString("error_configuration_empty")); } token = authService.login(configXML.getUser(), configXML.getPassword()); for (int i = 1; i <= openWindow.Selection.Count; i++) { Object selObject = openWindow.Selection.Item(i); if (selObject is Outlook.MailItem) { mailCount++; Outlook.MailItem mailItem = (selObject as Outlook.MailItem); DateTime receivedTime = mailItem.ReceivedTime; String user = configXML.getUser(); String basePath = "/okm:mail/" + user + "/"; String year = "" + receivedTime.Year; String month = "" + receivedTime.Month; String day = "" + receivedTime.Day; // Only creating folders when it's needed if (repositoryService.hasNode(token, basePath + year)) { if (repositoryService.hasNode(token, basePath + year + "/" + month)) { if (!repositoryService.hasNode(token, basePath + year + "/" + month + "/" + day)) { folder dayFolder = new folder(); dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, dayFolder); } } else { folder monthFolder = new folder(); folder dayFolder = new folder(); monthFolder.path = basePath + year + "/" + month; dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, monthFolder); folderService.create(token, dayFolder); } } else { folder yearFolder = new folder(); folder monthFolder = new folder(); folder dayFolder = new folder(); yearFolder.path = basePath + year; monthFolder.path = basePath + year + "/" + month; dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, yearFolder); folderService.create(token, monthFolder); folderService.create(token, dayFolder); } // Adding mail values mail newMail = new mail(); newMail.path = basePath + year + "/" + month + "/" + day + "/" + mailItem.Subject; newMail.subject = mailItem.Subject; newMail.from = mailItem.GetType().InvokeMember("SenderEmailAddress", System.Reflection.BindingFlags.GetProperty, null, mailItem, null).ToString(); //newMail.from = mailItem.SenderEmailAddress; // SenderEmailAddress was introduced in outlook 2002 newMail.sentDate = mailItem.SentOn; newMail.sentDateSpecified = true; newMail.receivedDate = mailItem.ReceivedTime; newMail.receivedDateSpecified = true; // Setting mail context and type BodyFormat format = (BodyFormat)mailItem.GetType().InvokeMember("BodyFormat", System.Reflection.BindingFlags.GetProperty, null, mailItem, null); if (format.Equals(BodyFormat.olFormatPlain)) { newMail.mimeType = "text/plain"; newMail.content = mailItem.Body; } else { newMail.mimeType = "text/html"; newMail.content = mailItem.HTMLBody; } // Initialize count recipient address variables int count = 0; int countTo = 0; int countCC = 0; int countBCC = 0; int actualCountTo = 0; int actualCountCC = 0; int actualCountBCC = 0; // Count each mail addresss type to / cc / bcc for (int x = 1; x <= mailItem.Recipients.Count; x++) { Outlook.Recipient recipient = mailItem.Recipients.Item(x); switch (recipient.Type) { case 1: countTo++; break; case 2: countCC++; break; case 3: countBCC++; break; default: countTo++; break; } count++; } // Initialize variables String[] mailTo = new String[(countTo > 0) ? countTo : 1]; String[] mailCC = new String[(countCC > 0) ? countCC : 1]; String[] mailBCC = new String[(countBCC > 0) ? countBCC : 1]; // All string[] must have at least one value, it¡s mandatory in webservices if (countTo == 0) { mailTo[0] = ""; } if (countCC == 0) { mailCC[0] = ""; } if (countBCC == 0) { mailBCC[0] = ""; } // Depending mail type each mail is assignede to it own type String[] for (int x = 1; x <= mailItem.Recipients.Count; x++) { Outlook.Recipient recipient = mailItem.Recipients.Item(x); switch (recipient.Type) { case 1: mailTo[actualCountTo] = recipient.Address; actualCountTo++; break; case 2: mailCC[actualCountCC] = recipient.Address; actualCountCC++; break; case 3: mailBCC[actualCountBCC] = recipient.Address; actualCountBCC++; break; default: mailTo[actualCountTo] = recipient.Address; actualCountTo++; break; } } // Assign mail recipients by type newMail.bcc = mailBCC; newMail.cc = mailCC; newMail.to = mailTo; // Creating mail newMail = mailService.create(token, newMail); // Setting attachments if (mailItem.Attachments.Count > 0) { for (int x = 1; x <= mailItem.Attachments.Count; x++) { Outlook.Attachment attachment = mailItem.Attachments.Item(x); mailAttach++; document doc = new document(); doc.path = newMail.path + "/" + attachment.FileName; // save as tempfile for reading String filename = Environment.GetEnvironmentVariable("TEMP") + "\\" + DateTime.Now.ToString("yymmddHHMMss-") + attachment.FileName; // save the attachment attachment.SaveAsFile(filename); // Uploading document documentService.create(token, doc, ReadFile(filename)); // Delete a file by using File class static method... if (File.Exists(filename)) { // Use a try block to catch IOExceptions, to // handle the case of the file already being // opened by another process. try { File.Delete(filename); } catch (System.IO.IOException ex) { MessageBox.Show(String.Format(resources.GetString("error_deleting_tmp_file"), filename, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // Releasing com object Marshal.ReleaseComObject(attachment); } } // Releasing com object Marshal.ReleaseComObject(mailItem); } // Releasing com object Marshal.ReleaseComObject(selObject); } if (!token.Equals("")) { authService.logout(token); // Always we logout token = ""; // Reseting token value } if (mailCount > 0) { MessageBox.Show(String.Format(resources.GetString("email_successful_imported"), mailCount, mailAttach)); } else { MessageBox.Show(resources.GetString("error_mail_not_selected"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (!token.Equals("")) { authService.logout(token); // Always we logout } } } }
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch (e.Button.Text) { case "Send": Outlook._Application outlookApp = null; Outlook._MailItem newMail = null; Outlook.Recipient recipient = null; Outlook.Recipients recipients = null; Type olType = Type.GetTypeFromProgID("Outlook.Application", false); if (olType != null) { try { outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application; } catch { } try { if (outlookApp == null) { outlookApp = Activator.CreateInstance(olType) as Outlook._Application; } } catch { } if (outlookApp != null) { if (tbtnMode.Pushed) { securityManager1.ConnectTo(outlookApp); securityManager1.DisableOOMWarnings = true; } try { newMail = outlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; if (newMail != null) { try { recipients = newMail.Recipients; if (recipients != null) { try { if (txbTO.Text != string.Empty) { recipient = recipients.Add(txbTO.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olTo; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } if (txbCC.Text != string.Empty) { recipient = recipients.Add(txbCC.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olCC; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } if (txbBCC.Text != string.Empty) { recipient = recipients.Add(txbBCC.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olBCC; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } newMail.Subject = txbSubject.Text; newMail.Body = txbMail.Text; newMail.Send(); } finally { Marshal.ReleaseComObject(recipients); } } Marshal.ReleaseComObject(newMail); newMail = null; MessageBox.Show("The message has been sent successfully.", "Send Mail", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception err) { if (newMail != null) { Marshal.ReleaseComObject(newMail); } MessageBox.Show(err.Message, err.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } finally { if (tbtnMode.Pushed) { securityManager1.DisableOOMWarnings = false; securityManager1.Disconnect(outlookApp); } if (outlookApp != null) { Marshal.ReleaseComObject(outlookApp); } } } } break; case "Security": if (tbtnMode.Pushed) { tbtnMode.ImageIndex = 2; } else { tbtnMode.ImageIndex = 1; } break; } }