欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 09:22:16
在c#中发送邮件的方式有2种,一种是使用webmail方式进行发送,另外一种就是采用netmail发送的方式,在采用这2种方式发送邮件时,如果采用公用的邮件服务器(如126...

在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文件中配置如下:

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