[英]Send Attachment in Email on Windows Azure with c#
我正在尝试从 AZURE 发送电子邮件。 我成功发送了没有附件的电子邮件。 当我发送带有附件的电子邮件时,我在下载附件时遇到以下问题。
对于附件,我在 blob 上上传 PDF 文件并下载它,然后添加附件。 我的代码如下。
var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();
using (msRead)
{
msRead.Position = 0;
blobread.DownloadToStream(msRead);
objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead, Session["UploadPDFFile"].ToString(), "pdf/application"));
try
{
objSmtpClient.Send(objMailMessgae);
}
catch (Exception ex) {
string s = ex.Message;
}
}
在将 blob 的内容读入其中后,您需要将内存流的位置重置为0
。 所以基本上你的代码是:
var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();
using (msRead)
{
blobread.DownloadToStream(msRead);
msRead.Position = 0;
objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead, Session["UploadPDFFile"].ToString(), "pdf/application"));
try
{
objSmtpClient.Send(objMailMessgae);
}
catch (Exception ex) {
string s = ex.Message;
}
}
试一试。 它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.