[英]Getting System.InvalidOperationException when sending an email using Exchange Web service
I'm trying to send a reply email using Exchange WebService and got blocked with the below exception.我正在尝试使用 Exchange WebService 发送回复电子邮件,但因以下异常而被阻止。
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.即使在绑定邮件 ID 后,在保存原始电子邮件时也会遇到相同的异常。 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.
我经历了这个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但没有得到合适的解决方案。 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 ).您不能在 EWS 托管 API 中执行此操作,因为它不支持将消息保存到文件中,而且如果您想要一个 Msg 文件,那么 EWS 根本无法用于此操作,这是一种 Office 文件格式(复合 ole文件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
您可以使用 Mime Stream 将消息保存为 Eml 文件,如https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-export-items-by中所述-使用 ews-in-exchange
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.