发送邮件,清除html

/// <summary> 
/// 发送邮件
/// </summary> 
/// <param name="to">收件人邮件地址</param> 
/// <param name="from">发件人邮件地址</param> 
/// <param name="subject">邮件主题</param> 
/// <param name="body">邮件内容</param> 
/// <param name="username">登录smtp主机时用到的用户名,注意是邮件地址'@'以前的部分</param> 
/// <param name="password">登录smtp主机时用到的用户密码</param> 
/// <param name="smtpHost">发送邮件用到的smtp主机</param>
/// <returns>true-发送成功,false-发送失败</returns>
public static Boolean sendMail(String to, String from, String subject, String body, String userName, String password, String smtpHost)
{
    try
    {
        System.Net.Mail.MailAddress fromAddr = new System.Net.Mail.MailAddress(from);
        System.Net.Mail.MailAddress toAddr = new System.Net.Mail.MailAddress(to);
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(fromAddr, toAddr);
        message.Subject = subject;//设置邮件主题 
        message.IsBodyHtml = true;//设置邮件正文为html格式 
        message.Body = body;//设置邮件内容 
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpHost);
        //设置发送邮件身份验证方式 
        //注意如果发件人地址是abc@def.com,则用户名是abc而不是abc@def.com 
        client.Credentials = new System.Net.NetworkCredential(userName, password); client.Send(message); return true;
    }
    catch
    {
        return false;
    }
}


C# code 
/// <summary>
/// 清除html
/// </summary>
/// <param name="html">字符串</param>
/// <returns>无html标签的字符串</returns>
public static String clearHTML(String htmlstr)
{
    System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("<(?:.|\\s)*?>",
        System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    return reg.Replace(htmlstr, "");
}
posted @ 2012-01-18 14:34  kangwl  阅读(191)  评论(0编辑  收藏  举报