2017-09-13 8 views
0

私はAzureブロブに文書をアップロードする剣道アップロードを持っています。私は剣道と一緒にアップロードされた書類を添付して電子メールで送付したい。 は、ここで私が試したものです: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

、私はあなたがAzureブロブストレージにファイルをアップロードするために、ストリームを使用しました。

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

いいえ、データは表示されません。 –

関連する問題