在C#中如何发送Email

C#中如何发送Email

一、创建界面  WebForm1.aspx

 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

类型

对象名

Text属性

Label

Label1

收件人地址:

Label

Label2

标题:

Label

Label3

 

TextBox

TextBox1

 

TextBox

TextBox2

 

TextBox

TextBox3

 

Button

Button1

发送

RegularExpressionValidator

RegularExpressionValidator1

 

 

 

 

 

注意点:

1.  当发送成功对象Label3text属性显示“发送成功“

2.  对象RegularExpressionValidator1的属性

ControlToValidate="TextBox1"

ErrorMessage="Email格式不对"

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" //代表email验证格式

当收件人的Email的地址格式不正确会显示“Email格式不对“

3.  对象TextBox1代表 收件人地址

对象TextBox2代表 标题

对象TextBox2代表 发送内容

 

二、显示代码  WebForm1.aspx.cs

 

1.  在文件头部添加代码 using System.Web.Mail;

2.  添加字段private System.Web.Mail.MailMessage m_Mail;

3

              private void Page_Load(object sender, System.EventArgs e)

         {

              m_Mail=new MailMessage(); //实例化MailMessage对象

         }

4.双击“发送“按钮

              private void Button1_Click(object sender, System.EventArgs e)

         {

              m_Mail.From="jinq@gpssz.com";

              m_Mail.To=TextBox1.Text;

              m_Mail.Subject=TextBox2.Text;

              m_Mail.BodyFormat=MailFormat.Html;

              m_Mail.Body=TextBox3.Text;

              SmtpMail.Send(m_Mail);

              Label3.Text="发送成功";

     }

这样整个功能都实现了,谢谢光顾tongue_smile.giftongue_smile.giftongue_smile.gif

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值