简体   繁体   English

如何在 asp.net MVC 中验证 smtp 主机?

[英]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.

相关问题 如何验证ASP.NET MVC中的复选框值? - How do I validate a checkbox value in ASP.NET MVC? 如何在 asp.NET MVC 中正确验证 forms? - How do I validate forms properly in asp.NET MVC? 如何验证电子邮件来自ASP.NET MVC中的特定域? - How Can I Validate that an Email is from a Particular Domain in ASP.NET MVC? ASP.NET MVC 3-调用UpdateModel或TryUpdateModel时如何有条件地验证(IValidatableObject)? - ASP.NET MVC 3 - How can I conditionally validate (IValidatableObject) when calling UpdateModel or TryUpdateModel? 使用ASP.NET MVC 5更改请求后,如何在控制器中手动验证模型? - How can I manually validate a model in the controller after altering the request using ASP.NET MVC 5? 如何在ASP.NET MVC中验证文本框 - How to validate textboxes in ASP.NET MVC 如何在 ASP.NET 中验证用户输入? - How can I validate user input in ASP.NET? 如何使用使用 hash 加密、C#、ASP.NET MVC、实体框架的存储过程验证帐户 - How can I validate an account with a stored procedure that uses hash encryption, C#, ASP.NET MVC, Entity Framework 我可以在 127.0.0.1 而不是本地主机上运行我的 asp.net 核心 mvc web 项目吗 - Can i run my asp.net core mvc web project on 127.0.0.1 instead of local host 如何在ASP.NET MVC中的2个表中进行过滤? - How can I filter in 2 tables in ASP.NET MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM