Преглед изворни кода

Seperate Mail Controller for contact us

master
30117125 пре 4 година
родитељ
комит
2ad117817b

+ 42
- 0
UnivateProperties_API/Controllers/Communication/MailController.cs Прегледај датотеку

@@ -0,0 +1,42 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using System.Threading.Tasks;
5
+using Microsoft.AspNetCore.Http;
6
+using Microsoft.AspNetCore.Mvc;
7
+using UnivateProperties_API.Model.Communication;
8
+using UnivateProperties_API.Repository.Communication;
9
+
10
+namespace UnivateProperties_API.Controllers.Communication
11
+{
12
+    [Route("api/[controller]")]
13
+    [ApiController]
14
+    public class MailController : ControllerBase
15
+    {
16
+        private readonly IMailRepository _repo;
17
+        public MailController(IMailRepository repo)
18
+        {
19
+            _repo = repo;
20
+        }
21
+
22
+        [HttpPost("{id}")]
23
+        public IActionResult Post(int id, [FromBody] MailModel mm)
24
+        {
25
+            try
26
+            {
27
+                switch (id)
28
+                {
29
+                    case 0:
30
+                        _repo.ContactUs(mm);
31
+                        break;
32
+                }
33
+                return new OkResult();
34
+            }
35
+            catch
36
+            {
37
+                return new BadRequestResult();
38
+            }
39
+
40
+        }
41
+    }
42
+}

+ 25
- 0
UnivateProperties_API/Model/Communication/MailModel.cs Прегледај датотеку

@@ -0,0 +1,25 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.ComponentModel.DataAnnotations;
4
+using System.Linq;
5
+using System.Threading.Tasks;
6
+
7
+namespace UnivateProperties_API.Model.Communication
8
+{
9
+    public class MailModel
10
+    {
11
+        public string ToAddress { get; set; }
12
+
13
+        public string FromAddress { get; set; }
14
+
15
+        public string Name { get; set; }
16
+
17
+        public string Email { get; set; }
18
+
19
+        public string Phone { get; set; }
20
+
21
+        public string Property { get; set; }
22
+
23
+        public string Message { get; set; }
24
+    }
25
+}

+ 59
- 0
UnivateProperties_API/Repository/Communication/MailRepository.cs Прегледај датотеку

@@ -0,0 +1,59 @@
1
+using MailKit.Net.Smtp;
2
+using MimeKit;
3
+using UnivateProperties_API.Model.Communication;
4
+
5
+namespace UnivateProperties_API.Repository.Communication
6
+{
7
+    public interface IMailRepository
8
+    {
9
+        void ContactUs(MailModel mm);
10
+    }
11
+
12
+    public class MailRepository : IMailRepository
13
+    {
14
+        MimeMessage messageObj = new MimeMessage();
15
+        MailboxAddress from;
16
+        MailboxAddress to;
17
+        BodyBuilder bodyBuilder = new BodyBuilder();
18
+        SmtpClient client = new SmtpClient();
19
+
20
+        public void ContactUs(MailModel mm)
21
+        {
22
+            string property = mm.Property;
23
+            string phone = mm.Phone;
24
+            string name = mm.Name;
25
+            string email = mm.Email;
26
+            string message = mm.Message;
27
+
28
+            from = new MailboxAddress("Admin", mm.FromAddress);
29
+
30
+            to = new MailboxAddress("User", mm.ToAddress);
31
+
32
+            messageObj.From.Add(from);
33
+            messageObj.To.Add(to);
34
+
35
+            messageObj.Subject = "Uni-Vate - New Contact Request";
36
+
37
+            bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
38
+                "<h4>Contact from: "+  name +"!</h4>" +
39
+                "<h4>Email: "+ email +"</h4>" +
40
+                "<h4>Phone: " + phone + "</h4>" +
41
+                "<h4>Property: " + property + "</h4>" +
42
+                "<div>" +
43
+                "<h4>Message: </h4>" +
44
+                "<p>" + message + "</p>" +
45
+                "</div>" +
46
+                "</div>" +
47
+                "</div>";
48
+
49
+            messageObj.Body = bodyBuilder.ToMessageBody();
50
+
51
+            client.Connect("smtp.gmail.com", 465, true);
52
+            client.Authenticate("jlouw365@gmail.com", "setskohatxpsceqo");
53
+
54
+            client.Send(messageObj);
55
+            client.Disconnect(true);
56
+            client.Dispose();
57
+        }
58
+    }
59
+}

+ 1
- 0
UnivateProperties_API/Startup.cs Прегледај датотеку

@@ -149,6 +149,7 @@ namespace UnivateProperties_API
149 149
             services.AddTransient<IRepository<Email>, EmailRepository>();
150 150
             services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
151 151
             services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
152
+            services.AddTransient<IMailRepository, MailRepository>();
152 153
             #endregion Communication
153 154
             #region Logs 
154 155
             services.AddTransient<ISearchLogRepository, SearchLogRepository>();

+ 3
- 1
UnivateProperties_API/UnivateProperties_API.csproj Прегледај датотеку

@@ -14,11 +14,13 @@
14 14
     <PackageReference Include="Abp.AutoMapper" Version="4.8.1" />
15 15
     <PackageReference Include="AutoMapper" Version="9.0.0" />
16 16
     <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="6.0.0" />
17
+    <PackageReference Include="MailKit" Version="2.8.0" />
17 18
     <PackageReference Include="Microsoft.AspNet.WebApi.Cors" Version="5.2.7" />
18 19
     <PackageReference Include="Microsoft.AspNetCore.App" />
19 20
     <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
20 21
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
21
-    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
22
+    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.4" />
23
+    <PackageReference Include="MimeKit" Version="2.9.1" />
22 24
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />
23 25
     <PackageReference Include="RestSharp" Version="106.11.4" />
24 26
     <PackageReference Include="System.Drawing.Common" Version="4.6.0" />

Loading…
Откажи
Сачувај