Skip to content

Instantly share code, notes, and snippets.

@henrikj242
Last active November 29, 2018 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikj242/8dce589a9cf33481b9a5835c0364fc2f to your computer and use it in GitHub Desktop.
Save henrikj242/8dce589a9cf33481b9a5835c0364fc2f to your computer and use it in GitHub Desktop.
PeytzMail Api Examples
using System;
using System.IO;
using System.Net;
namespace pmAttachment
{
class Program
{
static void Main(string[] args)
{
String fileToUpload = "TestFile.txt";
WebClient webClient = new WebClient();
CredentialCache credCache = new CredentialCache();
String url = "https://client-name.peytzmail.com/api/v1/attachments.json";
credCache.Add(
new Uri(url),
"Basic",
new NetworkCredential(
"secret-api-key",
""
)
);
webClient.Credentials = credCache;
byte[] responseArray = webClient.UploadFile(url,fileToUpload);
// Decode and display the response.
Console.WriteLine("\nResponse Received: \n{0}",
System.Text.Encoding.ASCII.GetString(responseArray)
);
}
}
}
using System;
using System.IO;
using System.Net;
namespace pmSubscribe
{
class Program
{
static void Main(string[] args)
{
// POST to: https://<client>.peytzmail.com/api/v1/mailinglists/subscribe.json
String dataBody = @"{""subscribe"": { ""mailinglist_ids"": [""list-id""], ""subscriber"": {""email"": ""john.doe@example.com""}}}";
WebClient webClient = new WebClient();
CredentialCache credCache = new CredentialCache();
String url = "https://test.peytzmail.com/api/v1/mailinglists/subscribe.json";
credCache.Add(
new Uri(url),
"Basic",
new NetworkCredential(
"API_TOKEN",
""
)
);
webClient.Credentials = credCache;
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
string response = webClient.UploadString(url, dataBody);
// Decode and display the response.
Console.WriteLine(
"\nResponse Received: \n{0}",
response
);
}
}
}
using System;
using System.IO;
using System.Net;
using System.IO.Compression;
namespace pmAttachment
{
class Program
{
static void Main(string[] args)
{
String base_url = @"https://test.peytzmail.com";
String api_token = "API_TOKEN";
String file_to_compress_path = @"./";
String file_to_compress_name = @"some_file.csv";
String zip_file_name = @"subscribers.zip";
String subscriber_feed_id = @"medlemsdata";
// Prepare zip-file
using (FileStream zip_fs = new FileStream(zip_file_name, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zip_fs, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(
file_to_compress_path + file_to_compress_name,
file_to_compress_name
);
}
}
WebClient webClient = new WebClient();
CredentialCache credCache = new CredentialCache();
String url = base_url + "/api/v1/subscriber_feeds/" + subscriber_feed_id + ".json";
credCache.Add(
new Uri(url),
"Basic",
new NetworkCredential(
api_token,
""
)
);
webClient.Credentials = credCache;
byte[] responseArray = webClient.UploadFile(url, zip_file_name);
// Decode and display the response.
Console.WriteLine("\nResponse Received: \n{0}",
System.Text.Encoding.ASCII.GetString(responseArray)
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment