You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

4 years ago
  1. using Apewer;
  2. using Apewer.Internals;
  3. using Apewer.Source;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace Apewer.Network
  8. {
  9. /// <summary>简单邮件传输协议客户端。</summary>
  10. [Serializable]
  11. public sealed class MailClient : Record
  12. {
  13. [NonSerialized]
  14. private TextSet _ts = new TextSet(false);
  15. [NonSerialized]
  16. private bool _smtpssl = false;
  17. [NonSerialized]
  18. private int _smtpport = 25;
  19. /// <summary>构造函数。</summary>
  20. public MailClient() { }
  21. /// <summary>构造函数。</summary>
  22. public MailClient(string server, string user, string pass)
  23. {
  24. Server = server;
  25. User = user;
  26. Pass = pass;
  27. }
  28. /// <summary>服务器地址。</summary>
  29. [Column]
  30. public string Server { get { return _ts["Server"]; } set { _ts["Server"] = value; } }
  31. /// <summary>认证用户。</summary>
  32. [Column]
  33. public string User { get { return _ts["User"]; } set { _ts["User"] = value; } }
  34. /// <summary>认证密码。</summary>
  35. [Column]
  36. public string Pass { get { return _ts["Pass"]; } set { _ts["Pass"] = value; } }
  37. /// <summary>使用安全套接字层加密连接,默认不加密。</summary>
  38. [Column]
  39. public int SmtpPort { get { return _smtpport; } set { _smtpport = value; } }
  40. /// <summary>使用安全套接字层加密连接,默认不加密。</summary>
  41. [Column]
  42. public bool SmtpSsl { get { return _smtpssl; } set { _smtpssl = value; } }
  43. /// <summary>获取 JSON 文本。</summary>
  44. public override string ToString()
  45. {
  46. return Json.From(this).ToString();
  47. }
  48. /// <exception cref="ArgumentException"></exception>
  49. /// <exception cref="ArgumentNullException"></exception>
  50. /// <exception cref="InvalidOperationException"></exception>
  51. internal System.Net.Mail.SmtpClient ToInstance()
  52. {
  53. var credential = new System.Net.NetworkCredential();
  54. credential.UserName = User;
  55. credential.Password = Pass;
  56. var instance = new System.Net.Mail.SmtpClient();
  57. instance.Host = Server;
  58. instance.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
  59. instance.Host = Server;
  60. instance.Credentials = credential;
  61. instance.EnableSsl = SmtpSsl;
  62. instance.Port = SmtpPort;
  63. return instance;
  64. }
  65. }
  66. }