[英]How can I validate smtp host in asp.net MVC?
I want to validate SMTP host and port.我想验证 SMTP 主机和端口。 Please let me know how can I do this?请让我知道我该怎么做? I want to smtp.gmail.com or smtp-mail.outlook.com.我想要 smtp.gmail.com 或 smtp-mail.outlook.com。 But now my code is allowing numbers also.但现在我的代码也允许数字。
public int Smtp_Id{ get; set; }
[Display(Name="Mail From")]
[Required(ErrorMessage = "Please enter senders mail address.")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Smtp_mailfrom { get; set; }
[Display(Name = "SMTP Host")]
[Required(ErrorMessage = "Please enter host name.")]
public string Smtp_Host { get; set; }
[Display(Name = "User Name")]
[Required(ErrorMessage = "Please enter username.")]
[MaxLength(50,ErrorMessage="Username Should not be more than 50 charachters.")]
public string Smtp_username { get; set; }
[Display(Name = "Password")]
[MaxLength(50, ErrorMessage = "Password Should not be more than 50 charachters.")]
[Required(ErrorMessage = "Please enter password.")]
public string Smtp_password { get; set; }
[Display(Name = "SMTP Port")]
[Required(ErrorMessage = "Please enter Port.")]
public int Smtp_Port { get; set; }
For host name you need to regular expression and for the port no you need something custom like as below it will helpful to you.对于主机名,您需要正则表达式,而对于端口号,您需要自定义的内容,如下所示,它将对您有所帮助。
[Display(Name = "SMTP Host")]
[Required(ErrorMessage = "Please enter host name.")]
[RegularExpression("smtp.gmail.com|smtp-mail.outlook.com")]
public string Smtp_Host { get; set; }
[Display(Name = "SMTP Port")]
[Required(ErrorMessage = "Please enter Port.")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a natural number")]
[Range(1, 9999, ErrorMessage = "Port no must between 1 to 9999")]
public int Smtp_Port { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.