教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 在ASP.NET中发送电子邮件的实例教程

在ASP.NET中发送电子邮件的实例教程

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供在ASP.NET中发送电子邮件的实例教程等资源,欢迎您收藏本站,我们将为您提供最新的在ASP.NET中发送电子邮件的实例教程资源
有些朋友习惯使用WinForm来发送邮箱,其实WinForm发送邮箱与asp.net发送基本是一样的,本文章主要介绍利用ASP.NET中发送电子邮件,希望文章对大家会带来帮助。

首先、导入命名空间:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2563')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2563>

using System.Net.Mail;

定义发送电子邮件的方法[网上很多不同的,可以对比着看一下,WinForm的也适用]:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4928')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4928>

/// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="MessageFrom">发件人邮箱地址</param>
/// <param name="MessageTo">收件人邮箱地址</param>
/// <param name="MessageSubject">邮件主题</param>
/// <param name="MessageBody">邮件内容</param>
/// <returns></returns>
public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
 MailMessage message = new MailMessage();
 message.From = MessageFrom;
 message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发
 message.Subject = MessageSubject;
 message.Body = MessageBody;
 message.IsBodyHtml = true; //是否为html格式
 message.Priority = MailPriority.Normal; //发送邮件的优先等级
 SmtpClient sc = new SmtpClient();
 sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP
 sc.Port = 25; //指定发送邮件端口
 //指定登录服务器的用户名和密码(发件人的邮箱登陆密码)
 sc.Credentials = new System.Net.NetworkCredential("【发件箱地址】", "【发件箱密码】");
 try
 {
  sc.Send(message); //发送邮件
 }
 catch
 {
  return false;
 }
 return true;
}

义发送电子邮件的方法

调用定义的方法,实现发送邮件:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6293')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6293>/// <summary>
/// 发送邮件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ForMail(string name, string mail)
{
 try
 {
  //string email = txtemail.Text.Trim();
  MailAddress MessageFrom = new MailAddress("【发件箱地址】"); //发件人邮箱地址
                  string MessageTo = mail; //收件人邮箱地址
  string MessageSubject = bs.HtmlEncode(邮件主题);
  //邮件内容 (一般是一个网址链接,生成随机数加验证id参数,点击去网站验证。)";
  string MessageBody = "" content1.Value.Trim() "";
  if (Send(MessageFrom, MessageTo, MessageSubject, MessageBody))
  {
   //Response.Write("发送邮件成功");
  }
  else
  {
   //Response.Write("发送邮件失败");
  }
 }
 catch
             {
  //ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('客户信息删除失败')</script>");
 }
}

调用方法发送邮件
 

不同邮箱的smtp地址都不一样、需要比如QQ的是smtp.qq.com、163的是smtp.163.com

您可能感兴趣的文章:
在ASP.NET中发送电子邮件的实例教程
如何提高E-mail收发的成功率
asp.net发送邮件的方法汇总
《Perl编程24学时教程》笔记第22课 CGI发送电子邮件
如何使用python发邮件
asp.net发邮件找回密码的功能
phpmailer发送yahoo邮件的例子
.NET环境下五种不同的邮件发送解决方案
phpmailer 类发送邮件乱码解决方法
PHPmailer邮件群发的入门例子

[关闭]
~ ~