如何在 .Net Core 中发送附有 pdf 文件的 AWS 原始电子邮件?
How to Send an AWS Raw Email with pdf file attached to it in .Net Core?
这是我目前所做的
public static async Task<bool> SendEmailWithAttachmentAsync(EmailObj emlData)
{
var replyToAddresses = new List<string>();
replyToAddresses.Add("Email");
try
{
var options = new CredentialProfileOptions
{
AccessKey = "1234",
SecretKey = "5678"
};
var profile = new CredentialProfile("default", options);
var sharedFile = new SharedCredentialsFile();
sharedFile.RegisterProfile(profile);
using (var client = new
AmazonSimpleEmailServiceClient(RegionEndpoint.APSoutheast2))
{
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "Hello World. Please view the attachment.";
bodyBuilder.TextBody = "Hello World. Please view the attachment.";
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress("Name", "Email"));
mimeMessage.To.Add(new MailboxAddress("Recipient Name", "Email"));
mimeMessage.Subject = "Hello World";
//using (var messageStream = new MemoryStream())
//{
MemoryStream messageStream = new MemoryStream();
emlData.Report.ExportToPdf(messageStream);
messageStream.Seek(0, System.IO.SeekOrigin.Begin);
bodyBuilder.Attachments.Add("File.pdf", messageStream, new
ContentType("application", "pdf"));
mimeMessage.WriteTo(messageStream);
var sendRequest = new SendRawEmailRequest {
Source = senderAddress,
Destinations = new List<string> { "Email" },
RawMessage = new RawMessage(messageStream)
};
var response = await client.SendRawEmailAsync(sendRequest);
if (response.MessageId != null)
{
messageStream.Close();
messageStream.Flush();
}
//}
return true;
}
}
catch (Exception err)
{
return false;
}
}
我试过将代码放入 using(memoryStream){} 但似乎不起作用。
emlData.Report.ExportToPdf(消息流);此处的报告是来自 DevExpress 报告的 XtraReport,代码工作正常,因为我已成功完成下载部分,但不知何故发送电子邮件不起作用。
Any help would be appreciated, thank you. I've attached an image of what I got in my email.
我已经按照 here 中的示例进行操作,并且能够使用以下代码使用 MimeKit 发送带有 PDF 附件的电子邮件:
public static void ReadPdfIntoMemoryStream(MemoryStream memoryStream)
{
byte[] fileContents = File.ReadAllBytes(filename);
memoryStream.Write(fileContents, 0, fileContents.Length);
}
public static async Task<bool> SendEmailWithAttachmentAsync()
{
try
{
using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1))
{
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "Hello World. Please view the attachment.";
bodyBuilder.TextBody = "Hello World. Please view the attachment.";
using (MemoryStream ms = new MemoryStream())
{
ReadPdfIntoMemoryStream(ms);
ms.Seek(0, System.IO.SeekOrigin.Begin);
bodyBuilder.Attachments.Add("Test.pdf", ms, new ContentType("application", "pdf"));
}
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress("Name", senderAddress));
mimeMessage.To.Add(new MailboxAddress("Recipient Name", destinationAddress));
mimeMessage.Subject = "Hello World";
mimeMessage.Body = bodyBuilder.ToMessageBody();
using (var messageStream = new MemoryStream())
{
await mimeMessage.WriteToAsync(messageStream);
var sendRequest = new SendRawEmailRequest { RawMessage = new RawMessage(messageStream) };
var response = await client.SendRawEmailAsync(sendRequest);
Console.WriteLine(response.HttpStatusCode);
}
return true;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
return false;
}
}
ReadPdfIntoMemoryStream
方法模拟您的 emlData.Report.ExportToPdf
并将 PDF 加载到内存流中。您可以将此内存流直接传递给 bodyBuilder 附件。不要重复使用此内存流来发送原始消息。新建一个。
这是我目前所做的
public static async Task<bool> SendEmailWithAttachmentAsync(EmailObj emlData)
{
var replyToAddresses = new List<string>();
replyToAddresses.Add("Email");
try
{
var options = new CredentialProfileOptions
{
AccessKey = "1234",
SecretKey = "5678"
};
var profile = new CredentialProfile("default", options);
var sharedFile = new SharedCredentialsFile();
sharedFile.RegisterProfile(profile);
using (var client = new
AmazonSimpleEmailServiceClient(RegionEndpoint.APSoutheast2))
{
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "Hello World. Please view the attachment.";
bodyBuilder.TextBody = "Hello World. Please view the attachment.";
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress("Name", "Email"));
mimeMessage.To.Add(new MailboxAddress("Recipient Name", "Email"));
mimeMessage.Subject = "Hello World";
//using (var messageStream = new MemoryStream())
//{
MemoryStream messageStream = new MemoryStream();
emlData.Report.ExportToPdf(messageStream);
messageStream.Seek(0, System.IO.SeekOrigin.Begin);
bodyBuilder.Attachments.Add("File.pdf", messageStream, new
ContentType("application", "pdf"));
mimeMessage.WriteTo(messageStream);
var sendRequest = new SendRawEmailRequest {
Source = senderAddress,
Destinations = new List<string> { "Email" },
RawMessage = new RawMessage(messageStream)
};
var response = await client.SendRawEmailAsync(sendRequest);
if (response.MessageId != null)
{
messageStream.Close();
messageStream.Flush();
}
//}
return true;
}
}
catch (Exception err)
{
return false;
}
}
我试过将代码放入 using(memoryStream){} 但似乎不起作用。 emlData.Report.ExportToPdf(消息流);此处的报告是来自 DevExpress 报告的 XtraReport,代码工作正常,因为我已成功完成下载部分,但不知何故发送电子邮件不起作用。
Any help would be appreciated, thank you. I've attached an image of what I got in my email.
我已经按照 here 中的示例进行操作,并且能够使用以下代码使用 MimeKit 发送带有 PDF 附件的电子邮件:
public static void ReadPdfIntoMemoryStream(MemoryStream memoryStream)
{
byte[] fileContents = File.ReadAllBytes(filename);
memoryStream.Write(fileContents, 0, fileContents.Length);
}
public static async Task<bool> SendEmailWithAttachmentAsync()
{
try
{
using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1))
{
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "Hello World. Please view the attachment.";
bodyBuilder.TextBody = "Hello World. Please view the attachment.";
using (MemoryStream ms = new MemoryStream())
{
ReadPdfIntoMemoryStream(ms);
ms.Seek(0, System.IO.SeekOrigin.Begin);
bodyBuilder.Attachments.Add("Test.pdf", ms, new ContentType("application", "pdf"));
}
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress("Name", senderAddress));
mimeMessage.To.Add(new MailboxAddress("Recipient Name", destinationAddress));
mimeMessage.Subject = "Hello World";
mimeMessage.Body = bodyBuilder.ToMessageBody();
using (var messageStream = new MemoryStream())
{
await mimeMessage.WriteToAsync(messageStream);
var sendRequest = new SendRawEmailRequest { RawMessage = new RawMessage(messageStream) };
var response = await client.SendRawEmailAsync(sendRequest);
Console.WriteLine(response.HttpStatusCode);
}
return true;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message);
return false;
}
}
ReadPdfIntoMemoryStream
方法模拟您的 emlData.Report.ExportToPdf
并将 PDF 加载到内存流中。您可以将此内存流直接传递给 bodyBuilder 附件。不要重复使用此内存流来发送原始消息。新建一个。