Home > Blockchain >  Send Email Marked High Importance via EWS API
Send Email Marked High Importance via EWS API

Time:10-05

Attempting to send an email marked as high importance via EWS API. Getting multiple compile errors and or issues stating missing assembly. Any help would be appreciated. Using the following namespaces:

  • System
  • System.Drawing
  • System.Data
  • Microsoft.Exchange.WebServices.Data
  • System.Collections.Generic
  • System.Web
  • System.Text
  • System.IO
  • System.Linq

I've referenced all the MS Docs on the EWS API and aware that the importance is an enum but unsure of the class to use to set the argument;

I've tried multiple bits of syntax e.g. email.Importance = 2 but getting enum conversion error.

Current code is as follows:

Success = true;
ErrorMessage = "";
try {
if (To == "" && CC == "" && BCC == "") {
    throw new Exception("Recipients was not provided");
}
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
string serviceUrl = "https://mail.mydomain.com/ews/exchange.asmx";
service.Url = new Uri(serviceUrl);

EmailMessage email = new EmailMessage(service);
email.Subject = Subject;
if (isHTML) {
    email.Body = new MessageBody(BodyType.HTML, Message);
} else {
    email.Body = new MessageBody(BodyType.Text, Message);
}
if (isHighImportance) {
    email.Importance = new ImportantChoiceType(HIGH);
} else {
    email.Importance = new ImportantChoiceType(NORMAL);
}   
char[] splitComma =  new char[]{','};
char[] splitAsterisk =  new char[]{'*'};
char[] splitSemicolon =  new char[]{';'};
char[] splitAny =  new char[]{',',';'};

//To
foreach (String r in To.Split(splitAny, StringSplitOptions.RemoveEmptyEntries))
{
    email.ToRecipients.Add(r.Trim());
}
//CC
foreach (String r in CC.Split(splitAny, StringSplitOptions.RemoveEmptyEntries)) 
{
    email.CcRecipients.Add(r.Trim());
}
//BCC
foreach (String r in BCC.Split(splitAny, StringSplitOptions.RemoveEmptyEntries)) 
{
    email.BccRecipients.Add(r.Trim());
}

//SaveDraft
email.Save(SearchFolderByName(service,"Drafts",mailbox));

// Add attachments
foreach (String att in Attachments.Split(splitSemicolon, StringSplitOptions.RemoveEmptyEntries)) 
{
    if (att.Contains(new string(splitAsterisk))) {
        String[] attArray = att.Split(splitAsterisk, StringSplitOptions.RemoveEmptyEntries);
        FileAttachment fileatt = email.Attachments.AddFileAttachment(attArray[0]);
        fileatt.IsInline = true;
        fileatt.ContentId = attArray[1];
    } else {
        email.Attachments.AddFileAttachment(att);
    }
}

//Send
if (SaveSent) 
{
    email.SendAndSaveCopy(SearchFolderByName(service,"SentItems",mailbox));
} else {
    email.Send();
}
}
catch (Exception e) 
{
Success = false;
ErrorMessage = e.ToString();
}

CodePudding user response:

Importance property in EmailMessage class is Enum so you have to use importance enum.

email.Importance = Importance.High // Or Importance.Low or Importance.Normal
  • Related