Advertisement
Guest User

Untitled

a guest
May 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. namespace LibMailer {
  2. using System.Net;
  3. using System.Net.Mail;
  4.  
  5. /// <summary>
  6. /// Usage
  7. /// var mailer = new Mailer(host: "smtp.mail.yahoo.com", port: 587)
  8. /// .From(address: "g.seref@yahoo.ca").To(address: "serefguneysu@gmail.com")
  9. /// .Subject(subject: "Konu")
  10. /// .Body(body: "<h1>GÖVDE</h1>", isHtml: true)
  11. /// .Html()
  12. /// .Login(user: "g.seref@yahoo.ca", password: "y0urP@assw0rd", enableSsl: true)
  13. /// .Ssl()
  14. /// .Send();
  15. /// </summary>
  16. public class Mailer {
  17. private SmtpClient client = new SmtpClient();
  18. private NetworkCredential credentials = new NetworkCredential();
  19. private MailAddress sender { get; set; }
  20. private MailAddress receiver { get; set; }
  21. private MailMessage message = new MailMessage();
  22. public Mailer(string host, int port) {
  23. client.Host = host;
  24. client.Port = port;
  25. }
  26.  
  27. public Mailer Login(string user, string password, bool enableSsl = false) {
  28. credentials.UserName = user;
  29. credentials.Password = password;
  30.  
  31. client.Credentials = credentials;
  32. client.EnableSsl = enableSsl;
  33.  
  34. return this;
  35. }
  36.  
  37. public Mailer Ssl() {
  38. client.EnableSsl = true;
  39. return this;
  40. }
  41.  
  42. public Mailer From(string address) {
  43. sender = new MailAddress(address);
  44. message.From = sender;
  45.  
  46. return this;
  47. }
  48.  
  49. public Mailer To(string address) {
  50. receiver = new MailAddress(address);
  51. message.To.Add(receiver);
  52. return this;
  53. }
  54.  
  55. public Mailer Subject(string subject) {
  56. message.Subject = subject;
  57. return this;
  58. }
  59.  
  60. public Mailer Body(string body, bool isHtml = false) {
  61. message.Body = body;
  62. message.IsBodyHtml = isHtml;
  63. return this;
  64. }
  65.  
  66. public Mailer Html() {
  67. message.IsBodyHtml = true;
  68. return this;
  69. }
  70.  
  71.  
  72. public Mailer Send() {
  73. client.Send(message);
  74. return this;
  75. }
  76.  
  77. public MailMessage Mail(string from, string to, string subject, string body) {
  78. var sender = new MailAddress(from);
  79. var receiver = new MailAddress(to);
  80. var msg = new MailMessage(from: sender, to: receiver);
  81. msg.Subject = subject;
  82. msg.Body = body;
  83. return msg;
  84. }
  85.  
  86. }
  87.  
  88.  
  89. class Program {
  90. static void Main(string[] args) {
  91.  
  92. new Mailer(host: "smtp.mail.yahoo.com", port: 587)
  93. .From(address: "g.seref@yahoo.ca").To(address: "serefguneysu@gmail.com")
  94. .Subject(subject: "Konu")
  95. .Body(body: "<h1>GÖVDE</h1>", isHtml: true)
  96. .Login(user: "g.seref@yahoo.ca", password: "y0urP@assw0rd", enableSsl: true)
  97. .Send();
  98.  
  99. new Mailer(host: "smtp.mail.yahoo.com", port: 587)
  100. .From(address: "g.seref@yahoo.ca").To(address: "serefguneysu@gmail.com")
  101. .Subject(subject: "Konu")
  102. .Body(body: "<h1>GÖVDE</h1>", isHtml: true)
  103. .Html()
  104. .Login(user: "g.seref@yahoo.ca", password: "y0urP@assw0rd")
  105. .Ssl()
  106. .Send();
  107.  
  108.  
  109. }
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement