user8724929
user8724929

Reputation:

datetime into my json mail subject c#

This is my DateTime function. it shows you the last month. I am able to put this into my mail body but I need to have it in my subject as well. Not sure how to do this. I thought it had something to do with parsing but I am not sure.

My C#:

{
    var currentDate = DateTime.Now;
    DateTime Uitgangstijd = BeginOfMonth(currentDate);
    DateTime Ingangstijd = Uitgangstijd.AddMonths(-1);

    var totDatum = Uitgangstijd;
    var retrieveDate = Ingangstijd;
    var dataItems = GetFromDatabase(retrieveDate, totDatum);
    var usageReport = new Core.Database.Report()
    {
        DatumVan = retrieveDate,
        DatumTot = totDatum,
        UsageItems = dataItems,
    };
    SetInDatabase(Report);
    Send(JobCancellationToken.Null, Report);

    DateTime t = BeginOfMonth(Ingangstijd);
}

private DateTime BeginOfMonth(DateTime t)
{
   return new DateTime(t.Year, t.Month, 1);
}

My JSON:

"Details": {
    "MailConfig": {
    "Addresses": {
      "From": "[email protected]",
      "To": [ "[email protected]" ],
      "CC": [],
      "BCC": []
    },
      "Subject": "Statistics_(DateTime function here)_2017"
    },
      "MailBaseUrl": "http://localhost/WebApp"
    }
}

Populating my Json:

   public class DetailSettings
  {
    public MailConfig MailConfig { get; set; }
    public string MailBaseUrl { get; set; }
  }
  }  
  public class MailConfig
  {
    public Addresses Addresses { get; set; }

    public string Subject { get; set; }
  }
  }
  public class Addresses
  {
    public string From { get; set; }
    public List<string> To { get; set; }
    public List<string> CC { get; set; }
    public List<string> BCC { get; set; }
      }
   }

Upvotes: 0

Views: 77

Answers (1)

Ali Ezzat Odeh
Ali Ezzat Odeh

Reputation: 2163

If your problem is to fill the subject, fill it from C# code, like the following :

Your classes :

public class DetailSettings
{
    public MailConfig MailConfig { get; set; }
    public string MailBaseUrl { get; set; }

    public DetailSettings()
    {
        MailConfig = new MailConfig();
    }
}

public class MailConfig
{
    public Addresses Addresses { get; set; }

    public string Subject { get; set; }
}

Then create instance and fill it then use serialization :

DetailSettings detailsSettings = new DetailSettings();
DateTime t = BeginOfMonth(DateTime.Now);
detailsSettings.MailConfig.Subject =string.Format("Statistics_{0}_{1}", t,t.Year);
string json = new JavaScriptSerializer().Serialize(detailsSettings);

Upvotes: 1

Related Questions