MODEL 을 위의 JSON 형태로 구성합니다.
이제 CONTROLLER 에서 HEADER 값을 가져오는 구문이며, BODY 의 JSON 은 MODEL 에 담습니다.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (StreamReader r = new StreamReader(@"D:\SampleSource\ConsoleApp1\ConsoleApp1\notice.json"))
{
string jsonString = r.ReadToEnd();
var modelMain = JsonConvert.DeserializeObject<RootMain>(jsonString);
Console.WriteLine(modelMain.Type);
Console.WriteLine(modelMain.MessageId);
Console.WriteLine(modelMain.Token);
Console.WriteLine(modelMain.TopicArn);
//Console.WriteLine(modelMain.Message);
Console.WriteLine(modelMain.SubscribeURL);
Console.WriteLine(modelMain.SignatureVersion);
Console.WriteLine(modelMain.Signature);
Console.WriteLine(modelMain.SigningCertURL);
Console.WriteLine(modelMain.UnsubscribeURL);
if (modelMain.Type == "Notification")
{
var modelBounced = JsonConvert.DeserializeObject<RootMessageJson>(modelMain.Message);
Console.WriteLine(modelBounced.notificationType); //Bounce
if (modelBounced.bounce.bouncedRecipients.Count() > 0)
{
Console.WriteLine(modelBounced.bounce.bouncedRecipients[0].action); //failed
Console.WriteLine(modelBounced.bounce.bouncedRecipients[0].diagnosticCode); //smtp; 505 this account is unknown user~
Console.WriteLine(modelBounced.bounce.bouncedRecipients[0].emailAddress); //xxxxx@univ.me
Console.WriteLine(modelBounced.bounce.bouncedRecipients[0].status); //5.3.0
}
Console.WriteLine(modelBounced.mail.messageId);
Console.WriteLine(modelBounced.mail.sendingAccountId);
Console.WriteLine(modelBounced.mail.source);
Console.WriteLine(modelBounced.mail.sourceArn);
Console.WriteLine(modelBounced.mail.sourceIp);
Console.WriteLine(modelBounced.mail.timestamp);
Console.WriteLine(modelBounced.mail.commonHeaders.subject);
}
}
Console.ReadKey();
}
}
public class RootMain
{
public string Type { get; set; } = "";
public string MessageId { get; set; } = "";
public string Token { get; set; } = "";
public string TopicArn { get; set; } = "";
public string Message { get; set; } = "";
public string SubscribeURL { get; set; } = "";
public DateTime Timestamp { get; set; }
public string SignatureVersion { get; set; } = "";
public string Signature { get; set; } = "";
public string SigningCertURL { get; set; } = "";
public string UnsubscribeURL { get; set; } = "";
}
/// <summary>
/// TYPE 이 Notification 경우, Message 의 json 값 object 할당하기
/// </summary>
public class RootMessageJson
{
public string notificationType { get; set; } = "";
public Bounced bounce { get; set; }
public Mail mail { get; set; }
}
public class BouncedRecipient
{
public string emailAddress { get; set; } = "";
public string action { get; set; } = "";
public string status { get; set; } = "";
public string diagnosticCode { get; set; } = "";
}
public class Bounced
{
public string bounceType { get; set; } = "";
public string bounceSubType { get; set; } = "";
public List<BouncedRecipient> bouncedRecipients { get; set; }
public DateTime timestamp { get; set; }
public string feedbackId { get; set; } = "";
public string remoteMtaIp { get; set; } = "";
public string reportingMTA { get; set; } = "";
}
public class Header
{
public string name { get; set; } = "";
public string value { get; set; } = "";
}
public class CommonHeaders
{
public List<string> from { get; set; }
public List<string> to { get; set; }
public string subject { get; set; } = "";
}
public class Mail
{
public DateTime timestamp { get; set; }
public string source { get; set; } = "";
public string sourceArn { get; set; } = "";
public string sourceIp { get; set; } = "";
public string sendingAccountId { get; set; } = "";
public string messageId { get; set; } = "";
public List<string> destination { get; set; }
public bool headersTruncated { get; set; }
public List<Header> headers { get; set; }
public CommonHeaders commonHeaders { get; set; }
}
}