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.

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