Reputation: 253
I have a Kendo Upload which uploads documents to an Azure blob. I want to send and e-mail with an attachement with a document uploaded with Kendo. Here is what I tried:
System.Web.HttpPostedFileBase doc;
string filepath;
string uniqueBlobName;
public ActionResult UploadRegistry()
{
doc = Request.Files["Registry"];
var inputstream = doc.InputStream;
var filename = doc.FileName;
var doctype = doc.ContentType;
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureStorageConnection"].ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("registrynumber");
container.CreateIfNotExists();
var permission = container.GetPermissions();
uniqueBlobName = string.Format("Document/{0}", filename);
CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = doctype;
blob.UploadFromStream(inputstream);
filepath = blob.Uri.AbsoluteUri;
TempData["Document"] = doc.FileName;
TempData["Document1"] = filepath;
string address ="[email protected]";
var credentials = new NetworkCredential("username", "password");
var myMessage = new SendGridMessage();
// Add the message properties.
myMessage.From = new MailAddress("[email protected]", "Test");
myMessage.AddAttachment(filepath);
myMessage.AddTo(address);
myMessage.Subject = "Document";
var transportWeb = new Web(credentials);
var x = transportWeb.DeliverAsync(myMessage);
return Json(new { success = true });
}
Later, I get an error that the filepath is invalid. What can I do?
Upvotes: 0
Views: 183
Reputation: 27972
According to your code and description, I found you directly use the uploaded path(url) as the AddAttachment parameter.
As far as I know, the SendGridMessage.AddAttachment method doesn't support directly send the email with online resources.
So you will face the filepath is invalid error. The details error is like as below:
Besides, I found you use stream to upload file to the azure blob storage.
The SendGridMessage.AddAttachment method also support add stream as its parameters.
So I suggest you could change the codes as below, it will work well.
myMessage.From = new MailAddress("[email protected]", "Test");
myMessage.AddAttachment(inputstream,doc.FileName);
myMessage.AddTo(address);
myMessage.Subject = "Document";
var transportWeb = new Web(credentials);
My test codes is like below:
Notice: I directly read file stream as the parameter.
static void ExecuteAsync(Customer customer)
{
using (Stream s1 = File.OpenRead(@"D:\toekn.txt"))
{
var storageAccount = CloudStorageAccount.Parse("connectionstring");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists();
var permission = container.GetPermissions();
CloudBlockBlob blob = container.GetBlockBlobReference("aaaa.txt");
blob.UploadFromStream(s1);
string filepath = blob.Uri.AbsoluteUri;
var credentials = new NetworkCredential("username", "password");
var myMessage = new SendGridMessage();
string address = "[email protected]";
// Add the message properties.
myMessage.From = new MailAddress("[email protected]", "Example User");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.Text = "and easy to do anywhere, even with C#";
// myMessage.AddAttachment(s1, "11.txt");
myMessage.AddAttachment(filepath);
myMessage.AddTo(address);
myMessage.Subject = "Document";
var transportWeb = new Web(credentials);
transportWeb.DeliverAsync(myMessage).Wait();
}
}
Result:
Upvotes: 1