【问题标题】:Font size for an email sent using System.Net.Mail.SmtpClient使用 System.Net.Mail.SmtpClient 发送的电子邮件的字体大小
【发布时间】:2013-10-23 16:54:12
【问题描述】:

我有一个经过广泛测试的 ASP.Net Web 应用程序。其中一部分使用 System.Net.Mail.SmtpClient 自动发送电子邮件。在测试期间,从各种 Windows 7 机器上发送了电子邮件,并以合理的字体大小进行了格式化。顺便说一句,我没有在代码中设置字体大小。现在应用程序已投入生产,第一封电子邮件以非常小的字体发送,用户希望增加字体大小。我当然可以对字体大小进行硬编码,但我更愿意了解这里发生了什么。什么决定了字体大小?是 SmtpClient 中的某些内容,还是 SMTP 服务器上的某些设置,还是什么?下面是发送电子邮件的代码。 TemplateEditor 是页面上的 AjaxControlToolkit.HTMLEditor 控件。 AttyParaMessageBody 是包含电子邮件正文的变量。我们的用户使用的是装有 Outlook 2010 的 Windows 7。

string AttyParaMessageBody = TemplateEditor.Content;
LHEmail.SendEmail(LHClassLibrary.LHConfigurationManager.AppSettings["ApprovedNewVersionLHEmailSubject"].ToString(), AttyParaMessageBody, AttyParaAddressees, CurrentLitHoldDetails.ResponsibleAttorney.Email, LHClassLibrary.LHConfigurationManager.AppSettings["EmailCCList"].ToString() + ";" + tbEmails.Text);

public static void SendEmail(string subject, string body, string to, string from, string cc)
    {
        SendEmail(subject, body, to, from, cc, "", "", MailPriority.High);
    }

public static void SendEmail(string subject, string body, string to, string from, string cc, string bcc, string fileName, MailPriority Priority)
    {

        MailMessage msgMail = new MailMessage();
        SmtpClient emailClient = new SmtpClient();
        try
        {
            msgMail = BuildMessage(subject, body, to, cc, bcc, from, fileName, Priority);
            emailClient.Send(msgMail);
        }
        catch (Exception ex)
        {
            string exception = ex.ToString();
        }
        finally
        {
            msgMail.Dispose();
        }
    }

private static MailMessage BuildMessage(string subject, string body, string to, string cc, string bcc, string from, string fileName, MailPriority Priority)
    {
        MailMessage msgMail = new MailMessage();

        if (!to.Equals(string.Empty))
        {
            //format emails for .NET 4.0 version
            string[] toAddressList = to.Split(';');

            //Loads the To address field 
            foreach (string toaddress in toAddressList)
            {
                if (toaddress.Length > 0)
                {
                    msgMail.To.Add(toaddress);
                }
            }

            //optional args
            //format emails for .NET 4.0 version
            if (!cc.Equals(string.Empty))
            {
                string[] ccAddressList = cc.Split(';');

                //Loads the Cc address field 
                foreach (string ccaddress in ccAddressList)
                {
                    if (ccaddress.Length > 0)
                    {
                        msgMail.CC.Add(ccaddress);
                    }
                }
            }
            if (!bcc.Equals(string.Empty))
            {
                string[] bccAddressList = bcc.Split(';');

                //Loads the Bcc address field 
                foreach (string bccaddress in bccAddressList)
                {
                    if (bccaddress.Length > 0)
                    {
                        msgMail.Bcc.Add(bccaddress);
                    }
                }
            }
            if (!fileName.Equals(string.Empty))
                msgMail.Attachments.Add(new Attachment(fileName));

            msgMail.Priority = Priority;
            msgMail.From = ((from == null) || (from.Equals(string.Empty))) ? new MailAddress("LitHold@kramerlevin.com", "Litigation Hold") : new MailAddress(from, "Litigation Hold");
            msgMail.Subject = subject;
            msgMail.Body = body;
            msgMail.IsBodyHtml = true;

        }
        else { throw new SmtpFailedRecipientException("Failed to provide destination address"); }
        return msgMail;
    }

【问题讨论】:

    标签: c# asp.net smtp smtpclient system.net.mail


    【解决方案1】:

    根据我的经验,当我需要格式化将通过 SMTP 发送的电子邮件内容时,我将在为内容变量设置值时使用 HTML 标记。例如:

    String content = "<h2>Thank you for your e-mail</h2><font size=4>We appreciate your feedback.<br/> We will process your request soon</font><br/>Regards.";
    

    如果你的内容会在后面的代码中设置,那么你可以使用上面的方法。

    希望对你有帮助。

    【讨论】:

    • Koo - 感谢您的意见。我终于让用户承认他从 Word 中剪切并粘贴了他的内容,这就是弄乱字体大小的原因。他被告知从现在开始使用 Ajax 控件的“从 Word 粘贴”功能。
    猜你喜欢
    • 2015-02-17
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多