Browse Source

Changes made to the Forget Email function.

master
GJWilliams87 4 years ago
parent
commit
9009d260a7

+ 105
- 102
UnivateProperties_API/Repository/Communication/MailRepository.cs View File

@@ -1,12 +1,13 @@
1
-using MailKit.Net.Smtp;
2
-using MimeKit;
3
-using System;
1
+using System;
4 2
 using System.Collections.Generic;
5 3
 using System.Linq;
6 4
 using System.Linq.Dynamic.Core;
5
+using System.Net.Mail;
6
+using System.Text;
7 7
 using UnivateProperties_API.Context;
8 8
 using UnivateProperties_API.Helpers;
9 9
 using UnivateProperties_API.Model.Communication;
10
+using UnivateProperties_API.Model.Users;
10 11
 
11 12
 namespace UnivateProperties_API.Repository.Communication
12 13
 {
@@ -27,18 +28,12 @@ namespace UnivateProperties_API.Repository.Communication
27 28
 
28 29
         public MailRepository(DataContext db)
29 30
         {
30
-            _dbContext = db;
31
+            _dbContext = db;              
31 32
         }
32 33
 
33 34
         public MailRepository()
34 35
         {
35
-        }
36
-
37
-        MimeMessage messageObj = new MimeMessage();
38
-        MailboxAddress from;
39
-        MailboxAddress to;
40
-        BodyBuilder bodyBuilder = new BodyBuilder();
41
-        SmtpClient client = new SmtpClient();
36
+        }        
42 37
 
43 38
         public void ContactUs(MailModel mm)
44 39
         {
@@ -48,69 +43,63 @@ namespace UnivateProperties_API.Repository.Communication
48 43
             string email = mm.Email;
49 44
             string message = mm.Message;
50 45
             var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
51
-
52
-            from = new MailboxAddress("Admin", mm.FromAddress);
53
-
54
-            InternetAddressList list = new InternetAddressList();
46
+            string body = "<div style=\"margin: 5px\">" +
47
+                            "<h4>Contact from: " + name + "!</h4>" +
48
+                            "<h4>Email: " + email + "</h4>" +
49
+                            "<h4>Phone: " + phone + "</h4>" +
50
+                            "<h4>Property: " + property + "</h4>" +
51
+                            "<div>" +
52
+                            "<h4>Message: </h4>" +
53
+                            "<p>" + message + "</p>" +
54
+                            "</div>" +
55
+                            "</div>" +
56
+                            "</div>";
57
+
58
+            string toList = "";
59
+            int emailCount = 0;
55 60
             foreach (var recipient in recipients)
56 61
             {
57
-                list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
62
+                toList += recipient.RecipientMail + ";, ";
63
+                emailCount++;
58 64
             }
59
-            //to = new MailboxAddress("User", mm.ToAddress);
60
-
61
-            messageObj.From.Add(from);
62
-            messageObj.To.AddRange(list);
63
-
64
-            messageObj.Subject = "Uni-Vate - New Contact Request";
65
-
66
-            bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
67
-                "<h4>Contact from: "+  name +"!</h4>" +
68
-                "<h4>Email: "+ email +"</h4>" +
69
-                "<h4>Phone: " + phone + "</h4>" +
70
-                "<h4>Property: " + property + "</h4>" +
71
-                "<div>" +
72
-                "<h4>Message: </h4>" +
73
-                "<p>" + message + "</p>" +
74
-                "</div>" +
75
-                "</div>" +
76
-                "</div>";
77 65
 
78
-            messageObj.Body = bodyBuilder.ToMessageBody();
79
-
80
-            client.Connect("smtp.gmail.com", 465, true);
81
-            client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
66
+            if (toList.Length > 0)
67
+            {
68
+                if (emailCount == 1)
69
+                    toList = toList.Substring(0, toList.Length - 3);
70
+                else
71
+                    toList = toList.Substring(0, toList.Length - 2);
72
+            }
82 73
 
83
-            client.Send(messageObj);
84
-            client.Disconnect(true);
85
-            client.Dispose();
74
+            var host = _dbContext.Hosts.FirstOrDefault();
75
+            using (SmtpClient smtp = new SmtpClient(host.Host))
76
+            {
77
+                MailMessage mail = new MailMessage();
78
+                mail.To.Add(toList);
79
+                mail.Subject = "Uni-Vate - New Contact Request";
80
+                mail.Body = body;
81
+                mail.IsBodyHtml = true;
82
+                mail.BodyEncoding = Encoding.ASCII;
83
+                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
84
+                mail.Sender = new MailAddress(host.User, "UniVate Properties");
85
+                mail.From = new MailAddress(mm.Email, mm.Name);
86
+
87
+                smtp.UseDefaultCredentials = host.NeedsAuthorize;
88
+                smtp.Credentials = host.GetNetworkCredential();
89
+                smtp.EnableSsl = host.UseSSL;
90
+                smtp.Send(mail);
91
+            }            
86 92
         }
87 93
 
88 94
         public void EnquireNow(MailModel mm)
89
-        {
95
+        {            
90 96
             string phone = mm.Phone;
91 97
             string name = mm.Name;
92 98
             string email = mm.Email;
93 99
             string message = mm.Message;
94
-            var props = _dbContext.Properties.ToList();
95
-            var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
100
+            var prop = _dbContext.Properties.Where(p => p.Id == int.Parse(mm.Property)).FirstOrDefault();
96 101
             var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
97
-
98
-            
99
-            from = new MailboxAddress("Admin", mm.FromAddress);
100
-
101
-            InternetAddressList list = new InternetAddressList();
102
-            foreach (var recipient in recipients)
103
-            {
104
-                list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
105
-            }
106
-            //to = new MailboxAddress("User", mm.ToAddress);
107
-
108
-            messageObj.From.Add(from);
109
-            messageObj.To.AddRange(list);
110
-
111
-            messageObj.Subject = "Uni-Vate - Enquiry to view property";
112
-
113
-            bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
102
+            string body = "<div style=\"margin: 5px\">" +
114 103
                 "<h4>Contact from: " + name + "!</h4>" +
115 104
                 "<h4>Email: " + email + "</h4>" +
116 105
                 "<h4>Phone: " + phone + "</h4>" +
@@ -124,52 +113,66 @@ namespace UnivateProperties_API.Repository.Communication
124 113
                 "</div>" +
125 114
                 "</div>";
126 115
 
127
-            messageObj.Body = bodyBuilder.ToMessageBody();
116
+            string toList = "";
117
+            foreach (var recipient in recipients)
118
+            {
119
+                toList += recipient.RecipientMail + ";, ";
120
+            }
128 121
 
129
-            client.Connect("smtp.gmail.com", 465, true);
130
-            client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
122
+            if (toList.Length > 0)
123
+                toList = toList.Substring(0, toList.Length - 2);
131 124
 
132
-            client.Send(messageObj);
133
-            client.Disconnect(true);
134
-            client.Dispose();
125
+            var host = _dbContext.Hosts.FirstOrDefault();
126
+            using (SmtpClient smtp = new SmtpClient(host.Host))
127
+            {
128
+                MailMessage mail = new MailMessage();
129
+                mail.To.Add(toList);
130
+                mail.Subject = "Uni-Vate - Enquiry to view property";
131
+                mail.Body = body;
132
+                mail.IsBodyHtml = true;
133
+                mail.BodyEncoding = Encoding.ASCII;
134
+                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
135
+                mail.Sender = new MailAddress(host.User, "UniVate Properties");
136
+                mail.From = new MailAddress(mm.ToAddress, "Admin");
137
+
138
+                smtp.UseDefaultCredentials = host.NeedsAuthorize;
139
+                smtp.Credentials = host.GetNetworkCredential();
140
+                smtp.EnableSsl = host.UseSSL;
141
+                smtp.Send(mail);
142
+            }            
135 143
         }
136 144
 
137
-        public void ForgotPassword(MailModel mm, string link)
145
+        public void ForgotPassword(Individual toPerson, string link)
138 146
         {
139
-            string name = mm.Name;
140
-            string email = mm.Email;
141
-
142
-            from = new MailboxAddress("Admin", mm.FromAddress);
143
-
144
-            to = new MailboxAddress("User", mm.ToAddress);
145
-
146
-            messageObj.From.Add(from);
147
-            messageObj.To.Add(to);
148
-
149
-            messageObj.Subject = "Uni-Vate - Password Reset Request";
150
-
151
-            bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
152
-                "<h4>Request to reset password</h4>" +
153
-                "<h4>Dear " + name + ", </h4>" +
154
-                "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
155
-                "<div>" +
156
-                "<h5>Please follow the link below to reset your password:</h5>" +
157
-                "<h3>"+ link + "</h3>" +
158
-                "<br />"+
159
-                "<h4>Thank You</h4>" +
160
-                "<h4>Team Uni-Vate</h4>" +
161
-                "</div>" +
162
-                "</div>" +
163
-                "</div>";
164
-
165
-            messageObj.Body = bodyBuilder.ToMessageBody();
166
-
167
-            client.Connect("smtp.gmail.com", 465, true);
168
-            client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
169
-
170
-            client.Send(messageObj);
171
-            client.Disconnect(true);
172
-            client.Dispose();
147
+            string body = "<div style=\"margin: 5px\">" + 
148
+                           "<h4>Dear " + toPerson.FullName + "</h4>" +
149
+                           "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
150
+                           "<div>" +
151
+                           "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
152
+                           "<br />"+
153
+                           "<h4>Thank You</h4>" +
154
+                           "<h4>Team Uni-Vate</h4>" +
155
+                           "</div>" +
156
+                           "</div>";
157
+
158
+            var host = _dbContext.Hosts.FirstOrDefault();
159
+            using (SmtpClient smtp = new SmtpClient(host.Host))
160
+            {
161
+                MailMessage mail = new MailMessage();
162
+                mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
163
+                mail.Subject = "Uni-Vate - Password Reset Request";
164
+                mail.Body = body;
165
+                mail.IsBodyHtml = true;
166
+                mail.BodyEncoding = Encoding.ASCII;
167
+                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
168
+                mail.Sender = new MailAddress(host.User, "UniVate Properties");
169
+                mail.From = mail.Sender;
170
+
171
+                smtp.UseDefaultCredentials = host.NeedsAuthorize;
172
+                smtp.Credentials = host.GetNetworkCredential();
173
+                smtp.EnableSsl = host.UseSSL;
174
+                smtp.Send(mail);
175
+            }                        
173 176
         }
174 177
 
175 178
         public void AddRecipient(MailRecipient rec)

+ 2
- 7
UnivateProperties_API/Repository/Users/RegisterRepository.cs View File

@@ -377,7 +377,7 @@ namespace UnivateProperties_API.Repository.Users
377 377
         public void ForgotPasswordMailCheck(string mail)
378 378
         {
379 379
             var indiv = _dbContext.Individuals.Where(x => x.Email.ToUpper() == mail.ToUpper()).FirstOrDefault();
380
-            var mailer = new MailRepository();
380
+            var mailer = new MailRepository(_dbContext);
381 381
             if (indiv != null)
382 382
             {
383 383
                 byte[] time = BitConverter.GetBytes(GetTime());
@@ -411,12 +411,7 @@ namespace UnivateProperties_API.Repository.Users
411 411
                 //string url = "http://training.provision-sa.com:122/#/forgotPasswordReset/" + linkStr;
412 412
                 string url = "https://www.pvsl.co.za:97/#/forgotPasswordReset/" + linkStr;
413 413
 
414
-                mailer.ForgotPassword(new MailModel
415
-                {
416
-                    ToAddress = mail,
417
-                    FromAddress = "jlouw365@gmail.com",
418
-                    Name = indiv.Name
419
-                }, url);
414
+                mailer.ForgotPassword(indiv, url);
420 415
             }
421 416
             else
422 417
             {

Loading…
Cancel
Save