Beispiel #1
0
        public void SendEmail(Mail email)
        {
            var service      = CreateService();
            var gmailMessage = CreateMessage(email);

            UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMessage, email.Properties.From);
            request.Execute();
        }
Beispiel #2
0
        public void SendMail(string To, string Subject, string Body)
        {
            UserCredential credential;
            var            PathToClientSecret = System.Web.HttpContext.Current.Request.MapPath("~\\Content\\client_secret.json");

            //using (var stream =
            //    new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            //{
            using (var stream =
                       new FileStream(PathToClientSecret, FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/client_secret.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            var msg = new AE.Net.Mail.MailMessage
            {
                Subject = "=?UTF-8?B?" + Base64UrlEncode(Subject) + "?=",

                Body        = Body,
                From        = new MailAddress("*****@*****.**"),
                ContentType = "text/html"
            };

            msg.To.Add(new MailAddress(To));
            msg.ReplyTo.Add(msg.From); // Bounces without this!!
            var msgStr = new StringWriter();

            msg.Save(msgStr);



            UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(new Message
            {
                Raw = Base64UrlEncode(msgStr.ToString())
            }, "me");

            var resultado = request.Execute();
        }
Beispiel #3
0
        public async Task <IActionResult> Send([FromForm] SendEmailRequest request, [FromServices] IGoogleAuthProvider auth)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index", new List <Destination>(_emailsService.Get())));
            }

            var credentials = await auth.GetCredentialAsync().ConfigureAwait(false);

            var gmailService = new GmailService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credentials,
                ApplicationName       = "EmailSender"
            });

            var destinations = _emailsService.Get();
            int sections     = (destinations.Count / 5) + 1;
            var tasks        = new List <Task>(5);

            for (int i = 0; i < sections; i++)
            {
                var currFive = destinations.Skip(i * 5).Take(5);
                tasks.Clear();

                foreach (var destination in currFive)
                {
                    string body    = request.Template.Replace("{0}", destination.Name);
                    var    message = new Message
                    {
                        Raw = EmailMessageCreator.Message(destination.Name, destination.Email, request.Subject, body)
                    };

                    var compose = new UsersResource.MessagesResource.SendRequest(gmailService, message, "me");
                    tasks.Add(compose.ExecuteAsync());
                }

                await Task.WhenAny(tasks).ConfigureAwait(false);

                await Task.Delay(1000).ConfigureAwait(false);
            }

            _emailsService.RemoveAll();

            return(Redirect("/"));
        }
Beispiel #4
0
        public static void SendGmail(string to, string subject, string body)
        {
            AuthorizeGmail();

            AE.Net.Mail.MailMessage message = new AE.Net.Mail.MailMessage();
            message.Subject = subject;
            message.Body    = body;
            message.To.Add(new MailAddress(to));
            message.From = new MailAddress(GetUserEmailAddress());

            StringWriter msgStr = new StringWriter();

            message.Save(msgStr);

            UsersResource.MessagesResource             messageResource = gmailService.Users.Messages;
            UsersResource.MessagesResource.SendRequest sendRequest     = messageResource.Send(new Google.Apis.Gmail.v1.Data.Message {
                Raw = Base64UrlEncode(msgStr.ToString())
            }, "me");
            sendRequest.Execute();
        }
Beispiel #5
0
        public ActionResult SendEmail(string subject, string emailTo, string body)
        {
            string       UserMail = User.FindFirst(c => c.Type == ClaimTypes.Email).Value;
            GmailService service  = GetService();

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(UserMail);
            msg.To.Add(emailTo);
            msg.ReplyToList.Add(UserMail);
            msg.Subject    = subject;
            msg.Body       = body;
            msg.IsBodyHtml = true;
            MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(msg);
            Message             gmailMsg    = new Message
            {
                Raw = Encode(mimeMessage.ToString())
            };

            UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(gmailMsg, UserMail);
            request.Execute();
            return(View("~/Views/Home/Index.cshtml"));
        }
Beispiel #6
0
        public void SendMail()
        {
            UserCredential credential;

            using (var stream =
                       new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/client_secret.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            var msg = new AE.Net.Mail.MailMessage
            {
                Subject = "Test GMail API Send Mail",
                Body    = "Hello, World, from Gmail API!",
                From    = new MailAddress("*****@*****.**")
            };

            msg.To.Add(new MailAddress("*****@*****.**"));
            msg.ReplyTo.Add(msg.From); // Bounces without this!!
            var msgStr = new StringWriter();

            msg.Save(msgStr);


            // Define parameters of request.
            //UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");

            UsersResource.MessagesResource.SendRequest request = service.Users.Messages.Send(new Message
            {
                Raw = Base64UrlEncode(msgStr.ToString())
            }, "me");

            // List labels.
            //IList<Label> labels = request.Execute().Labels;

            //Console.WriteLine("Labels:");
            //if (labels != null && labels.Count > 0)
            //{
            //    foreach (var labelItem in labels)
            //    {
            //        Console.WriteLine("{0}", labelItem.Name);
            //    }
            //}
            //else
            //{
            //    Console.WriteLine("No labels found.");
            //}
            var resultado = request.Execute();

            Console.WriteLine("Mensaje enviado " + resultado.Id);
            Console.ReadKey();
        }
Beispiel #7
0
        public static void EmailParameters(EmailMessage email)
        {
            UserCredential credential;

            using (var stream =
                       new FileStream("../Micklek.API/Properties/credentials.json", FileMode.Open, FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens, and is created
                // automatically when the authorization flow completes for the first time.
                string credPath = "token.json";
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // Create Gmail API service.
            var service = new GmailService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            // Define parameters of request.
            // UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");
            // request.Execute();
            // Console.WriteLine(request);

            var mailMessage = new System.Net.Mail.MailMessage();

            mailMessage.From = new System.Net.Mail.MailAddress(email.FromAddress);
            mailMessage.To.Add(email.ToRecipients);
            mailMessage.ReplyToList.Add(email.FromAddress);
            mailMessage.Subject    = email.Subject;
            mailMessage.Body       = email.Body;
            mailMessage.IsBodyHtml = email.IsHtml;

            // foreach (System.Net.Mail.Attachment attachment in email.Attachments)
            // {
            //     mailMessage.Attachments.Add(attachment);
            // }

            var mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(mailMessage);


            var gmailMessage = new Google.Apis.Gmail.v1.Data.Message
            {
                Raw = Encode(mimeMessage.ToString())
            };

            UsersResource.MessagesResource.SendRequest request2 = service.Users.Messages.Send(gmailMessage, "*****@*****.**");

            try
            {
                request2.Execute();
            }
            catch {}
        }