Are you looking for customized advices? Click here to contact the expert!
cghersi
cghersi
Reputation Top 1%
Cristiano Ghersi
Great passion for Software Engineering, mostly coding in Java and C#, I enjoy creating modular and reusable code for everybody!
254 Snippets  (3rd place)
Published
16 Channels
Created
73 Channels
Following
11 minutes ago
Last Visit
May 5, 2012
Registered
68896 points  (5th place)
Reputation
Top 1% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Master Publisher
Guru Publisher
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Master Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Master Trend Maker
Junior Influencer
Serious Influencer
Senior Influencer
Junior Commenter
Serious Commenter
Senior Commenter
Master Commenter
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Master Geek Aggregator
Junior Famous Coder
Serious Famous Coder
Senior Famous Coder
Master Famous Coder
Guru Famous Coder
Junior Wise Coder
Master Wise Coder

Recent Snippets See all snippets by cghersi

public by cghersi created Sep 7, 2018  182  1  3  0

Retrieve BLOB From Azure Blob Storage

Quick method to retrieve a set of BLOBs for Azure Blob Storage, given their names. Unfortunately the Azure REST APIs don't allow, yet, to BULK retrieve the content of several BLOBs in one single call, so we need to loop over each BLOB and retrieve the content one by one
public static Dictionary<Guid, byte[]> GetBlobs(List<string> names)
{
  Dictionary<string, byte[]> result = new Dictionary<string, byte[]>();

  // retrieve info about the interesting BLOBs:
  IEnumerable<CloudBlob> blobs = s_defaultContainer.ListBlobs().
    OfType<CloudBlob>().Where(c => names.Contains(c.Name));
  
  // loop over the retrieved BLOBs:
  foreach (CloudBlob b in blobs)
  {
  	b.FetchAttributes(); //otherwise we cannot query for Properties...
  	byte[] res = new byte[b.Properties.Length];
  	
  	// download the actual content:
  	b.DownloadToByteArray(res, 0); 
  	
  	result.Add(b.Name, res);
  }
  
  return result;
}
;

public by cghersi @ MailUp API DEV created May 8, 2015  1667  45  5  0

MailUp REST API - How to send a transactional email message

This snippet takes advantage of the MailUp .NET SDK to setup the secure communication towards the server. The credentials of the SMTP+ user are passed as parameter in the body of the request. Explore the MessageDTO object in order to understand how you can customize the message you want to send.
public static void SendMessage()
{
  // 1) Required values
  string username = "<your_username>";
	string password = "<your_password>";

	string clientid = "<your_clientid>";
	string clientsecret = "<your_clientsecret>";

	string smtpplus_username = "<your_smtpplus_username>";
	string smtpplus_password = "<your_smtpplus_password>";
  
  // 2) Setup the MailUp Client:
	MailUpClient client = new MailUpClient(clientid, clientsecret);
	client.RetrieveAccessToken( username, password );

  // 3) prepare the input:
  MessageDTO msg = new MessageDTO()
  {
    Html = new HtmlDTO()
    {
      Body = "<div>Hello Mr. [firstname][lastname] !!!</div><br><img width='600' height='397' src='cid:img001'>",
      BodyTag = "<body>"
    },
    Text = "Hello world!!!",
    Subject = "Hello friend!",
    From = new EmailAddressDTO() { Name = "Test User", Email = "test@mailup.it" },
    To = new List<EmailAddressDTO>() { new EmailAddressDTO() { Name = "Massimo", Email = "info@mailup.it" } },
    Cc = new List<EmailAddressDTO>(),
    Bcc = new List<EmailAddressDTO>(),
    ReplyTo = null,
    CharSet = "utf-8",
    ExtendedHeaders = null,
    Attachments = null,
    EmbeddedImages = new List<MessagePartDTO>() {
      new MessagePartDTO() {
        Filename = "Image.jpg",
        ContentId = "img001",
        Body = new byte[] { 0x01, 0x02, 0x03, 0x04 }
      }
    },
    XSmtpAPI = new XSmtpAPIDTO()
    {
      CampaignName = "Test Campaign",
      CampaignCode = "1001",
      Header = null,
      Footer = null,
      ClickTracking = null,
      ViewTracking = null,
      Priority = null,
      Schedule = null,
      DynamicFields = new List<NameValueDTO>() {
        new NameValueDTO() { N = "firstname", V = "Mario"},
        new NameValueDTO() { N = "lastname", V = "Rossi"}
      },
      CampaignReport = null,
      SkipDynamicFields = null
    },
    User = new SmtpUserDTO() { Username = smtpplus_username, Secret = smtpplus_password }
  };

  // 4) Invoke the web method:
  SendResponseDTO result = null;
  try
  {
    result = client.SendMessage(msg);
  }
  catch (Exception ex)
  {
    Console.WriteLine("Cannot perform the operation due to " + ex.Message);
    return;
  }

  // 5) Print results:
  if (result != null)
    Console.WriteLine("Result of sending task:" + result.Message);
  else
    Console.WriteLine("An error occurred while sending message.");
}
						
;

public by cghersi @ MailUp API DEV created May 8, 2015  2226  12  5  0

MailUp REST API - How to send a transactional email message that follows a template

This snippet takes advantage of the MailUp .NET SDK to setup the secure communication towards the server. The credentials of the SMTP+ user are passed as parameter in the body of the request. Explore the TemplateDTO object in order to understand how you can customize the message you want to send.
public static void SendTemplate()
{
  // 1) Required values
  string username = "<your_username>";
	string password = "<your_password>";

	string clientid = "<your_clientid>";
	string clientsecret = "<your_clientsecret>";

	string smtpplus_username = "<your_smtpplus_username>";
	string smtpplus_password = "<your_smtpplus_password>";
  
  // 2) Setup the MailUp Client:
	MailUpClient client = new MailUpClient(clientid, clientsecret);
	client.RetrieveAccessToken( username, password );

  // 3) prepare the input:
  TemplateDTO msg = new TemplateDTO()
  {
    TemplateId = 694,
    Subject = "Test message from template",
    From = new EmailAddressDTO() { Name = "Test User", Email = "test@mailup.it" },
    To = new List<EmailAddressDTO>() { new EmailAddressDTO() { Name = "Massimo", Email = "info@mailup.it" } },
    Cc = new List<EmailAddressDTO>(),
    Bcc = new List<EmailAddressDTO>(),
    ReplyTo = null,
    CharSet = "utf-8",
    ExtendedHeaders = null,
    Attachments = null,
    EmbeddedImages = new List<MessagePartDTO>() {
      new MessagePartDTO() {
        Filename = "Image.jpg",
        ContentId = "img001",
        Body = new byte[] { 0x01, 0x02, 0x03, 0x04 }
      }
    },
    XSmtpAPI = null,
    User = new SmtpUserDTO() { Username = smtpplus_username, Secret = smtpplus_password }
  };

  // 4) Invoke the web method:
  SendResponseDTO result = null;
  try
  {
    result = client.SendTemplate(msg);
  }
  catch (Exception ex)
  {
    Console.WriteLine("Cannot perform the operation due to " + ex.Message);
    return;
  }

  // 5) Print results:
  if (result != null)
    Console.WriteLine("Result of sending task:" + result.Message);
  else
    Console.WriteLine("An error occurred while sending template.");
}
												
;

public by cghersi @ MailUp API DEV created May 8, 2015  1744  8  5  0

MailUp REST API - How to retrieve information of SMTP+ Users

This snippet takes advantage of the MailUp .NET SDK to setup the secure communication towards the server. The credentials of the SMTP+ user are passed as parameter in the body of the request. You can optionally specify a username as third parameter in order to filter the resulting list.
public static void GetUserInfo()
{
  // 1) Required values
  string username = "<your_username>";
	string password = "<your_password>";

	string clientid = "<your_clientid>";
	string clientsecret = "<your_clientsecret>";

	string smtpplus_username = "<your_smtpplus_username>";
	string smtpplus_password = "<your_smtpplus_password>";
  
  // 2) Setup the MailUp Client:
	MailUpClient client = new MailUpClient(clientid, clientsecret);
	client.RetrieveAccessToken( username, password );

  // 3) Invoke the web method:
  List<UserInfoDTO> result = null;
  try
  {
    result = client.ListUserInfo(smtpplus_username, smtpplus_password);
  }
  catch (Exception ex)
  {
    Console.WriteLine("Cannot perform the operation due to " + ex.Message);
    return;
  }

  // 4) Print results:
  if (result != null)
    Console.WriteLine("Retrieved users total:" + result.Count);
  else
    Console.WriteLine("An error occurred while retrieving users.");
}
																		
;

public by cghersi created Jan 10, 2017  2303  10  5  3

How to write bytes into file in Java

Efficient way to write strings or bytes onto a file.
  import java.nio.file.Files;
  import java.nio.file.Paths;

  public static void writeFile(String path, String content) throws Exception {
		if (isNullOrEmpty(path) || isNullOrEmpty(content)) {
			return;
		}
		Files.write(Paths.get(path), content.getBytes());
	}                     
;