在用.net提供的 MailMessage 和SmtpClient发送邮件时,如果把IsBodyHtml 设为true,就没办法发送,这是什么原因呢?m_client = new SmtpClient(m_smtpServer);
MailMessage mailMsg = new MailMessage(this.m_from, mail_to, mail_subject, mail_body);下面这行,如果注释掉就可以发送,如果不注释就没办法发送,可我想用html的方式发送。
mailMsg.IsBodyHtml = true;this.m_client.Timeout = 20000;
this.m_client.Send(mailMsg);

解决方案 »

  1.   

    引用命名空间:using System.Text.RegularExpressions;   
    using System.Web.Mail;#region 发送Web邮件和附件
            /// <summary>
            /// 发送Web邮件和附件
            /// </summary>
            /// <param name="mailFrom">发送邮件邮箱地址</param>
            /// <param name="mailTo">接收邮件的邮箱地址</param>
            /// <param name="mailSubject">邮件主题</param>
            /// <param name="mailBody">邮件内容</param>
            /// <param name="mailFilePath">发送邮件附件集合</param>
            /// <returns></returns>
            public  bool SendWebMail(string mailFrom, string mailTo, string mailSubject, string mailPassWord, string mailBody, string[] mailFilePath)
            {
                MailMessage mail = new MailMessage();
                mail.From = mailFrom;
                mail.To = mailTo;
                mail.Subject = mailSubject;
                mail.Body = mailBody;
                mail.BodyFormat = MailFormat.Html;//设置为HTML格式
                string[] mailSmtp = Regex.Split(mailFrom, "@", RegexOptions.IgnoreCase);
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mailSmtp[0]); //set your username here
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassWord); //set your password here
                SmtpMail.SmtpServer = mailSmtp[1].Insert(0, "smtp.");
                string[] file = mailFilePath;
                foreach (string filePath in file)
                {
                    if (filePath != "")
                    {
                        mail.Attachments.Add(new MailAttachment(filePath));
                    }
                }
                try
                {
                    SmtpMail.Send(mail);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            #endregion
    如果发送附件的话就传一个空数组进去。