Mimecast API - 发送附件
Mimecast API - Send Attachment
我们正在使用 Mimecast API 从本地程序发送电子邮件/附件。我们的电子邮件确实使用 Mimecast 的示例代码(没有附件)发送出去,但我们在发送附件时遇到了问题。 (我确实检查了 Mimecast 的 API 论坛,但没有像这样的标记问题,所以我想我会试试。)
有问题的附件是一个 PDF 文件,这是我们正在使用的代码。我们看到远程服务器强行关闭连接的异常。我们尝试添加:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
但我们得到了相同的结果。我们尝试调整 Content-Type 但无济于事(除了 octet-stream 之外的任何其他方法都会使 Mimecast 抛出不受支持的对象错误。)我们使用的来自 Mimecast 的 API 文档的代码是:
static void uploadAttachmentToMimecast()
{
//Setup required variables
string baseUrl = "https://us-api.mimecast.com";
string uri = "/api/file/file-upload";
String accessKey = "accesskeyhere";
String secretKey = "secretkeyhere";
String appId = "ourappId";
String appKey = "ourappKey";
string fileName = @"C:\Path\Hello.pdf";
// thought I'd try this but it isn't working either... I was hoping it was an SSL thing.
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//Generate request header values
string hdrDate = System.DateTime.Now.ToUniversalTime().ToString("R");
string requestId = System.Guid.NewGuid().ToString();
//Generate sha256 of the file to upload
string sha256 = generateSha256(fileName);
//Get the file size of the file to upload
long fileSize = new System.IO.FileInfo(fileName).Length;
//Create the HMAC SHA1 of the Base64 decoded secret key for the Authorization header
System.Security.Cryptography.HMAC h = new System.Security.Cryptography.HMACSHA1(System.Convert.FromBase64String(secretKey));
//Use the HMAC SHA1 value to sign the hdrDate + ":" requestId + ":" + URI + ":" + appkey
byte[] hash = h.ComputeHash(System.Text.Encoding.Default.GetBytes(hdrDate + ":" + requestId + ":" + uri + ":" + appKey));
//Build the signature to be included in the Authorization header in your request
string signature = "MC " + accessKey + ":" + System.Convert.ToBase64String(hash);
//Build Request
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(baseUrl + uri);
request.Method = "POST";
request.ContentType = "application/octet-stream";
//Add Headers
request.Headers[System.Net.HttpRequestHeader.Authorization] = signature;
request.Headers.Add("x-mc-date", hdrDate);
request.Headers.Add("x-mc-req-id", requestId);
request.Headers.Add("x-mc-app-id", appId);
request.Headers.Add("x-mc-arg", "{'data': [{'sha256': '" + sha256 + "', 'fileSize': " + fileSize.ToString() + "}]}");
//Add request body
//Create and write data to stream
var payload = System.IO.File.ReadAllBytes(fileName);
System.IO.Stream stream = request.GetRequestStream();
stream.Write(payload, 0, payload.Length);
stream.Close();
//Send Request
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
//Output response to console
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
string responseBody = "";
string temp = null;
while ((temp = reader.ReadLine()) != null)
{
responseBody += temp;
};
System.Console.WriteLine(responseBody);
System.Console.ReadLine();
}
public static string generateSha256(string fileName)
{
using (System.IO.FileStream post_stream = System.IO.File.OpenRead(fileName))
{
System.Security.Cryptography.SHA256Managed sha = new System.Security.Cryptography.SHA256Managed();
byte[] file_hash = sha.ComputeHash(post_stream);
return System.BitConverter.ToString(file_hash).Replace("-", System.String.Empty);
}
}
抛出实际错误的行是:
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
如果有人有使用 Mimecast API 的经验并且有任何想法可以帮助我们阐明这一点,我们将不胜感激。谢谢
如果将来有人遇到这种情况,在用头撞墙之前,请在您的 Mimecast 服务 API 门户中重新生成您的服务帐户密钥(并撤销之前的密钥。)这似乎由于某种原因成为问题..
我们正在使用 Mimecast API 从本地程序发送电子邮件/附件。我们的电子邮件确实使用 Mimecast 的示例代码(没有附件)发送出去,但我们在发送附件时遇到了问题。 (我确实检查了 Mimecast 的 API 论坛,但没有像这样的标记问题,所以我想我会试试。)
有问题的附件是一个 PDF 文件,这是我们正在使用的代码。我们看到远程服务器强行关闭连接的异常。我们尝试添加:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
但我们得到了相同的结果。我们尝试调整 Content-Type 但无济于事(除了 octet-stream 之外的任何其他方法都会使 Mimecast 抛出不受支持的对象错误。)我们使用的来自 Mimecast 的 API 文档的代码是:
static void uploadAttachmentToMimecast()
{
//Setup required variables
string baseUrl = "https://us-api.mimecast.com";
string uri = "/api/file/file-upload";
String accessKey = "accesskeyhere";
String secretKey = "secretkeyhere";
String appId = "ourappId";
String appKey = "ourappKey";
string fileName = @"C:\Path\Hello.pdf";
// thought I'd try this but it isn't working either... I was hoping it was an SSL thing.
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//Generate request header values
string hdrDate = System.DateTime.Now.ToUniversalTime().ToString("R");
string requestId = System.Guid.NewGuid().ToString();
//Generate sha256 of the file to upload
string sha256 = generateSha256(fileName);
//Get the file size of the file to upload
long fileSize = new System.IO.FileInfo(fileName).Length;
//Create the HMAC SHA1 of the Base64 decoded secret key for the Authorization header
System.Security.Cryptography.HMAC h = new System.Security.Cryptography.HMACSHA1(System.Convert.FromBase64String(secretKey));
//Use the HMAC SHA1 value to sign the hdrDate + ":" requestId + ":" + URI + ":" + appkey
byte[] hash = h.ComputeHash(System.Text.Encoding.Default.GetBytes(hdrDate + ":" + requestId + ":" + uri + ":" + appKey));
//Build the signature to be included in the Authorization header in your request
string signature = "MC " + accessKey + ":" + System.Convert.ToBase64String(hash);
//Build Request
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(baseUrl + uri);
request.Method = "POST";
request.ContentType = "application/octet-stream";
//Add Headers
request.Headers[System.Net.HttpRequestHeader.Authorization] = signature;
request.Headers.Add("x-mc-date", hdrDate);
request.Headers.Add("x-mc-req-id", requestId);
request.Headers.Add("x-mc-app-id", appId);
request.Headers.Add("x-mc-arg", "{'data': [{'sha256': '" + sha256 + "', 'fileSize': " + fileSize.ToString() + "}]}");
//Add request body
//Create and write data to stream
var payload = System.IO.File.ReadAllBytes(fileName);
System.IO.Stream stream = request.GetRequestStream();
stream.Write(payload, 0, payload.Length);
stream.Close();
//Send Request
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
//Output response to console
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
string responseBody = "";
string temp = null;
while ((temp = reader.ReadLine()) != null)
{
responseBody += temp;
};
System.Console.WriteLine(responseBody);
System.Console.ReadLine();
}
public static string generateSha256(string fileName)
{
using (System.IO.FileStream post_stream = System.IO.File.OpenRead(fileName))
{
System.Security.Cryptography.SHA256Managed sha = new System.Security.Cryptography.SHA256Managed();
byte[] file_hash = sha.ComputeHash(post_stream);
return System.BitConverter.ToString(file_hash).Replace("-", System.String.Empty);
}
}
抛出实际错误的行是:
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
如果有人有使用 Mimecast API 的经验并且有任何想法可以帮助我们阐明这一点,我们将不胜感激。谢谢
如果将来有人遇到这种情况,在用头撞墙之前,请在您的 Mimecast 服务 API 门户中重新生成您的服务帐户密钥(并撤销之前的密钥。)这似乎由于某种原因成为问题..