Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. using System;
  2. using System.Web;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Web.Mvc;
  5. using System.Threading.Tasks;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Linq;
  10. using System.Net;
  11. using Vishwaprani.ContextClass;
  12. using Vishwaprani.Models;
  13. using System.Net.Mail;
  14. using System.Threading;
  15. namespace Vishwaprani.CustomValidator
  16. {
  17. public class EmailAddresValidationAttribute : ValidationAttribute
  18. {
  19. protected override ValidationResult IsValid(object value, ValidationContext validationContext)
  20. {
  21. if (value != null)
  22. {
  23. string mailadress = Convert.ToString(value);
  24. MailMessage email = new MailMessage(new MailAddress("emailvalidation123456@gmail.com", "Email validation"), new MailAddress(mailadress, "Validating email address"));
  25. email.Subject = "email validation";
  26. email.Body = @"Test for validation";
  27. var client = new SmtpClient("smtp.gmail.com") ;
  28. client.Port=587;
  29. client.EnableSsl=true;
  30. client.Credentials = new System.Net.NetworkCredential("emailvalidation123456", "U50h2#AP2XR@");
  31. try
  32. {
  33. client.Send(email);
  34. }
  35. catch(SmtpFailedRecipientException ex)
  36. {
  37. if(ex.StatusCode==SmtpStatusCode.GeneralFailure||ex.StatusCode==SmtpStatusCode.LocalErrorInProcessing||ex.StatusCode==SmtpStatusCode.ServiceNotAvailable||ex.StatusCode==SmtpStatusCode.TransactionFailed)
  38. {
  39. Thread.Sleep(5000);
  40. try
  41. {
  42. client.Send(email);
  43. }
  44. catch(SmtpFailedRecipientException exc)
  45. {
  46. if (exc.StatusCode == SmtpStatusCode.GeneralFailure || exc.StatusCode == SmtpStatusCode.LocalErrorInProcessing || exc.StatusCode == SmtpStatusCode.ServiceNotAvailable || exc.StatusCode == SmtpStatusCode.TransactionFailed)
  47. {
  48. 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.");
  49. }
  50. }
  51. }
  52. else if(ex.StatusCode==SmtpStatusCode.ExceededStorageAllocation||ex.StatusCode==SmtpStatusCode.InsufficientStorage)
  53. {
  54. return new ValidationResult("There is a storage issue with the email account. Please resolve that.");
  55. }
  56. else if(ex.StatusCode==SmtpStatusCode.MailboxBusy)
  57. {
  58. Thread.Sleep(5000);
  59. try
  60. {
  61. client.Send(email);
  62. }
  63. catch(SmtpFailedRecipientException exct)
  64. {
  65. if (exct.StatusCode == SmtpStatusCode.MailboxBusy)
  66. {
  67. return new ValidationResult("Mail box is busy two times try again later.");
  68. }
  69. }
  70. }
  71. else if(ex.StatusCode==SmtpStatusCode.MailboxUnavailable)
  72. {
  73. return new ValidationResult("Mail box is unavailable.");
  74. }
  75. else
  76. {
  77. return new ValidationResult("There is an error with the email address you provided. Please check it.");
  78. }
  79. }
  80. return ValidationResult.Success;
  81. }
  82. else return null;
  83. }
  84. }
  85. }
  86.  
  87. try
  88. {
  89. address = new MailAddress("emailvalidation123456@gmail.com", "Email validation").Address;
  90. }
  91. catch(FormatException) {
  92. //Invalid email address
  93. }
  94.  
  95. client.SendCompleted += new
  96. SendCompletedEventHandler(SendCompletedCallback);
  97.  
  98.  
  99. private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  100. {
  101. // Get the unique identifier for this asynchronous operation.
  102. String token = (string)e.UserState;
  103.  
  104. if (e.Cancelled)
  105. {
  106. Console.WriteLine("[{0}] Send canceled.", token);
  107. }
  108. if (e.Error != null)
  109. {
  110. Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
  111. }
  112. else
  113. {
  114. Console.WriteLine("Message sent.");
  115. }
  116. mailSent = true;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement