private string EnviaEmail(string emailRem, string senhaRem, string emailDest, string assunto, string corpo) { try { var service = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2013_SP1); service.Credentials = new Microsoft.Exchange.WebServices.Data.WebCredentials(emailRem, senhaRem); service.AutodiscoverUrl(emailRem); var email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service); email.Subject = assunto; email.Body = corpo; email.ToRecipients.Add(emailDest); email.SendAndSaveCopy(); return("Justificativa enviada com sucesso para aprovação !"); } catch (Exception) { return("Ocorreu algum erro ao tentar enviar o email !"); } }
private string EnviaEmail(string emailRem, string senhaRem, string emailDest, string assunto, StringBuilder corpo) { try { var service = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2013_SP1); service.Credentials = new Microsoft.Exchange.WebServices.Data.WebCredentials(emailRem, senhaRem); service.AutodiscoverUrl(emailRem); var email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service); email.Subject = assunto; email.Body = corpo.ToString(); email.ToRecipients.Add(emailDest); email.SendAndSaveCopy(); return("E-mail enviado com sucesso !"); } catch (Exception ex) { return("Ocorreu algum erro, " + ex); } }
public async Task SendEmailWithVerificationCode(Object json) { string name; string username; SprHuman[] sprHuman; dynamic jsonDynamic = JObject.Parse(json.ToString()); //if (!CheckOutGoogleRecaptchaToken(jsonDynamic.captcha.ToString())) //{ // await Response.WriteAsync("Is invalid recaptcha token !"); // return; //} AutodiscoverRedirectionUrlValidationCallback autodiscoverRedirectionUrlValidationCallback = (string redirectionUrl) => { Uri redirectionUri = new Uri(redirectionUrl); if (redirectionUri.Scheme == "https") { return(true); } else { return(false); } }; Microsoft.Exchange.WebServices.Data.ExchangeService exchangeService = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2013_SP1); exchangeService.Credentials = new Microsoft.Exchange.WebServices.Data.WebCredentials(AuthorizationConfig.login, AuthorizationConfig.password); try { string email = jsonDynamic.email; DateTime birthday = DateTime.Parse(jsonDynamic.birthdate.ToString()); sprHuman = GetInformationFromDatabase(email); if (birthday != sprHuman[0].Birthday) { await Response.WriteAsync($"Email is doesn't send !"); return; } name = $"{sprHuman[0].FirstName} {sprHuman[0].LastName}"; username = sprHuman[0].SamaccountName; Random random = new Random(); string verificationCode = AuthorizationConfig.verificationCode; for (int i = 0; i <= 10; i++) { char characterLetter = (char)random.Next('A', 'Z'); char characterDigit = (char)random.Next('1', '9'); char[] characters = { characterLetter, '~', '!', characterDigit, '@', '#', characterLetter, '$', '&', characterDigit, '*', '(', characterLetter, ')', '_', characterDigit, '+', '?', characterLetter, ':', '-', characterDigit, '=', ',', characterLetter, '.', ';', characterDigit, '[', ']', characterLetter }; verificationCode += $"{characters[random.Next(characters.Length)]}"; } verificationCodes.Add(verificationCode, username); exchangeService.Timeout = 300000; exchangeService.AutodiscoverUrl(AuthorizationConfig.login, autodiscoverRedirectionUrlValidationCallback); ThreadStart threadStart = () => { Action action = () => { Thread.Sleep(300000); verificationCodes.Remove(verificationCodes[verificationCode]); }; }; Thread thread = new Thread(threadStart); thread.Start(); Microsoft.Exchange.WebServices.Data.EmailMessage emailMessage = new Microsoft.Exchange.WebServices.Data.EmailMessage(exchangeService); emailMessage.ToRecipients.Add(email); emailMessage.Subject = "Hello World"; emailMessage.Body = new Microsoft.Exchange.WebServices.Data.MessageBody($"Hello {name} ! This is the verification code {verificationCode} which you must to introduce in following link http://elk-test.iep.ru/password-reset/{GenerateToken(username, AuthorizationConfig.lifeTimeForCheckOutEmailOrChangePasswordForUnauthUser)}"); await emailMessage.SendAndSaveCopy(); await Response.WriteAsync("Email is sent !"); } catch (Exception ex) { await Response.WriteAsync($"Email is doesn't send ! {ex.Message}"); } }