How to send Mail Using SMTP?

﹥>﹥吖頭↗ 提交于 2019-12-24 08:18:36

问题


So far I have this CODE for email validation and a Mail to send Using SMTPClient However it won't work ,it won't send to the gmail stated. But i think there is no problem with my code. I need some help to make a way for sending Mail .

string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
      + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
      + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
            Regex ex = new Regex(validEmailPattern, RegexOptions.IgnoreCase);

            if (ex.IsMatch(TextBox1.Text))
            {

                MailMessage m = new MailMessage();
                m.From = new MailAddress("kennethmontealto91@gmail.com");
                m.To.Add(new MailAddress("kennethmontealto91@gmail.com"));
                m.Subject = "Try";
                m.Body = "TEST";

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential()
                {
                    UserName = "kennethmontealto91@gmail.com",
                    Password = "********"
                };
                smtp.EnableSsl = true;
                smtp.Send(m);

回答1:


Change Your smtp.Host = "yourdomainname.com";




回答2:


Try this code after  **smtp.EnableSsl = true;**

string validEmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
  + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
  + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
        Regex ex = new Regex(validEmailPattern, RegexOptions.IgnoreCase);

        if (ex.IsMatch(TextBox1.Text))
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential()
            {
                UserName = "kennethmontealto91@gmail.com",
                Password = "********"
            };
            smtp.EnableSsl = true;

            MailMessage m = new MailMessage();
            m.From = new MailAddress("kennethmontealto91@gmail.com");
            m.To.Add(new MailAddress("kennethmontealto91@gmail.com"));
            m.Subject = "Try";
            m.Body = "TEST";

            smtp.Send(m);
        }


来源:https://stackoverflow.com/questions/40644159/how-to-send-mail-using-smtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!