问题
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