c#利用webmail邮件系统发送邮件示例分享

 更新时间:2020年6月25日 11:38  点击:2252

在C#中发送邮件的方式有2种,一种是使用webmail方式进行发送,另外一种就是采用netmail发送的方式,在采用这2种方式发送邮件时,如果采用公用的邮件服务器(如126邮件服务器,Sina的邮件服务器)都是需要授权认证才能够发送,如果是采用Gmail的话,还会有每天发送邮件的数量等限制。这2种方式是经过我测试通过了的代码,只需要将邮件的用户名和密码修改成自己的即可,同时也可以修改邮件服务器,改成自己配置的邮件服务器。

复制代码 代码如下:

/// <summary>
    /// 发送Email(带验证,采用微软新推荐的方式)
    /// </summary>
    /// <param name="strTo">收件Email</param>
    /// <param name="strCc">抄送Email</param>
    /// <param name="strSubject">标题</param>
    /// <param name="strBody">内容</param>
    /// <param name="UserName">邮箱验证帐号(与web.config里配置的帐号要一样)</param>
    /// <param name="from">发信人邮箱,要与UserName对应</param>
    /// <param name="strErrorMsg">错误消息</param>
    /// <returns></returns>
    public static bool WebSendEmail(string strTo, string strCc, string strSubject, string strBody, ref string strErrorMsg)
    {
        System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
        System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

        bool bState = false;
        string strSMTPServer = "";

        try
        {
            strSMTPServer = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SMTP"]);
            strSMTPServer = strSMTPServer == "" ? "localhost" : strSMTPServer;

            string strFromAddr = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FromAddress"]);
            if (reg.IsMatch(strFromAddr))
            {
                message.From = strFromAddr;
            }
            else
            {
                throw new Exception("The Email Address is wrong,Please reset the Email Address in the web.config file !");
            }

            string strTemp = "";
            foreach (string str in strTo.Split(';'))
            {
                if (reg.IsMatch(str))
                    if (!strTemp.Contains(str))
                        strTemp += str + ";";
            }

            message.Cc = "";
            foreach (string str in strCc.Split(';'))
            {
                if (reg.IsMatch(str))
                    if (!message.Cc.Contains(str))
                        message.Cc += str + ";";
            }

            message.Subject = strSubject;
            message.BodyFormat = System.Web.Mail.MailFormat.Html;

            message.Body ="<html><body>UtilMailMessage001"+ strBody+"- success</body></html>" ;
            //下面这块是加载附件的方法
            MailAttachment attachment1 =new MailAttachment(@"d:\My Documents\test1.doc");
            MailAttachment attachment2 =new MailAttachment("d:\\Documents\\test2.doc");
            message.Attachments.Add(attachment1);
            message.Attachments.Add(attachment2);

            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //这里的邮箱帐号和密码一定要和下面配置文件中设置的邮箱的帐号和密码一致
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxxxxx");//邮箱帐号,比如Test11@126.com帐号为:Test11
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxxxx");//邮箱密码
            //这个是指明邮件服务器的端口,可以不指定
            //message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25");
    

            foreach (string str in strTemp.Split(';'))
            {
                if (reg.IsMatch(str))
                {
                    message.To = str;
                    message.BodyEncoding = System.Text.Encoding.UTF8;
                    System.Web.Mail.SmtpMail.SmtpServer = strSMTPServer;

                    System.Web.Mail.SmtpMail.Send(message);
                }
            }

            bState = true;
        }
        catch (Exception ex)
        {
            System.IO.File.AppendAllText("C:\\Mail_Log.ini", string.Format("{0:yyyy/MM/dd HH:mm:ss}\r\n{1}\r\n\r\n", DateTime.Now, ex.Message));
            bState = false;
            strErrorMsg = ex.Message;
        }

        return bState;
    }
//测试发送邮件
protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {

            Email.SendEmail("xxxxxx@163.com", "", "Test Email", "Test Send Email");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

邮件在webconfig文件中配置如下:

[!--infotagslink--]

相关文章

  • c#使用netmail方式发送邮件示例

    这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
  • PHPMailer在SAE上无法发送邮件的解决方法

    PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
  • 整理几个android后台发送邮件的方法

    本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • 网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2

    Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25
  • PHP利用Jmail组件实现发送邮件

    学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • C#编程实现发送邮件的方法(可添加附件)

    这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php中利用curl smtp发送邮件实例

    本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
  • php定时发送邮件

    <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25
  • C# Email发送邮件 对方打开邮件可获得提醒

    这篇文章主要为大家详细介绍了C# Email发送邮件功能,对方打开通知你,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • phpmailer发送邮件 SMTP Error: Could not authenticate 错误

    今天在使用phpmailer发送邮件时居然提示SMTP Error: Could not authenticate,这个感觉是smtp设置的问题,下面我在网上找到了几种解决办法。 今天在使用phpmailer发...2016-11-25
  • phpmailer 发送邮件实例代码

    header("Content-type:text/html;charset=utf-8"); include('phpmailer/class.phpmailer.php'); include('phpmailer/class.smtp.php'); $mail = new PHPMailer();...2016-11-25
  • 利用phpmailer 发送邮件代码[发送html内容]

    我们利用 phpmailer功能实现 邮件发送功能哦,这里还利用了模板呢,就是读取指定文件内容再发送给朋友。 <?php @session_start(); include(dirname(__FILE__).'....2016-11-25
  • c#利用webmail邮件系统发送邮件示例分享

    在C#中发送邮件的方式有2种,一种是使用webmail方式进行发送,另外一种就是采用netmail发送的方式,这篇文章介绍了c#使用webmail方式发送邮件示例,大家参考使用吧...2020-06-25
  • Spring+quartz实现定时发送邮件功能实例

    本文介绍了Spring+quartz实现定时发送邮件功能实例,非常实用,有兴趣的同学快来看看吧 在ApplicationContext.xml的内容如下: 代码如下复制代码 <beans xmlns="...2017-07-06
  • Yii2使用swiftmailer发送邮件的方法

    这篇文章主要介绍了Yii2使用swiftmailer发送邮件的方法,结合实例形式分析了Yii2使用swiftmailer进行邮件发送的设置与代码实现技巧,需要的朋友可以参考下...2016-05-05
  • 使用PHPMailer发送邮件实例代码总结

    PHPMailer发送邮件现在php开发者比较常用的一个邮件发送组件了,利用它我们几乎不需要考虑任何问题,只要简单的把代码放网上把邮箱用户名密码与stmp改一下就可以发邮件了...2016-11-25
  • php 发送邮件与pop3邮件登录代码

    代码如下 复制代码 function send_msg($to,$subject,$body) { $...2016-11-25
  • php 使用qmail发送邮件实现代码

    以前都是利用mail函数或phpermail进行邮件发送,今天看这款利用qmail进行邮件发送,写法非常简单,是一款不错的工具。 function send_check_mail($email, $subject,$u...2016-11-25