C#发送邮件可用代码

public MailMessage Initial(string Address)
        {
            MailMessage m_Mail = new MailMessage();
            //发件人
            m_Mail.From = new MailAddress("******@126.com");
            //收件人
            m_Mail.To.Add(new MailAddress(Address));
            //主题
            m_Mail.Subject ="代理好久没验证了!!!";
            //内容
            m_Mail.Body = "赶紧重开一下吧!!!";
            //邮件主题和正文编码格式
            m_Mail.SubjectEncoding = System.Text.Encoding.UTF8;
            m_Mail.BodyEncoding = System.Text.Encoding.UTF8;
            //邮件正文是Html编码
            m_Mail.IsBodyHtml = true;
            //优先级
            m_Mail.Priority = System.Net.Mail.MailPriority.High;
            //添加附件,可以添加多个
            //m_Mail.Attachments.Add(new Attachment("c:\\1.txt"));
            //密件抄送收件人
            m_Mail.Bcc.Add(Address);
            //抄送收件人
            m_Mail.CC.Add(Address);

            return m_Mail;
        }a



            SmtpClient client = new SmtpClient();
            //这个地方的用户名你可以用split从发送人中截取
            client.Credentials = new System.Net.NetworkCredential("****", 密码);
            client.Port = 25;
            client.Host = "smtp.126.com";
            try
            {
                client.Send(Initial("******@qq.com"));
                //ClientScript.RegisterStartupScript(this.GetType(), "myScript", "<script>alert('发送成功!')</script>");
                SuccMsg.Add("发送邮件成功!" + "******@qq.com");
                return true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                SuccMsg.Add("发送邮件失败!" + "*****@qq.com");
                return false;
            }    

 

posted @ 2013-01-10 11:54  落冰  阅读(265)  评论(0编辑  收藏  举报