Reputation: 317
I've been sending emails using ASP.NET Smtp method and it's been working fine so far.
But now the structure of the business I'm working on has changed and they're requesting for new changes. First of all, now my email structure should be send to an API Email Service instead of an specific email address.
I've been searching for a while and I think I need to use HTTPRequest to do it, but I'm not exactly gifted when using C#. Here's my actual code (SMTP):
protected void Send_Button(object sender, EventArgs e)
{
// Mailing Initialization
System.Net.Mail.MailMessage mmsg = new System.Net.Mail.MailMessage();
mmsg.To.Add("[email protected]");
mmsg.Body = TextBox1.Text;
mmsg.BodyEncoding = System.Text.Encoding.UTF8;
mmsg.IsBodyHtml = true;
mmsg.From = new System.Net.Mail.MailAddress("[email protected]");
System.Net.Mail.SmtpClient cliente = new System.Net.Mail.SmtpClient();
cliente.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
cliente.Port = 587;
cliente.EnableSsl = true;
cliente.Host = "smtp.gmail.com";
try
{
cliente.Send(mmsg);
}
catch (Exception)
{
string message = "There is an error";
ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language=JavaScript>if(window.confirm('" + (message) + "'))</script>");
}
}
This needs to change, of course. I've been provided with an URL which is my API direction: https://apitp.trial/api/Online/Mail
The protocol must be HTTPS and the method must be POST.
I tell you, I've been searching for ages with no luck. Any ideas on how the structure of my mail should change to achieve this objective?
Thanks in advance.
Upvotes: 1
Views: 19029
Reputation: 369
So it looks as thought your solution is shifting from using SMTP protocol to post messages to your mail server to using an API that ingests JSON.
From your comments I can see that it accepts a JSON format like this:
{
"Email": "[email protected]",
"CustomerNumber": 123456789,
"ExternalMessageID": "1-222",
"CustomerName": "John Smith",
"CampaingName": "A Campaign name",
"Extrafields": "opcional1;opcional2"
}
I'm assuming The "Extrafields" node is a string and not an array as it appears to have that kind of format.
To work with Json in C# you'll need to import JSON.NET into your solution if you haven't already. We can then use your existing event handler on your code above and use it to create the following object:
public class MailMessage
{
public string Email { get; set; }
public int CustomerNumber { get; set; }
public string ExternalMessageID { get; set; }
public string CustomerName { get; set; }
public string CampaingName { get; set; }
public string Extrafields { get; set; }
}
You can then use JSON.NET to serialize this object into a JSON string and POST that to your API in the format it is requiring. But being as your company also requires authentication, you will need to add a header to your POST request. I would also recommend using RestSharp for this to make your life easier.
You can use something like the following to get all of this up and running:
protected void Send_Button(object sender, EventArgs e)
{
//Creates new MailMessage Object
MailMessage mm = new MailMessage{
Email = "[email protected]",
CustomerNumber = 123456789,
ExternalMessageID = "1-222",
CustomerName = "John Smith",
CampaingName = "A Campaign name",
Extrafields = "opcional1;opcional2"
};
var client = new RestClient("https://apitp.trial/"); //Makes a RestClient that will send your JSON to the server
var request = new RestRequest("Online/Mail", Method.POST); //Sets up a Post Request your your desired API address
request.AddHeader("api........?authorization", "INSERT YOUR BASE64 STRING HERE")
request.AddHeader("Content-type", "application/json"); //Declares this request is JSOn formatted
request.AddJsonBody(mm); //Serializes your MailMessage into a JSON string body in the request.
var response = client.Execute(request); //Executes the Post Request and fills a response variable
}
Just insert your Base64 encrypted credentials where I have indicated and you can include some logic on the response variable to check it has processed properly. The Response variable has StatusCode
on it, a code 200 will indicate a success.
Upvotes: 2