You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
9.3 KiB
192 lines
9.3 KiB
using API_NetFramework.Controllers;
|
|
using API_NetFramework.Models;
|
|
using Database;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlTypes;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web.Http;
|
|
using System.Web.Management;
|
|
using System.Configuration;
|
|
using SecuringWebApiUsingApiKey.Middleware;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
|
|
namespace OnDocAPI_NetFramework.Controllers
|
|
{
|
|
public class MailController : ApiController
|
|
{
|
|
string tokenfunction = "Mail";
|
|
|
|
//[HttpGet]
|
|
//[Route("API/SendPSMail")]
|
|
private IHttpActionResult SendPSMail(string MailID, string empfaenger, string betreff, string message, string dokumentid, string ondoclink, string absender, string bewilligungid)
|
|
{
|
|
string connectionstring = StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["EDOKAConnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm");
|
|
string URI = "";
|
|
|
|
APILogging.Log((HttpRequestMessage)Request, "Mailversand: " + empfaenger+""+betreff, LogLevelType.Debug);
|
|
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
|
|
{
|
|
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
|
|
}
|
|
|
|
if (Regex.IsMatch(empfaenger, @"^\d+$") == false &&!empfaenger.ToUpper().Contains("@TKB.CH"))
|
|
{
|
|
return Content(HttpStatusCode.Forbidden, empfaenger + ": Email nicht bei der TKB - Mail nicht versandt");
|
|
}
|
|
string s = "";
|
|
DB db = new DB(connectionstring);
|
|
try
|
|
{
|
|
db.clear_parameter();
|
|
db.add_parameter("@mailid", MailID.ToString());
|
|
db.add_parameter("@empf",empfaenger);
|
|
db.add_parameter("@dokid",dokumentid);
|
|
db.add_parameter("@absender", absender);
|
|
db.add_parameter("@bewilligungid", bewilligungid);
|
|
db.Get_Tabledata("sp_ondoc_maildaten", true, false);
|
|
if (message == null) { message = ""; }
|
|
if (betreff == null) { betreff = ""; }
|
|
if (betreff == "") { betreff = db.dsdaten.Tables[0].Rows[0][1].ToString(); }
|
|
if (message == "") { message = db.dsdaten.Tables[0].Rows[0][2].ToString(); }
|
|
if (Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][3]) == 0)
|
|
{
|
|
db = null;
|
|
return Content(HttpStatusCode.OK, empfaenger + ": Mail gem. MA-Einstelung nicht zugestellt");
|
|
}
|
|
empfaenger = db.dsdaten.Tables[0].Rows[0][0].ToString();
|
|
|
|
//if (Regex.IsMatch(empfaenger, @"^\d+$") == true)
|
|
//{
|
|
|
|
// db.Get_Tabledata("select email from mitarbeiter where mitarbeiternr=" + empfaenger, false, true);
|
|
// if (db.dsdaten.Tables[0].Rows.Count > 0)
|
|
// {
|
|
// empfaenger = db.dsdaten.Tables[0].Rows[0][0].ToString();
|
|
// }
|
|
// else
|
|
// {
|
|
// return Content(HttpStatusCode.InternalServerError, "Empfäger " + empfaenger + " konnte nicht ermittelt werden");
|
|
// }
|
|
//}
|
|
|
|
//if (message == null) { message = ""; }
|
|
//if (betreff == null) { betreff = ""; }
|
|
//URI = System.Configuration.ConfigurationManager.AppSettings["URI"];
|
|
//db.Get_Tabledata("Select * from ondoc_mail where id=" + MailID, false, true);
|
|
//if (betreff == "") { betreff = db.dsdaten.Tables[0].Rows[0][1].ToString(); }
|
|
//if (message == "") {message= db.dsdaten.Tables[0].Rows[0][2].ToString(); }
|
|
//betreff = betreff.Replace("&&dokumentid&&", dokumentid);
|
|
//message = message.Replace("&&dokumentid&&", dokumentid);
|
|
//message = message.Replace("&&OnDocLink&&", URI);
|
|
db = null;
|
|
|
|
s = System.Configuration.ConfigurationManager.AppSettings["MailParam"];
|
|
|
|
|
|
s = s.Replace("$$empfaenger$$", empfaenger);
|
|
s = s.Replace("$$betreff$$", betreff);
|
|
s = s.Replace("$$body$$", message);
|
|
|
|
|
|
string debugdir = System.Configuration.ConfigurationManager.AppSettings["DebugDir"];
|
|
string tmpfile = debugdir + @"\Mail_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".ps1";
|
|
System.IO.File.WriteAllText(tmpfile, s,new UTF8Encoding(true));
|
|
//StreamWriter writer = new StreamWriter(tmpfile);
|
|
//writer.Write(s);
|
|
//writer.Close();
|
|
//writer.Dispose();
|
|
System.Diagnostics.Process.Start("powershell.exe", tmpfile);
|
|
APILogging.Log((HttpRequestMessage)Request, "Mail Versand: " + s, LogLevelType.Debug);
|
|
//return Content(HttpStatusCode.OK, "Mail versand noch nicht implementiert");
|
|
return Content(HttpStatusCode.OK, empfaenger + ": Mail versandt");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
APILogging.Log((HttpRequestMessage)Request, "Mail Versand NOK: " + e.Message+" " + s, LogLevelType.Debug);
|
|
return Content(HttpStatusCode.InternalServerError, e.Message);
|
|
}
|
|
}
|
|
[HttpGet]
|
|
[Route("API/SendMail")]
|
|
public IHttpActionResult SendMail(string MailID, string empfaenger, string betreff, string message, string dokumentid, string ondoclink, string absender, string bewilligungid)
|
|
{
|
|
string connectionstring = StringCipher.Decrypt(ConfigurationManager.ConnectionStrings["EDOKAConnectionstring"].ConnectionString, "i%!k!7pab%bNLdA5hE4pkR4XaB%E^jB3d9tHuQ4pbF&BZjF7SB#WBWit5#HrbJiLrLVm");
|
|
string URI = "";
|
|
|
|
APILogging.Log((HttpRequestMessage)Request, "Mailversand: " + empfaenger + "" + betreff, LogLevelType.Debug);
|
|
if (SecuringWebApiUsingApiKey.Middleware.ApiKeyMiddleware.Authorized((HttpRequestMessage)Request, tokenfunction) == false)
|
|
{
|
|
return Content(HttpStatusCode.Unauthorized, "Invalid Token or API-Key");
|
|
}
|
|
|
|
if (Regex.IsMatch(empfaenger, @"^\d+$") == false && !empfaenger.ToUpper().Contains("@TKB.CH"))
|
|
{
|
|
return Content(HttpStatusCode.Forbidden, empfaenger + ": Email nicht bei der TKB - Mail nicht versandt");
|
|
}
|
|
string s = "";
|
|
DB db = new DB(connectionstring);
|
|
try
|
|
{
|
|
db.clear_parameter();
|
|
db.add_parameter("@mailid", MailID.ToString());
|
|
db.add_parameter("@empf", empfaenger);
|
|
db.add_parameter("@dokid", dokumentid);
|
|
db.add_parameter("@absender", absender);
|
|
db.add_parameter("@bewilligungid", bewilligungid);
|
|
db.Get_Tabledata("sp_ondoc_maildaten", true, false);
|
|
if (message == null) { message = ""; }
|
|
if (betreff == null) { betreff = ""; }
|
|
if (betreff == "") { betreff = db.dsdaten.Tables[0].Rows[0][1].ToString(); }
|
|
if (message == "") { message = db.dsdaten.Tables[0].Rows[0][2].ToString(); }
|
|
if (Convert.ToInt32(db.dsdaten.Tables[0].Rows[0][3]) == 0)
|
|
{
|
|
db = null;
|
|
return Content(HttpStatusCode.OK, empfaenger + ": Mail gem. MA-Einstelung nicht zugestellt");
|
|
}
|
|
empfaenger = db.dsdaten.Tables[0].Rows[0][0].ToString();
|
|
db = null;
|
|
|
|
MailMessage mail = new MailMessage();
|
|
SmtpClient SmtpServer = new SmtpClient();
|
|
mail.To.Add(empfaenger);
|
|
mail.From = new MailAddress("OnDoc@tkb.ch");
|
|
mail.Subject = betreff;
|
|
mail.IsBodyHtml = true;
|
|
mail.Body = message;
|
|
SmtpServer.Host = "smtp.tgcorp.ch";
|
|
SmtpServer.Port = 25;
|
|
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
|
|
try
|
|
{
|
|
SmtpServer.Send(mail);
|
|
APILogging.Log((HttpRequestMessage)Request, "SMTPMail Versand: " + empfaenger, LogLevelType.Debug);
|
|
return Content(HttpStatusCode.OK, empfaenger + ": Mail versandt");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
APILogging.Log((HttpRequestMessage)Request, "Mail Versand NOK: " + ex.Message + " " + empfaenger, LogLevelType.Debug);
|
|
if (ex.InnerException != null)
|
|
{
|
|
APILogging.Log((HttpRequestMessage)Request, "Mail Versand NOK - Exception Inner: " + ex.InnerException, LogLevelType.Debug);
|
|
}
|
|
return Content(HttpStatusCode.InternalServerError, ex.Message);
|
|
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
APILogging.Log((HttpRequestMessage)Request, "Mail Versand NOK: " + e.Message + " " + s, LogLevelType.Debug);
|
|
return Content(HttpStatusCode.InternalServerError, e.Message);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|