Kim Bay Andersen
Kim Bay Andersen

Reputation: 279

EWS fileAttachment.ContentType always null?

I'm currently working on this windows service that is pulling data from an exchange web service. Everything works like a charm, but i am not able to get the contentType / Mime-type of the mail attachments.

Can anyone point me in the direction where to find the problem?

 protected override void OnStart(string[] args)
    {
        Svc = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        Svc.Credentials = new WebCredentials("[email protected]", "kba123test");
        Svc.TraceEnabled = true;
        Svc.TraceFlags = TraceFlags.All;
        Svc.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback);

        streamsubscript = Svc.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);

        connection = new StreamingSubscriptionConnection(Svc, 30);
        connection.AddSubscription(streamsubscript);
        connection.OnNotificationEvent += OnNotificationEvent;
        connection.OnSubscriptionError += ConnectionOnOnSubscriptionError;
        connection.OnDisconnect += ConnectionOnOnDisconnect;
        connection.Open();
    }

private void OnNotificationEvent(object sender, NotificationEventArgs args)
    {
        foreach (var notification in args.Events)
        {
            if (notification.EventType != EventType.NewMail) continue;

            var itemEvent = (ItemEvent)notification;
            mail = GetMailByID(itemEvent.ItemId.UniqueId);

            List<int> ls = ExtractTicketNumber(mail.Subject);

            if (ls.Count > 0)
            {
                foreach (int i in ls)
                {
                    bool exist = CheckTicketExistance(i);

                    if (exist)
                    {
                        SaveMailToEvent(mail, i);
                    }
                }
            }
            else
            {
                SaveMailToTicket(mail);
            }
        }
    }   


 private void SaveMailToEvent(EmailMessage mail, int ticketId)
    {
        string[] mailAddr = mail.From.Address.Split(new char[] { '@' });
        string user = mailAddr[0].ToUpper();
        var eventId = 0;

        using (var conn = new SqlConnection(connectionStr))
        {
            conn.Open();
             eventId = conn.Query<int>("INSERT INTO THGITTicketEvents (EventStatus, CreatedBy, Comment, RefTicketRecID, FileAttached)" +
                        "Values(@EventStatus, @CreatedBy, @Comment, @RefTicketRecID, @FileAttached)" +
                        "SELECT CAST( @@IDENTITY AS int)",
                         new
                         {
                             EventStatus = 9,
                             CreatedBy = user,
                             Comment = mail.Body.Text,
                             RefTicketRecID = ticketId,
                             FileAttached = (mail.HasAttachments) ? 1 : 0
                         }).Single();
        }

        if (mail.HasAttachments)
        {
            using (var conn = new SqlConnection(connectionStr))
            {
                conn.Open();

                foreach (var i in mail.Attachments)
                {
                    FileAttachment fileAttachment = i as FileAttachment;
                    if (fileAttachment != null)
                    {
                        fileAttachment.Load();
                        string test = fileAttachment.ContentType;
                        conn.Query("INSERT INTO THGITAttachments (FileContent, CreatedBy, FileName, RefEventRecID, MimeType)" +
                                   "Values(@FileContent, @CreatedBy, @FileName, @RefEventRecID, @MimeType)",
                            new
                            {
                                FileContent = fileAttachment.Content,
                                CreatedBy = user,
                                FileName = fileAttachment.Name,
                                RefEventRecID = eventId,
                                MimeType = fileAttachment.ContentType,
                            });
                    }
                }
            }
        }

    }

Upvotes: 1

Views: 1414

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17692

Check with EWSEditor to see if the attachments actually have that property set.

Upvotes: 1

Related Questions