asp.net发送邮箱

时间:2022-06-07 14:39:15

通过邮箱找回密码,注册的时候给用户发一个提示到邮箱中或者是通过邮箱验证等等,都需要我们给用户发送一个邮件,那么在asp.net中怎么做呢

我们可以是同微软提供的Mail邮件发送方式

下面就是使用邮箱发送的代码:

//创建邮件信息
        MailMessage mailMessage = new MailMessage();
        mailMessage.From = "发送的邮箱地址";
        mailMessage.To = "收件箱地址";
        mailMessage.Subject ="主题"
        mailMessage.BodyFormat = MailFormat.Html;
        StringBuilder strBody = new StringBuilder();
        strBody.Append("<div>大家好!我是");
        strBody.Append(userName);
        strBody.Append("</div>");
        mailMessage.Body = strBody.ToString();
        SmtpMail.SmtpServer = "smtp.163.com" || "smtp.sina.com";
        mailMessage.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        //用户名
        mailMessage.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
        //密码
        mailMessage.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
        //开始发送
        SmtpMail.Send(mailMessage);

 下面总结了一下发送邮件的方法,供大家使用

    public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.Credentials =
            new System.Net.NetworkCredential(strFrom, strFromPass);
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message =new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Send(message);
        }