[Up] C # mail transmission method of embodiment] [webMail

There are in C # send two kinds of messages, one is transmitted using webmail embodiment, another way is to use netmail transmitted when using these two send mail, if the public mail server (e.g. message 126 server, Sina's mail server) are required to be able to send authorization and authentication, if it is using Gmail, then there will be a number of restrictions such as sending e-mail every day. This is the result of 2 ways I passed the test code, simply e-mail user name and password can be modified to their own, but can also modify the mail server, mail server into its own configuration.

/// <Summary>
    /// In Email sent (with authentication using Microsoft's new recommended way)
    /// </ Summary>
    /// <param name = "strto"> Write In Email </ param>
    /// <param name = "strCc"> Cc In Email </ param>
    /// <param name = "strSubject"> title </ param>
    /// <param name = "strBody"> SUMMARY </ param>
    /// <param name = "UserName"> mailbox validate account (account number and configuration to the web.config as) </ param>
    /// <param name = "from"> mail sender, corresponds to the UserName </ param>
    /// <param name = "strErrorMsg" > error message </ param>
    /// <Returns> </ Returns>
    public static BOOL WebSendEmail (strto String, String strCc, strSubject String, 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");

            // mail account and password consistent here e-mail account and password, and the following configuration file must be set
            message.Fields.Add ( "http://schemas.microsoft.com/cdo/configuration/sendusername", "xxxxxxxxx " ); // mail account, such as account number [email protected]: Test11
            message.Fields.Add ( "http://schemas.microsoft.com/cdo/configuration/sendpassword", "xxxxxxxx"); // mail password
            // this is the port specified in the mail server, you can not specify
            //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 = the 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;
    }

// Test Send e-mail

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
           
            Email.SendEmail("[email protected]", "", "Test Email", "Test Send Email");
         
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    }

Mail webconfig file configuration is as follows:






Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/12/23/2323356.html

Guess you like

Origin blog.csdn.net/weixin_33816611/article/details/93052557