2017-07-07 5 views
0

GMail APIを使用してGoogle Mailboxにメールを挿入しようとしています。 5メガバイトを超えるサイズのメールをアップロードしたいとします。私はResumableアップロードリクエストを使用しています。Google Mail APIを使用して再開可能アップロード例外:「不正なリクエスト」が発生しました

私は「200 OK」応答を与える再開可能なアップロードを開始する最初のPOST要求を使用していました。

POSTリクエスト:

String postUrl = "https://www.googleapis.com/upload/gmail/v1/users/" + "<username>" + "/messages/send?uploadType=resumable"; 
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(postUrl); 
httpRequest.Headers["Authorization"] = "Bearer " + f_token;// AccessToken; 
httpRequest.Headers["X-Upload-Content-Type"] = "message/rfc822"; 
httpRequest.Headers["X-Upload-Content-Length"] = f_bytes.Length.ToString(); 
httpRequest.Method = "POST"; 
httpRequest.ContentLength = 0; 
var response = (HttpWebResponse)httpRequest.GetResponse(); // 200 OK 

私はEMLをアップロードする場所のURLを取得している応答から。

場所:?https://www.googleapis.com/upload/gmail/v1/users/ /メッセージ/送信uploadType =再開可能&するupload_id = AEnB2UqeNYKVyyQdL07RZcbenWOqY8a2NFVIsQrbA-S-vxwUXC_W4ORQtpPx1HG6tc4Indx8AvqDjwXII3F6OW0G3wsdUMUjHw

私は要求を作成するために、PUT URLとして場所URLを使用EMLファイルをアップロードします。

putUrl = https://www.googleapis.com/upload/gmail/v1/users/<username>/messages/send?uploadType=resumable&upload_id=AEnB2UqeNYKVyyQdL07RZcbenWOqY8a2NFVIsQrbA-S-vxwUXC_W4ORQtpPx1HG6tc4Indx8AvqDjwXII3F6OW0G3wsdUMUjHw"; 
HttpWebRequest httpRequest1 = (HttpWebRequest)WebRequest.Create(postUrl); 
httpRequest1.Method = "PUT"; 
httpRequest1.ContentLength = f_bytes.Length; 
int EndOffset = f_bytes.Length;//5120000;5242880 
httpRequest1.Headers["Content-Range"] = "bytes " + 0 + "-" + EndOffset + "/" + f_bytes.Length; 
httpRequest1.ContentType = "message/rfc822"; 

MemoryStream stream = new MemoryStream(f_bytes); 

System.IO.Stream requestStream = httpRequest1.GetRequestStream(); 
{ 
          stream.CopyTo(requestStream); 
          requestStream.Flush(); 
          requestStream.Close(); 
} 

HttpWebResponse f_webResponse = (HttpWebResponse)httpRequest1.GetResponse(); //Exception 

例外:

The remote server returned an error: (400) Bad Request.

メールボックスの特定のフォルダにEMLファイルをアップロードするsoluionを提案してください。

答えて

0

再開可能アップロードを使用してメールを送信できます。

if (f_MailService == null) 
{ 
    bool isCreated = createMailService(ref f_MailService); 
} 
FileStream fs = new FileStream(@p_EMLPath, FileMode.Open,FileAccess.Read); 

は、メールを送信するためには、HTTPRequestを作成します。

string postUrl = "https://www.googleapis.com/upload/gmail/v1/users/[email protected]/messages/send?uploadType=resumable"; 

HttpWebRequest f_httpRequest = (HttpWebRequest)WebRequest.Create(postUrl); 
f_httpRequest.Headers["X-Upload-Content-Type"] = "message/rfc822"; 
f_httpRequest.Headers["X-Upload-Content-Length"] = fs.Length.ToString(); 

f_httpRequest.Headers["Authorization"] = "Bearer " + f_token; 
f_httpRequest.Method = "POST"; 
//f_httpRequest.ContentLength = 524288; 
f_httpRequest.ContentType = "application/json; charset=UTF-8";//"message/rfc822"; 
f_httpRequest.ContentLength = fs.Length; 
f_httpRequest.Timeout = 6000000; 
f_httpRequest.SendChunked = true; 

最初のPOST要求に対する応答取得:

try 
{ 
    using (Stream f_ObjHttpStream = f_httpRequest.GetRequestStream()) 
    { 

    } 
} 
catch (Exception EX) 
{ 

} 


try 
{ 
    using (var response = (HttpWebResponse)f_httpRequest.GetResponse()) 
    { 
     // data = ReadResponse(response); 
     UploadUrl = response.Headers["Location"].ToString();        

    } 
} 
catch (WebException exception) 
{ 
    using (var response = (HttpWebResponse)exception.Response) 
    { 
     // data = ReadResponse(response); 
    } 
} 

読むEMLは

byte[] Arrbyte = new byte[1024]; 
int ReadByte = 0; 

while (fs.Length > ReadByte) 
{ 
    bool ac = false; 
    int ByteRead = 0; 
    byte[] Data = new byte[4194304]; 
    byte[] LastData; 
    //Read block of bytes from stream into the byte array 
    // if (ReadByte == 0) 
    { 
     ByteRead = fs.Read(Data, 0, Data.Length); 
    } 
    //else 
    { 
     if ((ReadByte + Data.Length) > fs.Length) 
     { 
      //fs.Length - ReadByte- 
      LastData = new byte[fs.Length - ReadByte]; 
      ByteRead = fs.Read(LastData, 0, LastData.Length); 
      CallPUTReq(fs.Length, LastData); 
      ac = true; 
     } 

    } 
    //f_MsgRawStr = Convert.ToBase64String(f_bytes).TrimEnd(padding).Replace('+', '-').Replace('/', '_'); 
    ReadByte = ReadByte + ByteRead; 
    if (ac == false) 
    { 
     CallPUTReq(fs.Length, Data); 
    } 
    //long pos = fs.Seek(0, SeekOrigin.Current); 
    //fs.Position = ReadByte; 
} 

private void CallPUTReq(long p_lenth, byte[] Arrbyte) 
{ 
    try 
    { 
     String postUrl = UploadUrl; //"https://www.googleapis.com/upload/gmail/v1/users/[email protected]/messages/send?uploadType=resumable&upload_id=AEnB2UqZNtZVwWulAOhAVoFp-pZ-vTMcIXOpt_0dH_6jJecpm2Y1MNOGkE6JoDb0kn9Dt4yuHHMZWR--dBncxWQkZctF9h6jiPSL5uJDKeYE9Ut1c7-fImc"; 
     int EndOffset = 0; 
     HttpWebRequest httpRequest1 = (HttpWebRequest)WebRequest.Create(postUrl); 
     httpRequest1.Method = "PUT"; 
     httpRequest1.ContentLength = Arrbyte.Length; 
     if (rangeStartOffset == 0) 
     { 
      EndOffset = Arrbyte.Length - 1; 
     } 
     else 
     { 
      EndOffset = rangeStartOffset + Arrbyte.Length - 1; 
      if (EndOffset > p_lenth) 
      { 

       EndOffset = Convert.ToInt32(p_lenth); 
       httpRequest1.ContentLength = EndOffset - rangeStartOffset; 
      } 

     }//5120000;5242880 
     httpRequest1.Headers["Content-Range"] = "bytes " + rangeStartOffset + "-" + EndOffset + "/" + p_lenth; //"bytes */" + p_lenth; // 
     httpRequest1.ContentType = "message/rfc822"; 
     httpRequest1.Timeout = 6000000; 

     UTF8Encoding encoding = new UTF8Encoding(); 
     Stream stream = httpRequest1.GetRequestStream(); 
     stream.Write(Arrbyte, 0, Arrbyte.Length); 
     stream.Close(); 

     try 
     { 
      using (Stream f_ObjHttpStream = httpRequest1.GetRequestStream()) 
      { 

      } 
     } 
     catch (Exception EX) 
     { 

     } 
     WebResponse response1 = null; 
     try 
     { 
      using (response1 = (HttpWebResponse)httpRequest1.GetResponse()) 
      { 

      } 
     } 
     catch (Exception ex) 
     { 

      // UploadUrl = response1.Headers["Location"].ToString(); 
     } 
     //4194303 
     rangeStartOffset = EndOffset +1; 
    } 
    catch (Exception) 
    { 

    } 
} 
をアップロードする&送信チャンクデータファイル
関連する問題