SoFunction
Updated on 2025-03-07

c#Send email using webmail mail system and share example


/// <summary>
/// Send email (with verification, using the new Microsoft recommended method)
    /// </summary>
/// <param name="strTo">Income Email</param>
/// <param name="strCc">Cc Email</param>
/// <param name="strSubject">Title</param>
/// <param name="strBody">Content</param>
/// <param name="UserName">Email verification account (the same as the account configured in it)</param>
/// <param name="from">The sender's email address must correspond to UserName</param>
/// <param name="strErrorMsg">Error Message</param>
    /// <returns></returns>
    public static bool WebSendEmail(string strTo, string strCc, string strSubject, string strBody, ref string strErrorMsg)
    {
        message = new ();
        reg = new (@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

        bool bState = false;
        string strSMTPServer = "";

        try
        {
            strSMTPServer = (["SMTP"]);
            strSMTPServer = strSMTPServer == "" ? "localhost" : strSMTPServer;

            string strFromAddr = (["FromAddress"]);
            if ((strFromAddr))
            {
                = strFromAddr;
            }
            else
            {
                throw new Exception("The Email Address is wrong,Please reset the Email Address in the file !");
            }

            string strTemp = "";
            foreach (string str in (';'))
            {
                if ((str))
                    if (!(str))
                        strTemp += str + ";";
            }

            = "";
            foreach (string str in (';'))
            {
                if ((str))
                    if (!(str))
                        += str + ";";
            }

            = strSubject;
            = ;

            ="<html><body>UtilMailMessage001"+ strBody+"- success</body></html>" ;
//The following is the method of loading attachments
            MailAttachment attachment1 =new MailAttachment(@"d:\My Documents\");
            MailAttachment attachment2 =new MailAttachment("d:\\Documents\\");
            (attachment1);
            (attachment2);

            ("/cdo/configuration/smtpusessl", "true");
            ("/cdo/configuration/smtpauthenticate", "1");
//The email account and password here must be consistent with the email account and password set in the configuration file below.
("/cdo/configuration/sendusername", "xxxxxxxxx");//Email account, such as Test11@ account: Test11
("/cdo/configuration/sendpassword", "xxxxxxxx");//Email Password
//This indicates the port of the mail server and can be specified without
            //("/cdo/configuration/smtpserverport", "25");
    

            foreach (string str in (';'))
            {
                if ((str))
                {
                    = str;
                    = .UTF8;
                    = strSMTPServer;

                    (message);
                }
            }

            bState = true;
        }
        catch (Exception ex)
        {
            ("C:\\Mail_Log.ini", ("{0:yyyy/MM/dd HH:mm:ss}\r\n{1}\r\n\r\n", , ));
            bState = false;
            strErrorMsg = ;
        }

        return bState;
    }
//Test email
protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {

            ("xxxxxx@", "", "Test Email", "Test Send Email");

        }
        catch (Exception ex)
        {
            ();
        }
    }