首页 > 解决方案 > 使用 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));

我在这里做错了什么?

谢谢!

标签: c#asp.net

解决方案


推荐阅读