Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.Web.Mvc;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using Vishwaprani.ContextClass;
- using Vishwaprani.Models;
- using System.Net.Mail;
- using System.Threading;
- namespace Vishwaprani.CustomValidator
- {
- public class EmailAddresValidationAttribute : ValidationAttribute
- {
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- if (value != null)
- {
- string mailadress = Convert.ToString(value);
- MailMessage email = new MailMessage(new MailAddress("emailvalidation123456@gmail.com", "Email validation"), new MailAddress(mailadress, "Validating email address"));
- email.Subject = "email validation";
- email.Body = @"Test for validation";
- var client = new SmtpClient("smtp.gmail.com") ;
- client.Port=587;
- client.EnableSsl=true;
- client.Credentials = new System.Net.NetworkCredential("emailvalidation123456", "U50h2#AP2XR@");
- try
- {
- client.Send(email);
- }
- catch(SmtpFailedRecipientException ex)
- {
- if(ex.StatusCode==SmtpStatusCode.GeneralFailure||ex.StatusCode==SmtpStatusCode.LocalErrorInProcessing||ex.StatusCode==SmtpStatusCode.ServiceNotAvailable||ex.StatusCode==SmtpStatusCode.TransactionFailed)
- {
- Thread.Sleep(5000);
- try
- {
- client.Send(email);
- }
- catch(SmtpFailedRecipientException exc)
- {
- if (exc.StatusCode == SmtpStatusCode.GeneralFailure || exc.StatusCode == SmtpStatusCode.LocalErrorInProcessing || exc.StatusCode == SmtpStatusCode.ServiceNotAvailable || exc.StatusCode == SmtpStatusCode.TransactionFailed)
- {
- return new ValidationResult("There is a problem with your email address that can not be resolved. Please check it and try again later or enter another one.");
- }
- }
- }
- else if(ex.StatusCode==SmtpStatusCode.ExceededStorageAllocation||ex.StatusCode==SmtpStatusCode.InsufficientStorage)
- {
- return new ValidationResult("There is a storage issue with the email account. Please resolve that.");
- }
- else if(ex.StatusCode==SmtpStatusCode.MailboxBusy)
- {
- Thread.Sleep(5000);
- try
- {
- client.Send(email);
- }
- catch(SmtpFailedRecipientException exct)
- {
- if (exct.StatusCode == SmtpStatusCode.MailboxBusy)
- {
- return new ValidationResult("Mail box is busy two times try again later.");
- }
- }
- }
- else if(ex.StatusCode==SmtpStatusCode.MailboxUnavailable)
- {
- return new ValidationResult("Mail box is unavailable.");
- }
- else
- {
- return new ValidationResult("There is an error with the email address you provided. Please check it.");
- }
- }
- return ValidationResult.Success;
- }
- else return null;
- }
- }
- }
- try
- {
- address = new MailAddress("emailvalidation123456@gmail.com", "Email validation").Address;
- }
- catch(FormatException) {
- //Invalid email address
- }
- client.SendCompleted += new
- SendCompletedEventHandler(SendCompletedCallback);
- private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
- {
- // Get the unique identifier for this asynchronous operation.
- String token = (string)e.UserState;
- if (e.Cancelled)
- {
- Console.WriteLine("[{0}] Send canceled.", token);
- }
- if (e.Error != null)
- {
- Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
- }
- else
- {
- Console.WriteLine("Message sent.");
- }
- mailSent = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement