带Attachement的SendGridMessage电子邮件

时间:2017-09-13 06:15:46

标签: c# azure email

我有一个Kendo Upload,可以将文档上传到Azure blob。我想发送电子邮件并附上一份附有Kendo上传文件的附件。 这是我试过的:

   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 ="test.test@test.com";

                var credentials = new NetworkCredential("username", "password");

                var myMessage = new SendGridMessage();

                // Add the message properties.
                myMessage.From = new MailAddress("test@test.com", "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 });

            }

稍后,我收到文件路径无效的错误。我该怎么办?

1 个答案:

答案 0 :(得分:1)

根据您的代码和说明,我发现您直接使用上传的路径(网址)作为AddAttachment参数。

据我所知,SendGridMessage.AddAttachment方法不支持直接发送包含在线资源的电子邮件。

所以你将面对文件路径无效的错误。详细信息错误如下所示:

enter image description here

此外,我发现您使用流将文件上传到azure blob存储区。

SendGridMessage.AddAttachment方法也支持添加流作为参数。

所以我建议您可以更改下面的代码,它会很好用。

         myMessage.From = new MailAddress("test@test.com", "Test");

            myMessage.AddAttachment(inputstream,doc.FileName);
            myMessage.AddTo(address);
            myMessage.Subject = "Document";
            var transportWeb = new Web(credentials);

我的测试代码如下:

注意:我直接读取文件流作为参数。

    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 = "aaaaa@gmail.com";

        // Add the message properties.
        myMessage.From = new MailAddress("bbbbbb@hotmail.com", "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();
        }         
    }

结果:

enter image description here