新增 esg測驗結果通知信api
parent
cc6d56b7cc
commit
5d6754d208
|
|
@ -1,7 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,11 @@ using static System.Net.WebRequestMethods;
|
||||||
using MimeKit;
|
using MimeKit;
|
||||||
using MailKit.Net.Smtp;
|
using MailKit.Net.Smtp;
|
||||||
using MailKit.Security;
|
using MailKit.Security;
|
||||||
|
using System.Net.Mail;
|
||||||
|
using System.Net.Mime;
|
||||||
|
using NPOI.SS.UserModel;
|
||||||
|
using NPOI.XWPF.UserModel;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
|
||||||
namespace Bremen_ESG.Controllers
|
namespace Bremen_ESG.Controllers
|
||||||
|
|
@ -164,6 +169,206 @@ namespace Bremen_ESG.Controllers
|
||||||
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Route("esg_mail")]
|
||||||
|
public async Task<ActionResult> Esg_Mail(IFormCollection obj) {
|
||||||
|
result ret = new result();
|
||||||
|
|
||||||
|
string target_email = obj["email"].ToString();
|
||||||
|
string target_link = obj["link"].ToString();
|
||||||
|
|
||||||
|
string err_msg = "";
|
||||||
|
|
||||||
|
if (GlobalClass.isURL(target_link) == false)
|
||||||
|
{
|
||||||
|
err_msg += "無有效結果連結\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (GlobalClass.isEmail(target_email) == false)
|
||||||
|
{
|
||||||
|
err_msg += "無有效Email\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (err_msg != "")
|
||||||
|
{
|
||||||
|
ret.ret = "no";
|
||||||
|
ret.message = err_msg;
|
||||||
|
ret.err_code = "0001";
|
||||||
|
|
||||||
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
string html = $@"";
|
||||||
|
|
||||||
|
string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/email_pic/email_footer.jpg");
|
||||||
|
var res = new LinkedResource(fullPath, MediaTypeNames.Image.Jpeg);
|
||||||
|
res.ContentId = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
html += "<!doctype html>";
|
||||||
|
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 += "";
|
||||||
|
html += "<head>";
|
||||||
|
html += " <title> ESG </title>";
|
||||||
|
html += " <!--[if !mso]><!-->";
|
||||||
|
html += " <meta http-equiv='X-UA-Compatible' content='IE=edge'>";
|
||||||
|
html += " <!--<![endif]-->";
|
||||||
|
html += " <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
|
||||||
|
html += " <meta name='viewport' content='width=device-width, initial-scale=1'>";
|
||||||
|
html += " <style type='text/css'>";
|
||||||
|
html += " #outlook a {";
|
||||||
|
html += " padding: 0;";
|
||||||
|
html += " }";
|
||||||
|
html += "";
|
||||||
|
html += " body {";
|
||||||
|
html += " margin: 0;";
|
||||||
|
html += " padding: 0;";
|
||||||
|
html += " -webkit-text-size-adjust: 100%;";
|
||||||
|
html += " -ms-text-size-adjust: 100%;";
|
||||||
|
html += " }";
|
||||||
|
html += "";
|
||||||
|
html += " table,";
|
||||||
|
html += " td {";
|
||||||
|
html += " border-collapse: collapse;";
|
||||||
|
html += " mso-table-lspace: 0pt;";
|
||||||
|
html += " mso-table-rspace: 0pt;";
|
||||||
|
html += " }";
|
||||||
|
html += "";
|
||||||
|
html += " img {";
|
||||||
|
html += " border: 0;";
|
||||||
|
html += " height: auto;";
|
||||||
|
html += " line-height: 100%;";
|
||||||
|
html += " outline: none;";
|
||||||
|
html += " text-decoration: none;";
|
||||||
|
html += " -ms-interpolation-mode: bicubic;";
|
||||||
|
html += " }";
|
||||||
|
html += "";
|
||||||
|
html += " p {";
|
||||||
|
html += " display: block;";
|
||||||
|
html += " margin: 13px 0;";
|
||||||
|
html += " }";
|
||||||
|
html += " </style>";
|
||||||
|
html += " <!--[if mso]>";
|
||||||
|
html += " <noscript>";
|
||||||
|
html += " <xml>";
|
||||||
|
html += " <o:OfficeDocumentSettings>";
|
||||||
|
html += " <o:AllowPNG/>";
|
||||||
|
html += " <o:PixelsPerInch>96</o:PixelsPerInch>";
|
||||||
|
html += " </o:OfficeDocumentSettings>";
|
||||||
|
html += " </xml>";
|
||||||
|
html += " </noscript>";
|
||||||
|
html += " <![endif]-->";
|
||||||
|
html += " <!--[if lte mso 11]>";
|
||||||
|
html += " <style type='text/css'>";
|
||||||
|
html += " .mj-outlook-group-fix { width:100% !important; }";
|
||||||
|
html += " </style>";
|
||||||
|
html += " <![endif]-->";
|
||||||
|
html += " <style type='text/css'>";
|
||||||
|
html += " @media only screen and (min-width:480px) {";
|
||||||
|
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 += "";
|
||||||
|
html += "</html>";
|
||||||
|
|
||||||
|
//建立AlternativeView
|
||||||
|
var altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);
|
||||||
|
//將圖檔資源加入AlternativeView
|
||||||
|
altView.LinkedResources.Add(res);
|
||||||
|
|
||||||
|
await SendEsgResultMailAsync(altView, target_email);
|
||||||
|
|
||||||
|
ret.ret = "yes";
|
||||||
|
|
||||||
|
ret.message = "已寄送結果通知信至" + target_email;
|
||||||
|
|
||||||
|
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
public class newResult
|
public class newResult
|
||||||
{
|
{
|
||||||
public string ret = "no";
|
public string ret = "no";
|
||||||
|
|
@ -181,6 +386,40 @@ namespace Bremen_ESG.Controllers
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task SendEsgResultMailAsync(AlternateView altView, string toEmail) {
|
||||||
|
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
|
||||||
|
client.Host = GlobalClass.appsettings("MailServer:smtp_host");
|
||||||
|
client.Port = int.Parse(GlobalClass.appsettings("MailServer:smtp_port"));
|
||||||
|
client.EnableSsl = true;
|
||||||
|
client.Credentials = new NetworkCredential(
|
||||||
|
GlobalClass.appsettings("MailServer:smtp_username"),
|
||||||
|
GlobalClass.appsettings("MailServer:smtp_password")
|
||||||
|
);
|
||||||
|
|
||||||
|
var mail = new MailMessage();
|
||||||
|
mail.IsBodyHtml = true;
|
||||||
|
|
||||||
|
mail.AlternateViews.Add(altView);
|
||||||
|
mail.To.Add(toEmail);
|
||||||
|
mail.Bcc.Add("dk96pccu@gmail.com");
|
||||||
|
mail.From = new MailAddress(GlobalClass.appsettings("MailServer:smtp_username"), "ESG快速檢測結果分析", System.Text.Encoding.UTF8);
|
||||||
|
mail.Subject = "ESG快速檢測結果分析";
|
||||||
|
await client.SendMailAsync(mail).ContinueWith(task =>
|
||||||
|
{
|
||||||
|
if (task.IsFaulted)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error sending email: " + task.Exception);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Email sent successfully.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
client.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
public async Task SendEmailAsync(MailRequest mailRequest)
|
public async Task SendEmailAsync(MailRequest mailRequest)
|
||||||
{
|
{
|
||||||
var email = new MimeMessage();
|
var email = new MimeMessage();
|
||||||
|
|
@ -203,7 +442,7 @@ namespace Bremen_ESG.Controllers
|
||||||
builder.HtmlBody = mailRequest.Body;
|
builder.HtmlBody = mailRequest.Body;
|
||||||
email.Body = builder.ToMessageBody();
|
email.Body = builder.ToMessageBody();
|
||||||
|
|
||||||
using var smtp = new SmtpClient();
|
using var smtp = new MailKit.Net.Smtp.SmtpClient();
|
||||||
smtp.Connect(GlobalClass.appsettings("MailServer:smtp_host"), int.Parse(GlobalClass.appsettings("MailServer:smtp_port")), SecureSocketOptions.StartTls);
|
smtp.Connect(GlobalClass.appsettings("MailServer:smtp_host"), int.Parse(GlobalClass.appsettings("MailServer:smtp_port")), SecureSocketOptions.StartTls);
|
||||||
smtp.Authenticate(GlobalClass.appsettings("MailServer:smtp_username"), GlobalClass.appsettings("MailServer:smtp_password"));
|
smtp.Authenticate(GlobalClass.appsettings("MailServer:smtp_username"), GlobalClass.appsettings("MailServer:smtp_password"));
|
||||||
await smtp.SendAsync(email);
|
await smtp.SendAsync(email);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
"smtp_host": "smtp.gmail.com",
|
"smtp_host": "smtp.gmail.com",
|
||||||
"smtp_port": 587,
|
"smtp_port": 587,
|
||||||
"smtp_username": "bremen@bremen.com.tw",
|
"smtp_username": "bremen@bremen.com.tw",
|
||||||
"smtp_password": "2776Bremen5485",
|
"smtp_password": "sbkm kcwm opzu zaon",
|
||||||
"sender_email": "bremen@bremen.com.tw",
|
"sender_email": "bremen@bremen.com.tw",
|
||||||
"sender_name": "ESG需求聯絡表通知信"
|
"sender_name": "ESG需求聯絡表通知信"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 242 KiB |
Loading…
Reference in New Issue