ASP.NET发送电子邮件源码示例

时间:2021-09-11 17:29:39

using System.Collections.Generic;
using System.Text;

using System.Net;

using System.Net.Mail;

try

{

            //编码暂硬性规定为GB2312

            Encoding encoding = Encoding.GetEncoding(936);

            MailMessage Message = new MailMessage(

            new MailAddress("自己的邮箱", "5", encoding),//第一个是发信人的地址,第二个参数是发信人

            new MailAddress(TextBox1.Text));//收信人邮箱

            Message.SubjectEncoding = encoding;

            Message.Subject =TextBox2.Text;//标题

            Message.BodyEncoding = encoding;

            Message.Body = TextBox3.Text; //主体

            SmtpClient smtpClient = new SmtpClient("smtp.163.com");//信箱服务器

            smtpClient.Credentials = new NetworkCredential("自己的邮箱用户名", "自己的邮箱密码");//信箱的用户名和密码

            smtpClient.Timeout = 999999;

            smtpClient.Send(Message);

        }

        catch (Exception ex)

        {

            throw new Exception(ex.Message);

        }