1

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("[email protected]"); m.To.Add(new MailAddress("[email protected]")); 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 = "[email protected]", Password = "********" }; smtp.EnableSsl = true; smtp.Send(m); 
3
  • have you tried sending the email from others like hotmail or yahoo? you might need to allow access to your google account to be able to send me through gmail. Commented Nov 17, 2016 at 0:40
  • wow, your question helped me a lot. Nice and Easy. Commented Mar 17, 2018 at 11:42
  • @SaqibMobeen welcome ;) happy coding. Commented Mar 24, 2018 at 4:32

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

no [email protected] first create webmail in your domain account
0
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 = "[email protected]", Password = "********" }; smtp.EnableSsl = true; MailMessage m = new MailMessage(); m.From = new MailAddress("[email protected]"); m.To.Add(new MailAddress("[email protected]")); m.Subject = "Try"; m.Body = "TEST"; smtp.Send(m); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.