Files

164 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace KeficoMailService
{
public class Manager
{
public string Host { get; set; } = "https://service.kefico.co.kr";
public readonly string GetFormHTML = "/KEFICO.XML/MAIL/MailManager.asmx";
public readonly string SendMailDetailHTML = "/KEFICO.XML/MAIL/MailManager.asmx";
public string GetFormHtml(HtmlFormType formType)
{
string result = string.Empty;
string url = $"{Host}{GetFormHTML}";
string soapEnvelope = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body>
<GetFormHTML xmlns=""http://kefico.co.kr/"">
<Type>
<SystemName>{formType.SystemName}</SystemName>
<FormType>{formType.FormType}</FormType>
</Type>
</GetFormHTML>
</soap12:Body>
</soap12:Envelope>";
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"Request URL: {url}");
Console.WriteLine($"Request soap: {soapEnvelope}");
byte[] data = Encoding.UTF8.GetBytes(soapEnvelope);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=utf-8";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
//decode
XDocument doc = XDocument.Parse(result);
XNamespace ns = "http://kefico.co.kr/";
string htmlEncoded =
doc.Descendants(ns + "GetFormHTMLResult").First().Value;
// HTML Decode
result = WebUtility.HtmlDecode(htmlEncoded);
result = WebUtility.HtmlDecode(htmlEncoded);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("");
Console.WriteLine("Response");
Console.WriteLine(result);
}
return result;
}
public string SendMailDetail(Address from, List<Address> to, List<Address> cc, List<Address> bcc, string subject, string body, MailType type)
{
string result = string.Empty;
string url = $"{Host}{SendMailDetailHTML}";
string soapEnvelope = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body>
<SendMailDetail xmlns=""http://kefico.co.kr/"">
<From>
<AddressMail>{from.AddressMail}</AddressMail>
<AddressName>{from.AddressName}</AddressName>
</From>
<To>{string.Join("",to.Select(x=> $@"
<Address>
<AddressMail>{x.AddressMail}</AddressMail>
<AddressName>{x.AddressName}</AddressName>
</Address>"))}
</To>
<CC>{string.Join("",cc.Select(x => $@"
<Address>
<AddressMail>{x.AddressMail}</AddressMail>
<AddressName>{x.AddressName}</AddressName>
</Address>"))}
</CC>
<Bcc>{string.Join("", bcc.Select(x => $@"
<Address>
<AddressMail>{x.AddressMail}</AddressMail>
<AddressName>{x.AddressName}</AddressName>
</Address>"))}
</Bcc>
<Title>{subject}</Title>
<Body>{body}</Body>
<Type>{type.ToString()}</Type>
</SendMailDetail>
</soap12:Body>
</soap12:Envelope>";
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("");
Console.WriteLine($"Request URL: {url}");
Console.WriteLine($"Request soap: {soapEnvelope}");
byte[] data = Encoding.UTF8.GetBytes(soapEnvelope);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=utf-8";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
using (WebResponse response = request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
//decode
XDocument doc = XDocument.Parse(result);
XNamespace ns = "http://kefico.co.kr/";
string htmlEncoded =
doc.Descendants(ns + "SendMailDetailResponse").First().Value;
//decode
result = WebUtility.HtmlDecode(htmlEncoded);
result = WebUtility.HtmlDecode(result);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("");
Console.WriteLine("Response");
Console.WriteLine(result);
}
return result;
}
}
}