Send different attachment to different recipient in appointment EWS (Exchange Web Service) - c#

I am trying to send meeting invitation by Appointment class of EWS.
I have a requirement to send different attachment to different recipients. I am referencing the following links:
https://social.msdn.microsoft.com/Forums/exchange/en-US/cf4b9d9a-7bbb-4caa-9d55-300371fa84ac/ews-attachment-not-sent-with-invitation
This link is just for one or more than one attachment might to be sent but I need each recipients should have different-2 attachments.
I am trying to following in my code that might be helpful to better understand the challenge:
Appointment appointment = new Appointment(service) {
Start = DateTime.Now,
End = DateTime.Now.AddHours(2),
Subject = "XYZ Invitation",
Location = "XYZ Tower, Room No. 3",
IsAllDayEvent = false,
AllowNewTimeProposal = false,
IsResponseRequested = false,
Body = new MessageBody(BodyType.HTML, html),
ReminderMinutesBeforeStart = 60
};
int i = 0;
foreach(var attendee in attendies) { // List<string>
appointment.Attachments.AddFileAttachment(Image[i], file);
appointment.Attachments[0].IsInline = true;
appointment.Attachments[0].ContentId = Image[i];
FolderId folderCalendar = new FolderId(WellKnownFolderName.Calendar, attendee);
appointment.Save(folderCalendar, SendInvitationsMode.SendToNone);
appointment.RequiredAttendees.Add(attendee);
i++;
appointment.Update(ConflictResolutionMode.AutoResolve, SendInvitationsOrCancellationsMode.SendOnlyToAll);
}

You must use Bind method of Appointment class in order to send different attachment second time to add receipt.
appointment.Bind(ExchangeService, ItemId, PropertySet);
Binds to an existing appointment and loads the specified set of properties. Calling this method results in a call to Exchange Web Services (EWS).
I hope it helps you.

Related

Set Online meeting in Teams

I have been using Exchange WebServices (EWS) for some time now, in Asp.net C #, to add events in the calendars of Office365 users at my work.
I now needed those same events to appear at Microsoft Teams, with the possibility of going on videoconference.
Events appear but that possibility is not present.
One of the properties of "appointments" is "isOnlineMeeting". I tried to add it, making it true, but it always returns an error saying "Set action is invalid for property.".
In the online searches I have done, I have found that this is a read-only property.
So, is there any chance that I can "force" this property?
I have already configured my Exchange so that, in Outlook online when we do a new event, this is always by videoconference.
Some help?
Thank you!
UPDATE:
By the way, the code that I'm using is:
try {
ExchangeService service = new ExchangeService (ExchangeVersion.Exchange2013_SP1, TimeZoneInfo.FindSystemTimeZoneById ("GMT Standard Time"));
service.Url = new Uri ("https://outlook.office365.com/EWS/Exchange.asmx");
string User = "a#a.net";
string Password = "AAA";
service.Credentials = new NetworkCredential (User, Password);
Appointment appointment = new Appointment (service);
appointment.Subject = "Experiment";
appointment.Location = "Videoconference";
string dataStart = "10-02-2021 19:00:00.000";
string dataEnd = "10-02-2021 20:00:00.000";
appointment.Start = DateTime.Parse (dataStart);
appointment.End = DateTime.Parse (dataEnd);
appointment.Body = "<strong>Ignore! Just a test</strong>";
appointment.IsOnlineMeeting = true;
appointment.RequiredAttendees.Add ("b#a.net");
appointment.Save (SendInvitationsMode.SendOnlyToAll);
} catch (Exception ex) {
}
Posting the Answer for better knowledge
Copying from comments
Could you please try with this document by using Graph API. By using Graph API we can set "isOnlineMeeting" property to true, and "onlineMeetingProvider" property to "teamsForBusiness".

EWS save sent item in WellKnownFolderName.SentItems

I'm trying to send an EmailMessage using EWS with user A and save the sent item in the SentItems folder of user B. Basically it works. The only problem I encounter, the item is saved as a draft and not as a sent item.
The code:
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2, timeZone)
{
Url = new Uri(uri),
Credentials = new WebCredentials(new NetworkCredential(user, password, domain)),
UseDefaultCredentials = false,
};
EmailMessage message = new EmailMessage(service)
{
Subject = subject,
Body = new MessageBody(BodyType.HTML, fullBody)
};
message.From = email;
message.ToRecipients.Add(email);
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
What I tried:
// Simply sends the message
message.Send();
// Sends the item but is it not saved in the sentItems of email-account
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.SendAndSaveCopy(folderId);
// Sends the item, saves the item in the right folder, but it is saved as a draft, not as a sent item
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.Send();
message.Move(folderId);
What am I missing or doing wrong?
This guy tells to simply save and then save usind the folderId, but in such a scenario I get the following error:
This operation can't be performed because this service object already has an ID. To update this service object, use the Update() method instead.
After some tests I figured out that correct procedure is the following one:
FolderId folderId = new FolderId(WellKnownFolderName.SentItems, email);
message.SendAndSaveCopy(folderId);
However, there must be a bug because the item is always saved in the sentItems folder of the user used to authenticate the ExchangeService ignoring the folderId passed as argument.
The solution (workaround) is to impersonate the user as Microsoft Documentation explains. Here the impersionation code:
service.AutodiscoverUrl(email);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email);
It works...I wonder if somebody else was able to arrange it in a proper way.

EWS Managed API 2.0 Id is malformed

I am using EWS Managed API 2.0 to send the meeting invitation and capture the user response.
I have followed the reference from official website https://msdn.microsoft.com/en-us/library/office/jj220499(v=exchg.80).aspx
and https://msdn.microsoft.com/en-us/library/office/dd633661(v=exchg.80).aspx. I, am getting an error as Id is malformed.
I have used the exact code in the official website. I did some googling and found that if id has special character this error may appear. But the id I provided has no special character its just a simple character.
Here is the code
ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("********#live.com", "************");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("***********#live.com", RedirectionUrlValidationCallback);
// Create the appointment.
Appointment appointment = Appointment.Bind(service, "AAMkA=");
// Set properties on the appointment. Add two required attendees and one optional attendee.
appointment.Subject = "Status Meeting";
appointment.Body = "The purpose of this meeting is to discuss status.";
appointment.Start = new DateTime(2009, 3, 1, 9, 0, 0);
appointment.End = appointment.Start.AddHours(2);
appointment.Location = "Conf Room";
appointment.RequiredAttendees.Add("***********#live.com");
// Send the meeting request to all attendees and save a copy in the Sent Items folder.
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
foreach (Attendee t in appointment.RequiredAttendees)
{
if (t.ResponseType != null)
Console.WriteLine("Required attendee - " + t.Address + ": " + t.ResponseType.Value.ToString());
}
Getting an error in Appointment appointment = Appointment.Bind(service, "AAMkA=");The id is malfunction.
Exchange server will provide a unique Id on creating the appointment.
So need to save the Id and pass the same Id when fetching the user response.
var Id = appointment.Id;
So pass the Id to fetch in the response.

Exchange web services - Forward Email as Attachment

I'm currently using Exchange Web Services in C#. I basically have a small application that reads emails from an inbox and process them.
I would like to forward those emails I receive as an attachment of an email. This attachment will be an outlook email that will include the original email (including its own attachments if any).
Any ideas?
Thanks!
EDIT:
Not sure why I'm getting the down votes but it seems this is not possible as the EWS API does not provide such functionality
You can create an ItemAttachment with EWS but you can't replicate fully what is possible in Outlook with MAPI. Eg with EWS you can create an ItemAttachment and then use the MIMEContent to create an attachment based on a current message as a workaround eg
FolderId Inboxid = new FolderId(WellKnownFolderName.Inbox, "target#domain.com");
ItemView InboxItemView = new ItemView(1);
FindItemsResults<Item> inFiResuls = service.FindItems(Inboxid,InboxItemView);
if(inFiResuls.Items.Count == 1){
EmailMessage fwdMail = new EmailMessage(service);
EmailMessage orgMail = (EmailMessage)inFiResuls.Items[0];
PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
psPropSet.Add(ItemSchema.MimeContent);
orgMail.Load(psPropSet);
ItemAttachment emAttach = fwdMail.Attachments.AddItemAttachment<EmailMessage>();
emAttach.Item.MimeContent = orgMail.MimeContent;
ExtendedPropertyDefinition pr_flags = new ExtendedPropertyDefinition(3591,MapiPropertyType.Integer);
emAttach.Item.SetExtendedProperty(pr_flags,"1");
emAttach.Name = orgMail.Subject;
fwdMail.Subject = "see Attached";
fwdMail.ToRecipients.Add("user#domain.com");
fwdMail.Send();
}
This however doesn't give full fidelity of all the mapi properties associated with a particular message as the MIMEContent is just that, for most normal email messages this isn't an issue however for a message with an attached Outlook Task or other TNEF attachment then you would loose these of attachments or for other properties like categories,flags you would loose these also (you could copy these manually if needed).
Cheers
Glen
you can forward your email using this way. It first loads and reads the each emails with attachment who has "msg" extension. then forwards it to given address. see the below code
FindItemsResults<Item> findResults = exchange.FindItems(WellKnownFolderName.Inbox, newItemView(50,50));
Item[] msgItems = findResults.Where(msgItem => msgItem.HasAttachments).ToArray();
EmailMessage msgInfo = null;
var fileExtensions = new List<string> { "msg", "MSG", "Msg" };
foreach (MSEWS.Item msgItem in msgItems )
{
msgInfo = EmailMessage.Bind(exchange, msgItem.Id);
FileAttachment fa = msgInfo.Attachments[0] asFileAttachment;
if (fileExtensions.Any(ext => ext.Contains(fa.Name.Substring(fa.Name.Length - 3))))
{
fa.Load();
ResponseMessage responseMessage = msgInfo.CreateForward();
responseMessage.BodyPrefix = "messageBody";
responseMessage.ToRecipients.Add("toAddress");
responseMessage.Subject = "subject";
responseMessage.SendAndSaveCopy();
}
}

Mail item not found on Exchange server using Item ID

I have an application that has been working fine for several weeks, basically it polls an Exchange 2010 mail server mailbox for new messages, gets the details of each one and any attachments, and use that information to create cases (with attachments) on the SalesForce CRM.
After each case is created from the email, the email must be marked as read by retrieving it from Exchange using the stored Item ID and calling the property set.
For several weeks this has been working fine, even getting some extra properties from Exchange such as the Subject, From, To properties which I can use in the session log.
Suddenly today the mail isn't being returned by Exchange from the itemID if I try to include the PropSet - if I omit the PropSet it works fine.
here is the code:
try
{
//creates an object that will represent the desired mailbox
Mailbox mb = new Mailbox(common.strInboxURL); // new Mailbox(targetEmailAddress); #"bbtest#bocuk.local"
//creates a folder object that will point to inbox fold
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
//this will bind the mailbox you're looking for using your service instance
Microsoft.Exchange.WebServices.Data.Folder inbox = Microsoft.Exchange.WebServices.Data.Folder.Bind(service, fid);
// As a best practice, limit the properties returned to only those that are required.
PropertySet propSet = new PropertySet(BasePropertySet.IdOnly,
ItemSchema.Subject, EmailMessageSchema.IsRead, EmailMessageSchema.Sender, EmailMessageSchema.DateTimeReceived);
// Bind to the existing item by using the ItemId.
// This method call results in a GetItem call to EWS.
EmailMessage mail = EmailMessage.Bind(service, itemId); //, propSet);
if (!mail.IsRead) // check that you don't update and create unneeded traffic
{
mail.IsRead = true; // mark as read
mail.Update(ConflictResolutionMode.AutoResolve); // persist changes
}
mail.Update(ConflictResolutionMode.AlwaysOverwrite);
e2cSessionLog("\tcommon.MarkAsRead", "email Item ID: " + itemId);
//e2cSessionLog("\tcommon.MarkAsRead", "MARKED AS READ email Subject: " + mail.Subject.ToString() + "; From: " + mail.Sender.Name, mail.DateTimeReceived.ToString());
return true;
}
catch (Exception ex)
{
string innerException = "";
...
notice that in the line EmailMessage mail = EmailMessage.Bind(service, itemId); //, propSet); I have now omitted the PropSet argument and it now works.
But why?
Is there something else I need to do in order to always get the properties back with the mail returned using Item ID?
Any thoughts?

Categories

Resources