c# - 使用 C# 发送电子邮件的警报提醒时出错
问题描述
我正在尝试使用 C# 代码从应用程序发送提醒警报以及电子邮件,但我收到错误消息:“在标头名称中发现无效字符。” 在这一行 email.Headers.Add("Follow up by:", string.Format("{0: dd MM yyyy HH:mm:ss zz}", ReplyDate)); 下面是我的代码:
public bool SendEmail(EmailInformation emailInformation, ProcessInformation processInformation)
{
try
{
string emailBody = GenerateEmail(emailInformation, processInformation);
if (processInformation.Success)
{
string emailSubject = _settings.DefaultEmailSubject;
EmailEngine engine = new EmailEngine(_settings);
engine.Send(emailInformation.RecipientEmailAddress, emailBody, emailSubject); <-- this method is showing below
DateTime emailSent = DateTime.Now;
UpdateEmailSentInformation(emailInformation.CompanyId, emailInformation.SupervisorId, emailSent, processInformation);
if (processInformation.Success)
{
ArchiveEmail(emailInformation, emailBody, processInformation);
}
}
}
catch (Exception ex)
{
processInformation.AddContent("Exception encountered " + ex.Message);
processInformation.Success = false;
}
return processInformation.Success;
}
///////////////////////////////////////// ////////////////////////
public void Send(string recipientEmail, string emailBody, string emailSubject)
{
MailMessage email = new MailMessage();
SmtpClient smtpServer = new SmtpClient(_settings.EmailServer);
DateTime ReplyDate = DateTime.Now.AddDays(5);
//email.Headers.Add("X-Message-Flag", "Follow up");
email.Headers.Add("Follow up by:", string.Format("{0: dd MM yyyy HH:mm:ss zz}", ReplyDate)); <-- here is where the error is throwing
email.From = new MailAddress(_settings.SenderEmail);
if (!string.IsNullOrEmpty(_settings.TestingEmail))
{
email.To.Add(_settings.TestingEmail);
}
else
{
email.To.Add(recipientEmail);
}
email.Subject = emailSubject;
email.IsBodyHtml = true;
email.Body = emailBody;
smtpServer.Send(email);
}
}
我也试过这个
email.Headers.Add("Follow up by:", string.Format("{0:g}", ReplyDate));
我在这里做错了什么?
谢谢!
解决方案
推荐阅读
- postgresql - Clojure java.jdbc return-keys 选项只返回一行
- python-2.7 - pip install --upgrade fitsio:ValueError:无法配置cfitsio 3470
- triggers - 当父项上的字段更改时触发使用父值更新子项
- android - 使用黄油刀无法正确找到初始化视图
- python - 如何在 Python 中指定线性系统的时间延迟?
- python - Python OpenCV 将平面 YUV 4:2:0 图像转换为 RGB -- YUV 数组格式
- javascript - 如何让我的简单弹出式网页响应式?
- python - 使用正则表达式作为 URL 地址?
- python - 将多个表从 postgres 连接到 python
- excel - 基于一定数量的列/行的范围