使用C#发送电子邮件的警报提醒时出错

时间:2018-09-08 19:08:25

标签: c# asp.net

我正在尝试使用C#代码从应用程序发送提醒警报以及电子邮件,但出现错误:“在标头名称中发现无效字符。”在此行中email.Headers.Add(“跟进:”,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));

我在这里做错了什么?

谢谢!

0 个答案:

没有答案