main
嘉祥 詹 2025-06-27 17:22:35 +08:00
parent 5d6754d208
commit ad4b18237c
3 changed files with 92 additions and 196 deletions

View File

@ -10,6 +10,7 @@
<PackageReference Include="Dapper" Version="2.0.143" /> <PackageReference Include="Dapper" Version="2.0.143" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" /> <PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="MailKit" Version="4.1.0" /> <PackageReference Include="MailKit" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Cors.zh-Hant" Version="5.3.0" />
<PackageReference Include="MimeKit" Version="4.1.0" /> <PackageReference Include="MimeKit" Version="4.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NPOI" Version="2.6.1" /> <PackageReference Include="NPOI" Version="2.6.1" />

View File

@ -1,52 +1,20 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Dapper; using Dapper;
using System.Data;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Web;
using System.Text;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
using System.IO;
using System.Threading;
using System.Dynamic;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.XSSF;
using NPOI.XSSF.UserModel;
using NPOI.POIFS;
using NPOI.Util;
using Microsoft.Extensions.Configuration;
using Org.BouncyCastle.Ocsp;
using System.Security.Policy;
using NPOI.SS.Formula.Functions;
using static DbTableClass; using static DbTableClass;
using System.Runtime.InteropServices.ObjectiveC;
using static System.Net.WebRequestMethods;
using MimeKit; using MimeKit;
using MailKit.Net.Smtp;
using MailKit.Security; using MailKit.Security;
using System.Net.Mail; using System.Net.Mail;
using System.Net.Mime; using System.Net.Mime;
using NPOI.SS.UserModel;
using NPOI.XWPF.UserModel;
using System.Reflection;
namespace Bremen_ESG.Controllers namespace Bremen_ESG.Controllers
{ {
[EnableCors("any")]
[Route("Api")] [Route("Api")]
public class ApiController : ControllerBase public class ApiController : ControllerBase
@ -63,7 +31,8 @@ namespace Bremen_ESG.Controllers
this._hostingEnvironment = webHostEnvironment; this._hostingEnvironment = webHostEnvironment;
} }
[EnableCors("any")]
[Route("news_list")] [Route("news_list")]
public async Task<ActionResult> News_List(IFormCollection obj) { public async Task<ActionResult> News_List(IFormCollection obj) {
newResult ret = new newResult(); newResult ret = new newResult();
@ -84,7 +53,7 @@ namespace Bremen_ESG.Controllers
} }
[EnableCors("any")]
[Route("esg_message")] [Route("esg_message")]
public async Task<ActionResult> Esg_Message(IFormCollection obj) { public async Task<ActionResult> Esg_Message(IFormCollection obj) {
result ret = new result(); result ret = new result();
@ -169,7 +138,7 @@ namespace Bremen_ESG.Controllers
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
} }
[EnableCors("any")]
[Route("esg_mail")] [Route("esg_mail")]
public async Task<ActionResult> Esg_Mail(IFormCollection obj) { public async Task<ActionResult> Esg_Mail(IFormCollection obj) {
result ret = new result(); result ret = new result();
@ -204,159 +173,70 @@ namespace Bremen_ESG.Controllers
var res = new LinkedResource(fullPath, MediaTypeNames.Image.Jpeg); var res = new LinkedResource(fullPath, MediaTypeNames.Image.Jpeg);
res.ContentId = Guid.NewGuid().ToString(); res.ContentId = Guid.NewGuid().ToString();
html += "<!doctype html>"; html += "<body>";
html += "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office'>"; html += "<table align='center' border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:collapse;width:100%' width='100%'>";
html += ""; html += " <tbody>";
html += "<head>"; html += " <tr>";
html += " <title> ESG </title>"; html += " <td style='border-collapse:collapse'>";
html += " <!--[if !mso]><!-->"; html += " ";
html += " <meta http-equiv='X-UA-Compatible' content='IE=edge'>"; html += " <div style='margin:0px auto;max-width:1000px'>";
html += " <!--<![endif]-->"; html += " <table align='center' border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:collapse;width:100%' width='100%'>";
html += " <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"; html += " <tbody>";
html += " <meta name='viewport' content='width=device-width, initial-scale=1'>"; html += " <tr>";
html += " <style type='text/css'>"; html += " <td style='border-collapse:collapse;direction:ltr;font-size:0px;padding:20px 0;text-align:center' align='center'>";
html += " #outlook a {"; html += " ";
html += " padding: 0;"; html += " <div class='m_-2162171852830059364mj-column-per-100' style='font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%'>";
html += " }"; html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:collapse;vertical-align:top' width='100%' valign='top'>";
html += ""; html += " <tbody>";
html += " body {"; html += " <tr>";
html += " margin: 0;"; html += " <td align='center' style='border-collapse:collapse;font-size:0px;padding:10px 25px;padding-top:40px;padding-bottom:20px;word-break:break-word'>";
html += " padding: 0;"; html += " <div style=\"font-family:'Source Han Sans','Noto Sans CJK TC',sans-serif;font-size:28px;font-weight:500;letter-spacing:1px;line-height:40px;text-align:center;text-transform:uppercase;color:#163713\">您好謝謝您填寫ESG快速檢測<br>詳細的測驗結果分析,請點擊以下連結查看:</div>";
html += " -webkit-text-size-adjust: 100%;"; html += " </td>";
html += " -ms-text-size-adjust: 100%;"; html += " </tr>";
html += " }"; html += " <tr>";
html += ""; html += " <td align='center' style='border-collapse:collapse;font-size:0px;padding:10px 25px;word-break:break-word'>";
html += " table,"; html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:separate;line-height:100%'>";
html += " td {"; html += " <tbody><tr>";
html += " border-collapse: collapse;"; html += " <td align='center' bgcolor='#2c5228' role='presentation' style='border-collapse:collapse;border:none;border-radius:9999px;background:#2c5228' valign='middle'>";
html += " mso-table-lspace: 0pt;"; html += " <a href='" + target_link + "' style=\"display:inline-block;background:#2c5228;color:#ffffff;font-family:'Source Han Sans','Noto Sans CJK TC',sans-serif;font-size:28px;font-weight:normal;line-height:120%;margin:0;text-decoration:none;text-transform:none;padding:10px 25px;border-radius:9999px\" target='_blank' data-saferedirecturl='" + target_link + "'> 查看結果 </a>";
html += " mso-table-rspace: 0pt;"; html += " </td>";
html += " }"; html += " </tr>";
html += ""; html += " </tbody></table>";
html += " img {"; html += " </td>";
html += " border: 0;"; html += " </tr>";
html += " height: auto;"; html += " <tr>";
html += " line-height: 100%;"; html += " <td align='center' style='border-collapse:collapse;font-size:0px;padding:0;padding-top:40px;word-break:break-word'>";
html += " outline: none;"; html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:collapse;border-spacing:0px'>";
html += " text-decoration: none;"; html += " <tbody>";
html += " -ms-interpolation-mode: bicubic;"; html += " <tr>";
html += " }"; html += " <td style='border-collapse:collapse;width:800px' width='800'>";
html += ""; html += " <a href='" + target_link + "' target='_blank' data-saferedirecturl='https://www.google.com/url?q=" + target_link + "'>";
html += " p {"; html += " <img alt='' height='auto' src='cid:" + res.ContentId + "' style='line-height:100%;border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px' width='800' class='CToWUd' data-bit='iit'>";
html += " display: block;"; html += " </a>";
html += " margin: 13px 0;"; html += " </td>";
html += " }"; html += " </tr>";
html += " </style>"; html += " </tbody>";
html += " <!--[if mso]>"; html += " </table>";
html += " <noscript>"; html += " </td>";
html += " <xml>"; html += " </tr>";
html += " <o:OfficeDocumentSettings>"; html += " </tbody>";
html += " <o:AllowPNG/>"; html += " </table>";
html += " <o:PixelsPerInch>96</o:PixelsPerInch>"; html += " </div>";
html += " </o:OfficeDocumentSettings>"; html += " ";
html += " </xml>"; html += " </td>";
html += " </noscript>"; html += " </tr>";
html += " <![endif]-->"; html += " </tbody>";
html += " <!--[if lte mso 11]>"; html += " </table>";
html += " <style type='text/css'>"; html += " </div>";
html += " .mj-outlook-group-fix { width:100% !important; }"; html += " ";
html += " </style>"; html += " </td>";
html += " <![endif]-->"; html += " </tr>";
html += " <style type='text/css'>"; html += " </tbody>";
html += " @media only screen and (min-width:480px) {"; html += "</table>";
html += " .mj-column-per-100 {";
html += " width: 100% !important;";
html += " max-width: 100%;";
html += " }";
html += " }";
html += " </style>";
html += " <style media='screen and (min-width:480px)'>";
html += " .moz-text-html .mj-column-per-100 {";
html += " width: 100% !important;";
html += " max-width: 100%;";
html += " }";
html += " </style>";
html += " <style type='text/css'>";
html += " @media only screen and (max-width:480px) {";
html += " table.mj-full-width-mobile {";
html += " width: 100% !important;";
html += " }";
html += "";
html += " td.mj-full-width-mobile {";
html += " width: auto !important;";
html += " }";
html += " }";
html += " </style>";
html += "</head>";
html += "";
html += "<body style='word-spacing:normal;'>";
html += " <div style=''>";
html += " <table align='center' border='0' cellpadding='0' cellspacing='0' role='presentation' style='width:100%;'>";
html += " <tbody>";
html += " <tr>";
html += " <td>";
html += " <!--[if mso | IE]><table align='center' border='0' cellpadding='0' cellspacing='0' class='' style='width:1000px;' width='1000' ><tr><td style='line-height:0px;font-size:0px;mso-line-height-rule:exactly;'><![endif]-->";
html += " <div style='margin:0px auto;max-width:1000px;'>";
html += " <table align='center' border='0' cellpadding='0' cellspacing='0' role='presentation' style='width:100%;'>";
html += " <tbody>";
html += " <tr>";
html += " <td style='direction:ltr;font-size:0px;padding:20px 0;text-align:center;'>";
html += " <!--[if mso | IE]><table role='presentation' border='0' cellpadding='0' cellspacing='0'><tr><td class='' style='vertical-align:top;width:1000px;' ><![endif]-->";
html += " <div class='mj-column-per-100 mj-outlook-group-fix' style='font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;'>";
html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='vertical-align:top;' width='100%'>";
html += " <tbody>";
html += " <tr>";
html += " <td align='center' style='font-size:0px;padding:10px 25px;padding-top:40px;padding-bottom:20px;word-break:break-word;'>";
html += " <div style='font-family:'Source Han Sans', 'Noto Sans CJK TC', '思源黑體', sans-serif;font-size:28px;font-weight:500;letter-spacing:1px;line-height:40px;text-align:center;text-transform:uppercase;color:#163713;'>您好謝謝您填寫ESG快速檢測<br>詳細的測驗結果分析,請點擊以下連結查看:</div>";
html += " </td>";
html += " </tr>";
html += " <tr>";
html += " <td align='center' vertical-align='middle' style='font-size:0px;padding:10px 25px;word-break:break-word;'>";
html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:separate;line-height:100%;'>";
html += " <tr>";
html += " <td align='center' bgcolor='#2c5228' role='presentation' style='border:none;border-radius:9999px;cursor:auto;mso-padding-alt:10px 25px;background:#2c5228;' valign='middle'>";
html += " <a href='" + target_link + "' style='display:inline-block;background:#2c5228;color:#ffffff;font-family:'Source Han Sans', 'Noto Sans CJK TC', '思源黑體', sans-serif;font-size:28px;font-weight:normal;line-height:120%;margin:0;text-decoration:none;text-transform:none;padding:10px 60px;mso-padding-alt:0px;border-radius:9999px;' target='_blank'> 查看結果 </a>";
html += " </td>";
html += " </tr>";
html += " </table>";
html += " </td>";
html += " </tr>";
html += " <tr>";
html += " <td align='center' style='font-size:0px;padding:0;padding-top:60px;word-break:break-word;'>";
html += " <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='border-collapse:collapse;border-spacing:0px;'>";
html += " <tbody>";
html += " <tr>";
html += " <td style='width:900px;'>";
html += " <a href='https://google.com' target='_blank'>";
html += " <img alt='' height='auto' src='cid:" + res.ContentId + "' style='border:0;display:block;outline:none;text-decoration:none;height:auto;width:100%;font-size:13px;' width='900' />";
html += " </a>";
html += " </td>";
html += " </tr>";
html += " </tbody>";
html += " </table>";
html += " </td>";
html += " </tr>";
html += " </tbody>";
html += " </table>";
html += " </div>";
html += " <!--[if mso | IE]></td></tr></table><![endif]-->";
html += " </td>";
html += " </tr>";
html += " </tbody>";
html += " </table>";
html += " </div>";
html += " <!--[if mso | IE]></td></tr></table><![endif]-->";
html += " </td>";
html += " </tr>";
html += " </tbody>";
html += " </table>";
html += " </div>";
html += "</body>"; html += "</body>";
html += "";
html += "</html>";
//建立AlternativeView //建立AlternativeView
var altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html); var altView = AlternateView.CreateAlternateViewFromString(html, null, "text/html");
//將圖檔資源加入AlternativeView //將圖檔資源加入AlternativeView
altView.LinkedResources.Add(res); altView.LinkedResources.Add(res);
@ -401,7 +281,7 @@ namespace Bremen_ESG.Controllers
mail.AlternateViews.Add(altView); mail.AlternateViews.Add(altView);
mail.To.Add(toEmail); mail.To.Add(toEmail);
mail.Bcc.Add("dk96pccu@gmail.com"); mail.Bcc.Add("dk96pccu@gmail.com,sunny.lin@bremen.com.tw");
mail.From = new MailAddress(GlobalClass.appsettings("MailServer:smtp_username"), "ESG快速檢測結果分析", System.Text.Encoding.UTF8); mail.From = new MailAddress(GlobalClass.appsettings("MailServer:smtp_username"), "ESG快速檢測結果分析", System.Text.Encoding.UTF8);
mail.Subject = "ESG快速檢測結果分析"; mail.Subject = "ESG快速檢測結果分析";
await client.SendMailAsync(mail).ContinueWith(task => await client.SendMailAsync(mail).ContinueWith(task =>

View File

@ -49,22 +49,33 @@ var builder = WebApplication.CreateBuilder(args);
// }); // });
//}); //});
// Add services to the container.
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowAll", builder => options.AddPolicy(name: "any", builder =>
builder.AllowAnyOrigin() {
.AllowAnyMethod() builder.WithOrigins("https://www.bremen.com.tw",
.AllowAnyHeader()); "https://bremen.com.tw")
.AllowAnyHeader()
.AllowAnyMethod();
});
}); });
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
//builder.Services.AddControllers(); //builder.Services.AddControllers();
builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
@ -80,13 +91,17 @@ defaultFilesOptions.DefaultFileNames.Add("iisstart.htm");
app.UseDefaultFiles(defaultFilesOptions); app.UseDefaultFiles(defaultFilesOptions);
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
//app.UseCors("AllowAnyOrigins");
//app.UseCors("AllowAll"); app.UseCors("any");
//app.UseCors();
app.UseAuthorization(); //app.UseAuthorization();
app.MapControllerRoute( app.MapControllerRoute(
name: "default", name: "default",