简体   繁体   English

带附件的SendGridMessage电子邮件

[英]SendGridMessage E-mails with Attachement

I have a Kendo Upload which uploads documents to an Azure blob. 我有一个Kendo Upload,它可以将文档上传到Azure blob。 I want to send and e-mail with an attachement with a document uploaded with Kendo. 我想发送和发送带有通过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 ="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 });

            }

Later, I get an error that the filepath is invalid. 后来,我得到一个错误,指出文件路径无效。 What can I do? 我能做什么?

According to your code and description, I found you directly use the uploaded path(url) as the AddAttachment parameter. 根据您的代码和描述,我发现您直接使用上载的路径(url)作为AddAttachment参数。

As far as I know, the SendGridMessage.AddAttachment method doesn't support directly send the email with online resources. 据我所知,SendGridMessage.AddAttachment方法不支持直接使用在线资源发送电子邮件。

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. 此外,我发现您使用流将文件上传到azure blob存储。

The SendGridMessage.AddAttachment method also support add stream as its parameters. SendGridMessage.AddAttachment方法还支持添加流作为其参数。

So I suggest you could change the codes as below, it will work well. 因此,我建议您可以如下更改代码,它将很好地工作。

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

Result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM