zoukankan      html  css  js  c++  java
  • asp.net 发送电子邮件的方法

    方法一:

             在ASP.NET利用知名的邮件服务提供商的SMTP来发送邮件
             首先需要去他们的邮件站点上注册免费邮箱,因为你要使用邮件服务提供商的SMTP,他们需要对身份进行验证,这样可以避免产生大量的垃圾邮件。假设我们在新浪上注册了一个免费电子邮件,用户名是fasongren,密码是123.该帐号为虚构的,请使用自己注册的用户名称和密码代替。我们利用该fasongren@sina.com邮箱作为发送邮箱

    新浪免费邮箱发信(smtp)服务器的地址为:smtp.sina.com.

    我们需要向shouxinren@163.com发送邮件,该邮箱为收件箱

    最开始假如下面using语句

    using System.Web.Mail;//实际上Web.Mail的发送邮件的形式是属于低版本(.NET 1.0)的,现在推荐使用Net.Mail(.NET 2.0)来发送邮件

    protected void Button1_Click(object sender, EventArgs e)  
          {  
              MailMessage objMailMessage;   
              MailAttachment objMailAttachment;   
          // 创建一个附件对象   
       //   objMailAttachment = new MailAttachment( "d:\test.txt" );//发送邮件的附件   
          // 创建邮件消息   
          objMailMessage = new MailMessage();   
          objMailMessage.From = "<A href="mailto:fasongren@sina.com">fasongren@sina.com";//</A>源邮件地址,发送人的邮箱   
          objMailMessage.To = "<A href="mailto:shouxinren@163.com">shouxinren@163.com";//</A>目的邮件地址,接收人的邮箱   
          objMailMessage.Subject = "邮件发送标题:你好";//发送邮件的标题   
          objMailMessage.Body = "邮件发送标内容:测试一下是否发送成功!";//发送邮件的内容   
      
          objMailMessage.BodyFormat = MailFormat.Html;   
       //   objMailMessage.Attachments.Add( objMailAttachment );//将附件附加到邮件消息对象中   
          //接着利用sina的SMTP来发送邮件,需要使用Microsoft .NET Framework SDK v1.1和它以上的版本   
          //基本权限   
          objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");   
          //发件人账号,同样也是发送人邮箱  
           objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "<A href="mailto:fasongren@sina.com">fasongren@sina.com</A>") ;   
           //发件人密码   
           objMailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123");   
           //如果没有上述三行代码,则出现如下错误提示:服务器拒绝了一个或多个收件人地址。服务器响应为: 554 : Client host rejected: Access denied   
          //SMTP地址 不同类型的邮箱SMTP地址不同,新浪的SMTP地址:smtp.sina.com;163邮箱的SMTP地址:smtp.163.com;qq邮箱SMTP地址:smtp.qq.com  
          SmtpMail.SmtpServer = "smtp.sina.com";   
          //开始发送邮件   
          SmtpMail.Send( objMailMessage );   
      
          }  

    方法二:

                 利用jmail组件来发送电子邮件

    首先下载jmail,安装注册【本地注册jmail组件(把jmail.dll放入系统盘下system32文件夹下,运行cmd 输入  regsvr32   jmail.dll )】,复制jmail.dll到你的项目文件中bin文件中,然后要进行com引用,在VS2010(目前只是在vs2010上尝试)中打开解决方案管理器,右键点击—添加引用,在添加引用对话框点击com,找到jmail.dll文件,(如果没有找到,就点击浏览,打开复制到你bin中的jmail文件),点击确认,引用成功,就可以在引用中添加了jmail.

    用户名是fasongren,密码是123.请使用自己注册的用户名称和密码代替。我们利用该fasongren@sina.com邮箱作为发送邮箱

    我们需要向shouxinren@163.com发送邮件,该邮箱为收件箱

    最开始假如下面using语句

    using jmail;

     protected void Button1_Click(object sender, EventArgs e)
    {
              jmail.Message jmessage = new jmail.Message();
              jmessage.Charset = "GB2312";//字符集,固定为GB2312
              jmessage.ISOEncodeHeaders = false;//是否将 信头编码陈iso-8895-1字符集
                jmessage.From = "<A href="mailto:fasongren@sina.com">fasongren@sina.com</A>";//发件人邮箱
                jmessage.FromName = "旅游咨询系统"; //发件人 姓名
                jmessage.Subject = "您的密码已找回!(⊙o⊙).....";//邮件主题
                jmessage.MailServerUserName = "<A href="mailto:fasongren@sina.com">fasongren@sina.com</A>";  //登录邮件服务器的用户名     
                jmessage.MailServerPassWord = "123"; ;//登录服务器的用户密码
                jmessage.AddRecipient("<A href="mailto:shouxinren@163.com">shouxinren@163.com</A>", "旅游咨询系统 ", "");//添加收件人Email地址,姓名并对其加密()
                jmessage.Body = "尊敬的,恭喜您,您的密码已经找回,密码为:。请妥善保管!";//邮件内容
                jmessage.Send("smtp.sina.com", false);//qq邮件服务器地址,不同邮件服务器地址不同
                jmessage.Close();
    }

    以上2种方法都经过试验,都能发送成功!!!

    PS:发件人邮箱要设置打开POP3/SMTP/IMAP,要不然发送不成功!!!

    下面链接有关于ASP.NET发送邮件的详细说明。。。 

    .NET开发邮件发送功能的全面教程(含邮件组件源码)

  • 相关阅读:
    碰撞检测 :Polygon
    碰撞检测 :Line
    碰撞检测 :Rectangle
    碰撞检测:Point
    Canvas 绘制 1 px 直线模糊(非高清屏)的问题
    threading之线程的开始,暂停和退出
    win10利用hexo+gitee搭建博客
    Fullscreen API与DOM监听API
    <el-input>只能输入数字,保留两位小数
    谷歌浏览器查看gitee和github代码的插件
  • 原文地址:https://www.cnblogs.com/caixiaofeng/p/3479464.html
Copyright © 2011-2022 走看看