/*
         * //with sendgrid api
         * public static bool SendEmail(string subject, string message, string toemail, bool IsHTML = true)
         * {
         *
         *
         *  // Your gmail email address
         *  var FromEmail = ConfigurationManager.AppSettings["MailUser"];//"*****@*****.**";
         *  var apiKey = ConfigurationManager.AppSettings["SendGridKey"];
         *
         *
         *  try
         *  {
         *      var client = new SendGridClient(apiKey);
         *      var from = new EmailAddress(FromEmail, "SILPO - Balai Penelitian Tanah");
         *
         *      var to = new EmailAddress(toemail);
         *      var plainTextContent = message;
         *      var htmlContent = IsHTML ? message : $"<strong>{message}</strong>";
         *      var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
         *      var response = client.SendEmailAsync(msg).GetAwaiter().GetResult();
         *      return response.StatusCode == HttpStatusCode.Accepted;
         *  }
         *  catch (Exception ex)
         *  {
         *      //Console.WriteLine("failed to send email with the following error:");
         *      //Console.WriteLine(ep.Message);
         *      LogHelpers.source = typeof(EmailService).ToString();
         *      LogHelpers.message = "failed to send email with the following error:" + ex.Message;
         *      LogHelpers.user = CommonWeb.GetCurrentUser();
         *      LogHelpers.WriteLog();
         *      return false;
         *  }
         * }
         */
        /*
         * //with SMTP
         * public static bool SendEmail(string subject, string message, string toemail,bool IsHTML=true)
         * {
         *  SmtpMail oMail = new SmtpMail("TryIt");
         *  SmtpClient oSmtp = new SmtpClient();
         *
         *  // Your gmail email address
         *  oMail.From = ConfigurationManager.AppSettings["MailUser"];//"*****@*****.**";
         *
         *  // Set recipient email address
         *  oMail.To = toemail;
         *
         *  // Set email subject
         *  oMail.Subject = subject;
         *
         *  // Set email body
         *  if (IsHTML)
         *      oMail.HtmlBody = message;
         *  else
         *      oMail.TextBody = message;
         *
         *  // Gmail SMTP server address
         *  SmtpServer oServer = new SmtpServer(ConfigurationManager.AppSettings["MailServer"]);//"SMTP.Office365.com"
         *
         *  // If you want to use direct SSL 465 port,
         *  // please add this line, otherwise TLS will be used.
         *  // oServer.Port = 465;
         *
         *  // set 587 TLS port;
         *  oServer.Port = int.Parse(ConfigurationManager.AppSettings["MailPort"]); //587;
         *
         *  // detect SSL/TLS automatically
         *  oServer.ConnectType = SmtpConnectType.ConnectSTARTTLS;
         *
         *  // Gmail user authentication
         *  // For example: your email is "*****@*****.**", then the user should be the same
         *  oServer.User = ConfigurationManager.AppSettings["MailUser"];//"*****@*****.**";
         *  oServer.Password = ConfigurationManager.AppSettings["MailPassword"];//"Balittanah123";
         *
         *  try
         *  {
         *      Console.WriteLine("start to send email over SSL ...");
         *      oSmtp.SendMail(oServer, oMail);
         *      Console.WriteLine("email was sent successfully!");
         *      return true;
         *  }
         *  catch (Exception ex)
         *  {
         *      //Console.WriteLine("failed to send email with the following error:");
         *      //Console.WriteLine(ep.Message);
         *      LogHelpers.source = typeof(EmailService).ToString();
         *      LogHelpers.message = "failed to send email with the following error:"+ ex.Message;
         *      LogHelpers.user = CommonWeb.GetCurrentUser();
         *      LogHelpers.WriteLog();
         *      return false;
         *  }
         * }*/
        //using outlook without EAMail
        public static bool SendEmail(string subject, string message, string toemail, bool IsHTML = true)
        {
            string smtpServer = "smtp-mail.outlook.com";

            var UserMail     = ConfigurationManager.AppSettings["MailUser"];     //"*****@*****.**";
            var UserPassword = ConfigurationManager.AppSettings["MailPassword"]; //"Balittanah123";

            try
            {
                //Send teh High priority Email
                EmailManager mailMan = new EmailManager(smtpServer);

                EmailSendConfigure myConfig = new EmailSendConfigure();
                // replace with your email userName
                myConfig.ClientCredentialUserName = UserMail;
                // replace with your email account password
                myConfig.ClientCredentialPassword = UserPassword;
                myConfig.TOs             = new string[] { toemail };
                myConfig.CCs             = new string[] { };
                myConfig.From            = UserMail;
                myConfig.FromDisplayName = "SILPO - Balai Penelitian Tanah";
                myConfig.Priority        = System.Net.Mail.MailPriority.Normal;
                myConfig.Subject         = subject;

                EmailContent myContent = new EmailContent();
                myContent.Content = message;
                myContent.IsHtml  = IsHTML;
                mailMan.SendMail(myConfig, myContent);
                Console.WriteLine("email was sent successfully!");
                return(true);
            }
            catch (Exception ex)
            {
                //Console.WriteLine("failed to send email with the following error:");
                //Console.WriteLine(ep.Message);
                LogHelpers.source  = typeof(EmailService).ToString();
                LogHelpers.message = "failed to send email with the following error:" + ex.Message;
                LogHelpers.user    = CommonWeb.GetCurrentUser();
                LogHelpers.WriteLog();
                return(false);
            }
        }
        public static async Task <bool> BlobPostAsync(byte[] data, string filename)
        {
            var content = new MemoryStream(data, false);
            CloudStorageAccount storageAccount     = null;
            CloudBlobContainer  cloudBlobContainer = null;

            // Upload file info
            //string sourceFile = null;
            //string fileName = null;

            //string destinationFile = null;
            string storageConnectionString = AppConstants.BlobStorageConnection;
            string blobContainer           = AppConstants.DefaultBlobCountainer;

            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try
                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.
                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    // Create a container called 'balittanah' and append a GUID value to it to make the name unique.
                    cloudBlobContainer = cloudBlobClient.GetContainerReference(blobContainer);
                    if (cloudBlobContainer.Exists())
                    {
                        BlobContainerPermissions permissions = new BlobContainerPermissions
                        {
                            PublicAccess = BlobContainerPublicAccessType.Blob
                        };
                        await cloudBlobContainer.SetPermissionsAsync(permissions);

                        // Get a reference to the blob address, then upload the file to the blob.
                        // Use the value of filename for the blob name.
                        CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
                        await cloudBlockBlob.UploadFromStreamAsync(content);

                        return(true);
                    }
                    else
                    {
                        await cloudBlobContainer.CreateAsync();

                        // Set the permissions so the blobs are public.
                        BlobContainerPermissions permissions = new BlobContainerPermissions
                        {
                            PublicAccess = BlobContainerPublicAccessType.Blob
                        };
                        await cloudBlobContainer.SetPermissionsAsync(permissions);

                        // Get a reference to the blob address, then upload the file to the blob.
                        // Use the value of filename for the blob name.
                        CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
                        await cloudBlockBlob.UploadFromStreamAsync(content);
                    }

                    return(true);
                }
                catch (StorageException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                    LogHelpers.source  = MethodBase.GetCurrentMethod().DeclaringType.Name;
                    LogHelpers.message = ex.Message;
                    LogHelpers.user    = "******";
                    LogHelpers.WriteLog();
                }
                //finally
                //{
                //    // Clean up resources. This includes the container and the two temp files.
                //    Console.WriteLine("Deleting the container and any blobs it contains");
                //    if (cloudBlobContainer != null)
                //    {
                //        await cloudBlobContainer.DeleteIfExistsAsync();
                //    }
                //}
            }
            else
            {
                Console.WriteLine(
                    "A connection string has not been defined in the web.config appsetting. " +
                    "Add a environment variable named 'BlobStorageConnection' with your storage " +
                    "connection string as a value.");
            }

            return(false);
        }