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) { 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 Product deserialized = JsonSerializer.Deserialize <Product>(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(deserialized.OwnerFK); //mm.From = new MailAddress("*****@*****.**"); //mm.Subject = "New Product In Inventory"; //mm.Body = $"Name:{deserialized.Name}; Price {deserialized.Price};"; //SmtpClient mailserver = new SmtpClient("smtp.gmail.com", 587); //mailserver.Credentials = //mailserver.Send(mm); //Send Email with deserialized. Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8 MailMessage message = new MailMessage(); SmtpClient smtp = new SmtpClient(); message.From = new MailAddress("*****@*****.**"); message.To.Add(new MailAddress(deserialized.OwnerFK)); message.Subject = "New Product In Inventory"; message.IsBodyHtml = true; //to make message body as html var cipher = KeyRepository.Encrypt($"Name:{deserialized.Name}; File {deserialized.Name}; Link {deserialized.File}; "); string realvalue = KeyRepository.Decrypt(cipher); message.Body = realvalue; //$"Name:{deserialized.Name}; File {deserialized.Name}; Link {deserialized.File}; "; smtp.Port = 587; smtp.Host = "smtp.gmail.com"; //for gmail host smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("*****@*****.**", "PfcAssignment"); //go on google while you are logged in in this account > search for lesssecureapps > turn it to on smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(message); 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()); } }
/// <summary> /// Publish method: uploads a message to the queue /// </summary> /// <param name="p"></param> public void AddToEmailQueue(FileUpload fu) { PublisherServiceApiClient client = PublisherServiceApiClient.Create(); var t = CreateGetTopic(); string serialized = JsonSerializer.Serialize(fu, typeof(FileUpload)); string encryptedFile = KeyRepository.Encrypt(serialized); 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 }
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); } } }
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) { 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 string decryptedFile = KeyRepository.Decrypt(toDeserialize); FileUpload deserialized = JsonSerializer.Deserialize <FileUpload>(toDeserialize); //Deserializing since when we published it we serialized it MailMessage mm = new MailMessage(); SmtpClient smtp = new SmtpClient(); mm.To.Add(deserialized.userEmail); mm.From = new MailAddress("*****@*****.**"); mm.Subject = "A new file has been shared with you"; mm.IsBodyHtml = true; mm.Body = "New Message From"; smtp.Port = 587; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("*****@*****.**", "noreply1234"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(mm); List <string> acksIds = new List <string>(); acksIds.Add(pullResponse.ReceivedMessages[0].AckId); client.Acknowledge(s.SubscriptionName, acksIds.AsEnumerable()); //SmtpClient mailserver = new SmtpClient("smtp.gmail.com", 587); //mailserver.Credentials = //mailserver.Send(mm); //Send Email with deserialized. Documentation: https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8 /* * * MailMessage message = new MailMessage(); * SmtpClient smtp = new SmtpClient(); * message.From = new MailAddress("FromMailAddress"); * message.To.Add(new MailAddress("ToMailAddress")); * message.Subject = "Test"; * message.IsBodyHtml = true; //to make message body as html * message.Body = htmlString; * smtp.Port = 587; * smtp.Host = "smtp.gmail.com"; //for gmail host * smtp.EnableSsl = true; * smtp.UseDefaultCredentials = false; * smtp.Credentials = new NetworkCredential("*****@*****.**", "password"); * * //go on google while you are logged in in this account > search for lesssecureapps > turn it to on * * smtp.DeliveryMethod = SmtpDeliveryMethod.Network; * smtp.Send(message); * * * * * 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());*/ } }