MailUtil.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // System.Web.Mail.MailUtil.cs
  3. //
  4. // Author(s):
  5. // Per Arneng <[email protected]>
  6. //
  7. //
  8. using System;
  9. using System.Text;
  10. namespace System.Web.Mail {
  11. // This class contains some utillity functions
  12. // that doesnt fit in other classes and to keep
  13. // high cohesion on the other classes.
  14. internal class MailUtil {
  15. // determines if a string needs to
  16. // be encoded for transfering over
  17. // the smtp protocol without risking
  18. // that it would be changed.
  19. public static bool NeedEncoding( string str ) {
  20. foreach( char chr in str ) {
  21. int ch = (int)chr;
  22. if( ! ( (ch > 61) && (ch < 127) || (ch>31) && (ch<61) ) ) {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. // Encodes a string to base4
  29. public static string Base64Encode( string str ) {
  30. return Convert.ToBase64String( Encoding.Default.GetBytes( str ) );
  31. }
  32. // Generate a unique boundary
  33. public static string GenerateBoundary() {
  34. StringBuilder boundary = new StringBuilder("__MONO__Boundary");
  35. boundary.Append("__");
  36. DateTime now = DateTime.Now;
  37. boundary.Append(now.Year);
  38. boundary.Append(now.Month);
  39. boundary.Append(now.Day);
  40. boundary.Append(now.Hour);
  41. boundary.Append(now.Minute);
  42. boundary.Append(now.Second);
  43. boundary.Append(now.Millisecond);
  44. boundary.Append("__");
  45. boundary.Append((new Random()).Next());
  46. boundary.Append("__");
  47. return boundary.ToString();
  48. }
  49. }
  50. }