2017-09-13 114 views
0

我有一个Kendo上传将文件上传到Azure blob。我想发送一封附有Kendo上传文件的附件并发送电子邮件。 这里是我的尝试:SendGridMessage带附件的电子邮件

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 }); 

      } 

后来,我得到一个错误,该文件路径是无效的。我能做什么?

回答

1

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

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

所以你将面临的文件路径是无效的错误。细节错误就像如下:

enter image description here

此外,我发现你使用流上传文件到蔚蓝的Blob存储。

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

所以我建议你可以改变下面的代码,它会工作得很好。

  myMessage.From = new MailAddress("[email protected]", "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 = "[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(); 
     }   
    } 

结果:

enter image description here

+0

事实上,没有异常抛出,但仍然没有邮件... –

+0

你有没有检查这个[URL](https://app.sendgrid.com/ email_activity)?它有发送电子邮件日志吗? –

+0

不,不显示数据。 –