I'm trying to send a reply email using Exchange WebService and got blocked with the below exception.
System.InvalidOperationException
HResult=0x80131509
Message=This operation can't be performed because this service object already has an ID. To update this service object, use the Update() method instead.
Source=Microsoft.Exchange.WebServices
StackTrace:
at Microsoft.Exchange.WebServices.Data.ServiceObject.ThrowIfThisIsNotNew()
at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
at Microsoft.Exchange.WebServices.Data.Item.Save(FolderId parentFolderId)
at DataLoadLibrary.DataLoad.Email.EWSOAuthAdapter.genericEmail(String recepient, String subject, String bodyMessage, Dictionary`2 replacementStrings, String cc, String bcc, String headerMessage, String footerMessage, FileInfo[] attachments, String[] filenames, EmailMessage originalEmail, ExchangeService exchangeClient) in C:\Path\DataLoadLibrary\DataLoad\Email\EWSOAuthAdapter.cs:line 95
at DataLoadLibrary.DataLoad.Email.OAuthBasedTestPortRequest.startDataload() in C:\Path\DataLoadLibrary\DataLoad\Email\OAuthBasedTestPortRequest.cs:line 132
at TestPortEmailReader.Program.<Main>d__8.MoveNext() in C:\Path\TestPortEmailReader\Program.cs:line 93
This exception was originally thrown at this call stack:
[External Code]
DataLoadLibrary.DataLoad.Email.EWSOAuthAdapter.genericEmail(string, string, string, System.Collections.Generic.Dictionary<string, string>, string, string, string, string, System.IO.FileInfo[], string[], Microsoft.Exchange.WebServices.Data.EmailMessage, Microsoft.Exchange.WebServices.Data.ExchangeService) in EWSOAuthAdapter.cs
DataLoadLibrary.DataLoad.Email.OAuthBasedTestPortRequest.startDataload() in OAuthBasedTestPortRequest.cs
TestPortEmailReader.Program.Main(string[]) in Program.cs
#Implementation
public bool genericEmail(
string recepient,
string subject,
string bodyMessage,
Dictionary<string, string> replacementStrings = null,
string cc = "",
string bcc = "",
string headerMessage = "",
string footerMessage = "",
FileInfo[] attachments = null,
string[] filenames = null,
EmailMessage originalEmail = null,
ExchangeService exchangeClient = null)
{
EmailMessage email = EmailMessage.Bind(exchangeClient, originalEmail.Id, PropertySet.FirstClassProperties);
//exchangeClient.LoadPropertiesForItems((IEnumerable<Item>)email, PropertySet.FirstClassProperties);
email.Sender.Address = config.exchangeUsername + "@domain.com";
email.Sender.Name = "ABC";
if (recepient.Length == 0)
{
email.ToRecipients.Add(iPACTTeamEmail);
}
else
{
email.ToRecipients.Add(recepient.TrimEnd(';'));
}
if (cc.Length != 0)
email.CcRecipients.Add(cc.TrimEnd(';'));
if (config.environment != "PR")
{
email.Sender.Address = config.exchangeUsername + "@domain.com";
email.CcRecipients.Add(null);
}
if (attachments != null)
{
int i = 0;
foreach (FileInfo fi in attachments)
{
FileStream theStream = new FileStream(fi.FullName, FileMode.OpenOrCreate);
if (filenames == null)
email.Attachments.AddFileAttachment(fi.Name, theStream);
else
email.Attachments.AddFileAttachment(filenames[i], theStream);
i++;
}
}
if (originalEmail != null)
{
string originalEmailFilename = config.extractFolder + "\\Emails" + originalEmail.Subject.Trim().Replace('\\', '_').Replace('/', '_').Replace(":", "_").Replace("\"", "_").Replace("*", "_").Replace("?", "_").Replace("<", "_").Replace(">", "_").Replace("|", "_") + "_" + originalEmail.DateTimeReceived.ToString("yyyyMMdd_HHmmss") + ".msg";
originalEmail.Save(originalEmailFilename); //Getting above exception here
email.Attachments.AddFileAttachment(originalEmailFilename);
}
if (config.environment == "PR")
email.Subject = subject.Trim();
else
email.Subject = "[" + config.environment + "] " + subject.Trim();
if (replacementStrings != null)
{
foreach (KeyValuePair<string, string> kv in replacementStrings)
{
bodyMessage = bodyMessage.Replace(kv.Key, kv.Value);
}
}
bodyMessage = bodyMessage.Replace("{headerMessage}", headerMessage);
bodyMessage = bodyMessage.Replace("{footerMessage}", footerMessage);
email.Body = new MessageBody(BodyType.HTML, bodyMessage);
email.Send();
return true;
}
Even after binding the mail id am getting the same exception when saving the original email. Is it because am trying to save it multiple times. I went through this one https://social.msdn.microsoft.com/Forums/exchange/en-US/4beb7588-f851-4c7d-b060-f46ae2642e61/get-email-message-after-send-it-with-exchange-library?forum=exchangesvrdevelopment but didn't get a proper solution. Kindly help me to identify what is missing in my implementation
string originalEmailFilename = config.extractFolder + "\\Emails" + originalEmail.Subject.Trim().Replace('\\', '_').Replace('/', '_').Replace(":", "_").Replace("\"", "_").Replace("*", "_").Replace("?", "_").Replace("<", "_").Replace(">", "_").Replace("|", "_") + "_" + originalEmail.DateTimeReceived.ToString("yyyyMMdd_HHmmss") + ".msg";
originalEmail.Save(originalEmailFilename); //
You can't do this in the EWS Managed API as it doesn't support saving a Message to a file, also if you want an Msg file then EWS won't work for that at all this is an Office file format (compound ole file https://docs.microsoft.com/en-us/cpp/mfc/containers-compound-files?view=msvc-170 ). You can save a Message as an Eml file using Mime Stream as documented in https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-export-items-by-using-ews-in-exchange
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.