2017-07-28 15 views
0

私はヘルプデスクアプリケーションを作成しています。ユーザーは電子メールでチケットをログに記録できますので、EWS APIを使用して電子メールを受け取って件名と本文をプレーンテキストデータベース。EWS添付ファイルとプレーンテキストの本文を取得するC#

添付ファイルを保存したいのですが、BodyTypeをテキストにすることはできません。添付ファイルを保存するにはHTMLでなければなりませんが、本文をデータベースに保存する際にはプレーンテキストである必要があります。

このリンクのプレーンテキストを保存するコードを取得しましたEWS body plain text。私は添付ファイルに興味のあるものは見つけられないようです。

私のコードは、これまで私が正しくあなたの問題を理解

itempropertyset.RequestedBodyType = BodyType.Text; 
email.Load(); 

bb = email.Body.Text; 

try 
{// Add The new ticket to the DB 
    string stmt = "INSERT INTO Tickets(Subject, StatusId, PriorityId, CreatedByUserId, CreatedDateTime, ClientDescription, TicketTypeId) VALUES (@Subject, @sId, @pId, @cId, @ctd, @cd, @tId); Select SCOPE_IDENTITY() as Id"; 
    cmd = new SqlCommand(stmt, conn); 
    cmd.Parameters.AddWithValue("@Subject", ss); //Subject Line from email 
    cmd.Parameters.AddWithValue("@sId", 1); //Default to Open 
    cmd.Parameters.AddWithValue("@pId", 6); //Default Prioirity to Normal 
    cmd.Parameters.AddWithValue("@cId", UserId); //GetTheUserId 
    cmd.Parameters.AddWithValue("@ctd", DateTime.Now); 
    cmd.Parameters.AddWithValue("@cd", bb); //Subject Line from email 
    cmd.Parameters.AddWithValue("@sId", 2); //Default to Email 

    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataTable dte = new DataTable("res"); 
    da.Fill(dte); 
    if (dte.Rows.Count > 0) 
    { 
     decimal tId = Convert.ToDecimal(dte.Rows[0]["Id"]); 
     try 
     { //Add the new activity for the ticket 
      string stmnt = "INSERT INTO Activities(TicketId, ActivityTypeId, CreatedByUserId, Subject, ActivityContent, CreatedDateTime) VALUES (@tId, @atId, @cId, @Subject, @ac, @ctd); Select SCOPE_IDENTITY() as Id";; 
      SqlCommand cmde = new SqlCommand(stmnt, conn); 
      cmde.Parameters.AddWithValue("@tId", tId); //Subject Line from email 
      cmde.Parameters.AddWithValue("@atId", 3); //Default to Email 
      cmde.Parameters.AddWithValue("@cId", UserId); //GetTheUserId 
      cmde.Parameters.AddWithValue("@Subject", ss); 
      cmde.Parameters.AddWithValue("@ac", bb); 
      cmde.Parameters.AddWithValue("@ctd", DateTime.Now); 
      SqlDataAdapter sdtc = new SqlDataAdapter(cmde); 
      DataTable sdta = new DataTable("ActivityTable"); 
      sdtc.Fill(sdta); 
      if (sdta.Rows.Count > 0 && email.HasAttachments == true) 
      { 
       decimal aId = Convert.ToDecimal(sdta.Rows[0]["Id"]); 
       foreach(Microsoft.Exchange.WebServices.Data.Attachment attachment in email.Attachments) 
       { 
        if (attachment is FileAttachment) 
        { 
         fileAttachment = attachment as FileAttachment; 
         fileAttachment.Load("\\\\Helpdesk\\Attachments\\" + attachment.Name); 
        } 
        else 
        { 
         ItemAttachment itemAttachment = attachment as ItemAttachment; 
         itemAttachment.Load(); 
        } 
        string path = "\\\\Helpdesk\\Attachments\\" + attachment.Name; 
        string stmtattach = "INSERT INTO Attachment(TicketId, ActivityId, AttachmentTypeId, CreatedByUserId, CreatedDateTime, AttachmentPath) VALUES (@tId, @aId, @atId, @cId, @cdt, @path)"; 
        SqlCommand comd = new SqlCommand(stmtattach, conn); 
        comd.Parameters.AddWithValue("@tId", tId); 
        comd.Parameters.AddWithValue("@aId", aId); 
        comd.Parameters.AddWithValue("@atId", 1); 
        comd.Parameters.AddWithValue("@cId", UserId); 
        comd.Parameters.AddWithValue("@cdt", DateTime.Now); 
        comd.Parameters.AddWithValue("@path", path); 
        SqlDataAdapter ada = new SqlDataAdapter(comd); 
        DataTable adte = new DataTable("attachment"); 
        ada.Fill(adte); 
       } 

      } 
      SendMail(senderName, email.Sender.Address, "TICKET?" + tId.ToString() + "#" + ss, "Original Message: " + bb + "Your Ticket has successfully been added to the helpdesk your ticket is " + tId); 
     } 
     catch (System.Exception ex) 
     { 
      Console.WriteLine(ex); 
      lg.AppendLine(" - Unsuccessful adding activity to DB.<br/>"); 
     } 

    } 
} 

答えて

0

場合は、添付ファイルの本文を取得したいです。

これを行うには、Item.TextBodyプロパティを使用する必要があります。

ので、以下のようなもの:ヘルプデスクへ行く添付ファイルのほとんどは、画像やファイルになりますようFileAttachmentについてItemAttachmentが、何のために働くようだ

ItemAttachment itemAttachment = attachment as ItemAttachment; 

itemAttachment.Load(new PropertySet(EmailMessageSchema.TextBody)); 

Console.WriteLine(itemAttachment.Item.TextBody); 
+0

感謝。前もって感謝します。 –

+0

'FileAttachment'でもうまくいくはずです。そうでない場合は、 'fileAttachment.Item.Body.Text' – MadDev

関連する問題