asp.net简单发送邮件代码

 protected void SendMailLocalhost(string ToMail, string Name, string Pwd)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.To.Add(ToMail); //收邮件地址
       
        //msg.CC.Add("[email protected]");可以抄送给多人
      
        msg.From = new System.Net.Mail.MailAddress("[email protected]", "admin", System.Text.Encoding.UTF8);
        /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/

        msg.Subject = "发给您的用户名和密码";//邮件标题
        msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
        msg.Body = "您的用户名是:" + Name + ",密码是:" + Pwd; //邮件内容
        msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
        msg.IsBodyHtml = false;//是否是HTML邮件
        msg.Priority = System.Net.Mail.MailPriority.High;//邮件优先级

 

        //System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        //client.Host = "localhost";

 

        //System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        //client.Credentials = new System.Net.NetworkCredential("[email protected]", "xxx");
        //注册的邮箱和密码
        //client.Host = "smtp.yahoo.com.cn";

 

        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("[email protected]", "xxx");
        //上述写你的GMail邮箱和密码
        client.Port = 587;//Gmail使用的端口
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;//经过ssl加密


        object userState = msg;
        try
        {
            //client.SendAsync(msg, userState); //此为异步传输
            client.Send(msg);
            this.lblMsgMail.Visible = true;
            this.lblMsgMail.Text = "邮件发送成功!";
            this.lblMsgMail.ForeColor = System.Drawing.Color.Red;
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            //ex = null;
            this.lblMsgMail.Visible = true;
            this.lblMsgMail.Text = "发送邮件出错!" + ex.ToString();
            this.lblMsgMail.ForeColor = System.Drawing.Color.Red;
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章