I have a specific requirement, I have a .Net Application where I have some dates, so I want to create appointments for these dates in Outlook, GSuit and iCal calendar.
Can someone help me to give steps to create these appointments programmatically C#.
I tried to achieve by adding reference Microsoft.Office.interop.outlook and below code. i got this code from Microsoft site but I have difficulty to replace "this". what code should replace "this" in below code.
{
try
{
Outlook.AppointmentItem newAppointment =
(Outlook.AppointmentItem)
this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
newAppointment.Start = DateTime.Now.AddHours(2);
newAppointment.End = DateTime.Now.AddHours(3);
newAppointment.Location = "ConferenceRoom #2345";
newAppointment.Body =
"We will discuss progress on the group project.";
newAppointment.AllDayEvent = false;
newAppointment.Subject = "Group Project";
newAppointment.Recipients.Add("Roger Harui");
Outlook.Recipients sentTo = newAppointment.Recipients;
Outlook.Recipient sentInvite = null;
sentInvite = sentTo.Add("Holly Holt");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olRequired;
sentInvite = sentTo.Add("David Junca ");
sentInvite.Type = (int)Outlook.OlMeetingRecipientType
.olOptional;
sentTo.ResolveAll();
newAppointment.Save();
newAppointment.Display(true);
}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " + ex.Message);
}```
Related
I basically want to create emails which shall be displayed in the Outlook Inbox using C# Outlook VSTO AddIn.
I have installed the test dlls for Outlook Redemption in my C# Outlook VSTO Addin.
For testing purposes I created a Ribbon Bar with a button. When the button is clicked the following event is triggered:
private void CreateMailItemWithRedemption()
{
//FYI: outlookApp is set in Ribbon load event
//Outlook.Application outlookApp = Globals.ThisAddIn.Application as Microsoft.Office.Interop.Outlook.Application;
var outlookNameSpace = outlookApp.GetNamespace("MAPI");
RDOSession session = new RDOSession();
session.MAPIOBJECT = outlookNameSpace.MAPIOBJECT;
RDOFolder folder = session.GetDefaultFolder(rdoDefaultFolders.olFolderInbox);
RDOMail msg = folder.Items.Add("IPM.Note");
msg.Sent = true;
DateTime dt = new DateTime(2021, 8, 29, 5, 10, 20, DateTimeKind.Utc);
msg.ReceivedTime = dt;
msg.Subject = "Mail created with Redemption";
msg.To = “******#outlook.com”;
msg.Sender = GetAddressEntry("[TestFirstName] [TestLastName]");
msg.Save();
}
It basically works – but for the time being I am stuck with setting the Sender for the new RDOMail which I understand needs to be an RDOAddressEntry.
The GetAddressEntry method looks as follows (with firstname string + lastname string of an existing contact as parameter, like “John Do”):
private RDOAddressEntry GetAddressEntry(string _name)
{
RDOSession session = new RDOSession();
session.MAPIOBJECT = outlookApp.Session.MAPIOBJECT;
RDOAddressList contacts = session.AddressBook.GAL;
RDOAddressEntry contact = contacts.ResolveName(_name);
MessageBox.Show(contact.ToString());
return contact;
}
At contacts.ResolveName an exception is thrown. Do you have an idea what I am doing wrong?
Any hints are welcomed.
Many thanks in advance
Alexander
Hello Im updating a UWP app that we use in ouroffice.
Before when we use it to send email it uses the Windows.ApplicationModel.Email.EmailMessage however this i really limited. So i would like to use Microsoft.Office.Interop.Outlook instead to create the email directly with outlook
My test code looks like this.
try
{
List<string> lstAllRecipients = new List<string>();
//Below is hardcoded - can be replaced with db data
lstAllRecipients.Add("info#test.com");
//lstAllRecipients.Add("chandan.kumarpanda#testmail.com");
Outlook.Application outlookApp = new Outlook.Application();
Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Inspector oInspector = oMailItem.GetInspector;
// Thread.Sleep(10000);
// Recipient
Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
foreach (String recipient in lstAllRecipients)
{
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
}
////Add CC
//Outlook.Recipient oCCRecip = oRecips.Add("THIYAGARAJAN.DURAIRAJAN#testmail.com");
//oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC;
//oCCRecip.Resolve();
//Add Subject
oMailItem.Subject = "Test Mail";
// body, bcc etc...
//Display the mailbox
oMailItem.Display(true);
}
catch (Exception objEx)
{
await new MessageDialog(objEx.Message, "Error email to Outlook").ShowAsync();
}
I have added the Microsoft.Office.Interop.Outlook.dll version to the app by finding it in C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Outlook\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll it is file version 15.0.4569.1507
My problem is when i run the code i get the following error
Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} using CoCreateInstanceFromApp failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). Please make sure your COM object is in the allowed list of CoCreateInstanceFromApp.
I guees i have to add a reference in the appxmanifest how ever i cannot figure out what.
Based on this tutorial, I'm trying to get contacts photos
private String createPhoto() {
try {
AttachmentCollection attachments = contact.getAttachments();
for (Attachment attachment : attachments.getItems()) {
if (attachment instanceof FileAttachment) {
boolean isPhoto = ((FileAttachment) attachment).isContactPhoto();
if (isPhoto) {
attachment.load();
FileAttachment photo = contact.getContactPictureAttachment();
String filename = photo.getName() + ".jpg";
photo.load(new FileOutputStream(filename, true));
return filename;
}
}
}
} catch (Exception ex) {
LOGGER.info("" + ex);
}
return null;
}
However, attachments.getItems() is always an empty array.
On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name#company.exchange.com&size=HR360x360
Why I can't get a photo from the code?
On my mailbox, I have few contacts with photos, and I can receive them by calling URL https://companyname.exchange.com/EWS/Exchange.asmx/s/GetUserPhoto?email=name#company.exchange.com&size=HR360x360
That request gets the Userphoto which is stored in the (Source)Users Mailbox (or low res in ActiveDirectory) and made available by that operation.
Your code is trying to retrieve the ContactPhoto which can be stored as an attachment on Contacts in a UserMailbox.
So these are two separate things so which one are you dealing with ?, As you haven't shown it you need to make sure you ExchangeServerRequest Version is set to 2010 or greater as Contact photos aren't returned in 2007. You might also want to test quickly the contacts in Question with the EWS Editor https://ewseditor.codeplex.com/ that will allow you to get the objects and see if there is a ContactPhoto Attachments using EWS.
I am new to sharepoint development.
I have a requirement to create subsite in sharepoint server with the help of visual studio 2010. I need a code to create a subsite when a user add a new list item in the sharepoint site. I used EventListViewer to call the Add Item event. Please guide me how to work on this issue.
I am sharing the code i used in event receiver
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
try
{
WriteLog("Process started", System.Diagnostics.EventLogEntryType.SuccessAudit );
// Get Properties
string name = properties.ListItem["Title"].ToString();
string description = properties.ListItem["Description"].ToString();
DateTime startDate = (DateTime)properties.ListItem["Start Date"];
DateTime endDate = (DateTime)properties.ListItem["End Date"];
// Create sub site
SPWeb web = properties.OpenSite().AllWebs.Add(name.Replace(" ", string.Empty), name,
description, 0, SPWebTemplate.WebTemplateSTS, false, false);
web.Update();
WriteLog("Process Completed", System.Diagnostics.EventLogEntryType.SuccessAudit);
}
catch (Exception ex)
{
WriteLog("Error:" + ex.Message + " StackTrace --" + ex.StackTrace, System.Diagnostics.EventLogEntryType.Error);
}
}
I have written a custom code such that when ever an email arrives which has an attachment,it must get downloaded into a shared location, and email arrives daily.
When I open my laptop daily it's working fine if i don't open and if there are continuous mails (with attachments) and it is not getting downloaded for example, when i open my laptop on monday I have 3 mails with attachment (from satu, sunday, monday).
It is not downloading the latest report from monday it is still showing me same report on saturday.
Here is my code..
private void ThisAddIn_Startup(object sender,System.EventArgs e) {
outlookNameSpace=this.Application.GetNamespace("MAPI");
inbox=outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items=inbox.Items;
items.ItemAdd +=new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
private void items_ItemAdd(object Item) {
Outlook.Items inboxitems;
const string destinationDirectory=#"\\Service Now\";
Outlook.MailItem newEmail=null;
inboxitems=inbox.Items.Restrict("[Unread] = true");
try {
foreach (object collectionItem in inboxitems) {
newEmail=collectionItem as Outlook.MailItem;
if (newEmail !=null) {
if (newEmail.Attachments.Count > 0) {
for (int i=1; i <= newEmail.Attachments.Count; i++) {
if (newEmail.Attachments[i].FileName.Contains("Logic")) {
// String Des= destinationDirectory.Remove(0, 1);
newEmail.Attachments[i].SaveAsFile(destinationDirectory + newEmail.Attachments[i].FileName);
// MessageBox.Show("Hurry");
}
}
}
}
}
}catch (System.Exception ex) {
MessageBox.Show(""+ex);
}
}
Did you try to debug the code?
I see the following conditions in the code:
inboxitems= inbox.Items.Restrict("[Unread] = true");
and
if (newEmail.Attachments[i].FileName.Contains( "Logic"))
Make sure that emails corresponds to the conditions shown above.
Be aware, the ItemAdd event of the Items class is not fired when a large number of items are added to the folder at once (more than 16).
P.S. Try to check out the MarkForDownload property of Outlook items - an OlRemoteStatus value that determines the status of an item once it is received by a remote user.