c#正确用smtp.发邮件

时间:2025-02-06 21:03:47


        /// <summary>
        /// SSL加密发送邮件From:
        /// </summary>
        /// <param name="sto"></param>
        /// <param name="sToSubject"></param>
        /// <param name="sContent"></param>
        /// <returns></returns>
        public string SendEmailSSL(string sto, string sToSubject, string sContent)
        {
            string sRestring = "";
            try
            {

                string sSmtp = "smtp.";
                string sPort = "587";
                string sFrom = "web@";
                string sAccount = "web@";
                string sPass = "web122";

                client = new SmtpClient();
                = sSmtp;
                = false;
                = Convert.ToInt16(sPort);
                = new (sAccount, sPass);
                = true;
                //星号改成自己邮箱的密码
                // = ;
                message = new MailMessage(sFrom, sto);

                = sToSubject;
                = sContent;
                = .UTF8;
                = true;

                //添加附件
                //Attachment data = new Attachment(@"附件地址如:e:/", );
                //(data);


                (message);
                sRestring = "ok";
            }
            catch (Exception ex)
            {
                sRestring = "failed," + ();
            }


            return sRestring;
        }