使用内存流时,获取对象引用未在 SMTP mail.send() C#.net core API 项目中设置错误
Getting Object Reference Not set Error in SMTP mail.send() C#.net core API project while using memory stream
我有一个 .docx 模板,其中定义了一些动态显示字符串,如下所示
{{FirstName}}, {{ContactNumber}}",{{EmailId}}
现在我需要阅读这个 .docx 文件并且需要将上面的文本替换为等同的文本和
需要将该文档作为附件发送给用户。这就是我想要实现的目标。
到目前为止我写的代码是这样的
string content = System.IO.File.ReadAllText(path);
content.Replace("{{FirstName}}", objUser.FirstName);
content.Replace("{{ContactNumber}}", objUser.Contact);
content.Replace("{{EmailId}}", objUser.EmailId);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine(content);
writer.Flush();
stream.Position = 0;
using (var client = new SmtpClient())
{
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
using (var emailMessage = new MailMessage())
{
System.Net.Mime.ContentType contentType = new
System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var coverAttachment = new Attachment(stream, contentType);
coverAttachment.ContentDisposition.FileName = "CoverLetter.pdf";
emailMessage.Attachments.Add(coverAttachment);
emailMessage.To.Add(new MailAddress(value.toemail));
emailMessage.CC.Add(new
MailAddress("My To Address"));
emailMessage.From = new MailAddress(_configuration["Email:Email"]);
emailMessage.Subject = value.subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = true;
emailMessage.BodyEncoding = Encoding.UTF8;
emailMessage.SubjectEncoding = Encoding.Default;
emailMessage.Priority = MailPriority.High;
emailMessage.ReplyToList.Add(new MailAddress(_configuration["Email:Email"]));
//在调用client.send()
之前
我有一个代码像
client.SendCompleted += (s, e) =>
{
client.Dispose();
emailMessage.Dispose();
writer.Dispose();
};
我有什么地方做错了吗?
两个观察结果:
- 如果您在“using”块中创建了 SmtpClient 和 MailMessage 对象,它会自动调用这些对象的 Dispose 方法。你不需要自己称呼他们。
- SendCompleted 事件在异步电子邮件发送操作完成时发生。
您可能应该使用 SendAsync() 而不是 Send()
更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendcompleted?view=net-5.0
我有一个 .docx 模板,其中定义了一些动态显示字符串,如下所示
{{FirstName}}, {{ContactNumber}}",{{EmailId}}
现在我需要阅读这个 .docx 文件并且需要将上面的文本替换为等同的文本和 需要将该文档作为附件发送给用户。这就是我想要实现的目标。
到目前为止我写的代码是这样的
string content = System.IO.File.ReadAllText(path);
content.Replace("{{FirstName}}", objUser.FirstName);
content.Replace("{{ContactNumber}}", objUser.Contact);
content.Replace("{{EmailId}}", objUser.EmailId);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine(content);
writer.Flush();
stream.Position = 0;
using (var client = new SmtpClient())
{
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
using (var emailMessage = new MailMessage())
{
System.Net.Mime.ContentType contentType = new
System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
var coverAttachment = new Attachment(stream, contentType);
coverAttachment.ContentDisposition.FileName = "CoverLetter.pdf";
emailMessage.Attachments.Add(coverAttachment);
emailMessage.To.Add(new MailAddress(value.toemail));
emailMessage.CC.Add(new
MailAddress("My To Address"));
emailMessage.From = new MailAddress(_configuration["Email:Email"]);
emailMessage.Subject = value.subject;
emailMessage.Body = message;
emailMessage.IsBodyHtml = true;
emailMessage.BodyEncoding = Encoding.UTF8;
emailMessage.SubjectEncoding = Encoding.Default;
emailMessage.Priority = MailPriority.High;
emailMessage.ReplyToList.Add(new MailAddress(_configuration["Email:Email"]));
//在调用client.send()
之前我有一个代码像
client.SendCompleted += (s, e) =>
{
client.Dispose();
emailMessage.Dispose();
writer.Dispose();
};
我有什么地方做错了吗?
两个观察结果:
- 如果您在“using”块中创建了 SmtpClient 和 MailMessage 对象,它会自动调用这些对象的 Dispose 方法。你不需要自己称呼他们。
- SendCompleted 事件在异步电子邮件发送操作完成时发生。 您可能应该使用 SendAsync() 而不是 Send()
更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.sendcompleted?view=net-5.0