none
Cannot send email with attachment using Exchange Web Services Managed API 1.2.1 RRS feed

  • Question

  • public string SendEmail(MailMessage message)
            {
                string result = string.Empty;
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                service.UseDefaultCredentials = true;
                service.AutodiscoverUrl(message.From);
                TraceListener listener = new TraceListener();
                service.TraceListener = listener;
                service.TraceEnabled = true;
                service.TraceFlags = TraceFlags.EwsResponse;
                EmailMessage exchangeMessage = new EmailMessage(service);
                try
                {
                    foreach (string toRecipient in message.To)
                    {
                        exchangeMessage.ToRecipients.Add(toRecipient);
                    }
                    if (message.From != null)
                    {
                        exchangeMessage.From = message.From;
                    }
                    foreach (string ccRecipient in message.CC)
                    {
                        exchangeMessage.CcRecipients.Add(ccRecipient);
                    }
                    foreach (string bccrecipient in message.BCC)
                    {
                        exchangeMessage.BccRecipients.Add(bccrecipient);
                    }
                    exchangeMessage.Body = message.Body;
                    exchangeMessage.Subject = message.Subject;
                    foreach (EmailAttachment attachment in message.Attachments)
                    {
                        exchangeMessage.Attachments.AddFileAttachment(attachment.Filepath);
                    }
                    exchangeMessage.SendAndSaveCopy();
                    return listener.Message;
                }
                catch (Microsoft.Exchange.WebServices.Data.ServiceResponseException ex)
                {
                    if (ex.ErrorCode.ToString() == "ErrorSendAsDenied")
                    {
                        return "You do not have permission to send email from this mailbox";
                    }
                    else
                    {
                        string errorMessage = string.Empty;
                        if (message.Attachments.Count > 0)
                        {
                            errorMessage = message.Attachments.FirstOrDefault().Filepath + ":  ";
                        }
                        return errorMessage + ex.Message.ToString();
                    }
                }
                catch (Exception e)
                {
                    string errorMessage = string.Empty;
                    if (message.Attachments.Count > 0)
                    {
                        errorMessage = message.Attachments.FirstOrDefault().Filepath + ":  ";
                    }
                    return errorMessage + e.Message + e.InnerException;
                }
            }
        }

    I had been using EWS Managed API 1.0 with Exchange 2007 with no issues for a couple years.  Our company decided to migrate to Exchange 2010.  To allow functionality to continue, I needed to upgrade to EWS Managed API 1.2.1, which is the latest released version of EWS Managed API.  Since the upgrade, I can send messages without attachments but I am unable to send messages with attachments.  Using the Visual Studio WCF Test Client, I receive a 501:  Not Implemented error on a small 2KB test attachment.  The attachment I'm trying to send in my business scenario is 131 KB and it times out on the web server.

    I've attached the code below for your review.

    • Moved by Bob Shen Friday, July 13, 2012 9:21 AM (From:Visual C# General)
    Thursday, July 12, 2012 10:10 PM

All replies