Exemplo n.º 1
0
        public ActionResult Send(FormCollection collection)
        {
            try
            {
                string           fileid = collection.GetValue("FieldId").AttemptedValue;
                int              fileId = Convert.ToInt32(fileid);
                PubSubRepository psr    = new PubSubRepository();
                FilesRepository  fr     = new FilesRepository();

                Models.File f   = fr.GetFile(fileId);
                FileSendTo  fst = new FileSendTo();

                string email   = collection.GetValue("Email").AttemptedValue;
                string message = collection.GetValue("Message").AttemptedValue;

                fst.Link    = f.Link;
                fst.Name    = f.Name;
                fst.OwnerFk = f.OwnerFk;
                fst.Message = message;
                fst.Email   = email;

                psr.AddToEmailQueue(fst);

                psr.DownloadEmailFromQueueAndSend();

                new LoggingRepository().Logging("File: " + f.Name + " Shared With: " + email + " At: " + DateTime.Now);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                new LoggingRepository().ErrorLogging(ex);
            }
            return(View());
        }
Exemplo n.º 2
0
        public void SendMail(FileSendTo fst)
        {
            try
            {
                SmtpClient        client   = new SmtpClient();
                string            email    = "*****@*****.**";
                string            password = "******";
                NetworkCredential nc       = new NetworkCredential(email, password);
                MailMessage       mm       = new MailMessage();

                mm.Subject = fst.Message;
                mm.From    = new MailAddress(fst.OwnerFk);
                mm.Body    = fst.Link;
                mm.To.Add(new MailAddress(fst.Email));

                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.EnableSsl             = true;
                client.Credentials           = nc;
                client.Send(mm);
            }
            catch (Exception ex)
            {
                new LoggingRepository().ErrorLogging(ex);
            }
        }
Exemplo n.º 3
0
        public void DownloadEmailFromQueueAndSend()
        {
            SubscriberServiceApiClient client = SubscriberServiceApiClient.Create();

            var s            = CreateGetSubscription();                  //This must be called before being able to read messages from Topic/Queue
            var pullResponse = client.Pull(s.SubscriptionName, true, 1); //Reading the message on top; You can read more than just 1 at a time

            if (pullResponse != null)
            {
                try
                {
                    string     toDeserialize = pullResponse.ReceivedMessages[0].Message.Data.ToStringUtf8(); //extracting the first message since in the previous line it was specified to read one at a time. if you decide to read more then a loop is needed
                    FileSendTo deserialized  = JsonSerializer.Deserialize <FileSendTo>(toDeserialize);       //Deserializing since when we published it we serialized it

                    //MailMessage mm = new MailMessage();  //Message on queue/topic will consist of a ready made email with the desired content, you can upload anything which is serializable
                    //mm.To.Add("*****@*****.**");
                    //mm.From = new MailAddress("*****@*****.**");
                    //mm.Subject = "New Product In Inventory";
                    //mm.Body = $"Name:{deserialized.Name}; Price {deserialized.Price};";

                    if (deserialized.Name != null)
                    {
                        deserialized.Link    = KeyRepository.Decrypt(deserialized.Link);
                        deserialized.OwnerFk = KeyRepository.Decrypt(deserialized.OwnerFk);
                        deserialized.Message = KeyRepository.Decrypt(deserialized.Message);
                        deserialized.Name    = KeyRepository.Decrypt(deserialized.Name);
                        deserialized.Email   = KeyRepository.Decrypt(deserialized.Email);
                        SendMail(deserialized);
                    }

                    //Send Email with deserialized. Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8

                    List <string> acksIds = new List <string>();
                    acksIds.Add(pullResponse.ReceivedMessages[0].AckId); //after the email is sent successfully you acknolwedge the message so it is confirmed that it was processed

                    client.Acknowledge(s.SubscriptionName, acksIds.AsEnumerable());
                }
                catch (Exception ex)
                {
                    new LoggingRepository().ErrorLogging(ex);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Publish method: uploads a message to the queue
        /// </summary>
        /// <param name="p"></param>
        public void AddToEmailQueue(FileSendTo p)
        {
            PublisherServiceApiClient client = PublisherServiceApiClient.Create();
            var t = CreateGetTopic();

            p.Link    = KeyRepository.Encrypt(p.Link);
            p.OwnerFk = KeyRepository.Encrypt(p.OwnerFk);
            p.Message = KeyRepository.Encrypt(p.Message);
            p.Name    = KeyRepository.Encrypt(p.Name);
            p.Email   = KeyRepository.Encrypt(p.Email);

            string serialized = JsonSerializer.Serialize(p, typeof(FileSendTo));


            List <PubsubMessage> messagesToAddToQueue = new List <PubsubMessage>(); // the method takes a list, so you can upload more than 1 message/item/product at a time
            PubsubMessage        msg = new PubsubMessage();

            msg.Data = ByteString.CopyFromUtf8(serialized);

            messagesToAddToQueue.Add(msg);

            client.Publish(t.TopicName, messagesToAddToQueue); //committing to queue
        }