using System.Threading.Tasks; using MailKit.Net.Smtp; using MimeKit; using System.IO; using MailKit.Security; using System; using Yavsc.Models.Messaging; using Yavsc.Helpers; using System.Net; using System.Net.Http; using Microsoft.AspNet.Identity; using Yavsc.Models; using Yavsc.Models.Google.Messaging; using System.Collections.Generic; namespace Yavsc.Services { // This class is used by the application to send Email and SMS // when you turn on two-factor authentication in ASP.NET Identity. // For more details see this link http://go.microsoft.com/fwlink/?LinkID=532713 public class AuthMessageSender : IEmailSender, IGoogleCloudMessageSender { /// /// /// /// /// /// /// a MessageWithPayloadResponse, /// bool somethingsent = (response.failure == 0 && response.success > 0) /// public async Task NotifyAsync(GoogleAuthSettings googleSettings, IEnumerable registrationIds, YaEvent ev) { MessageWithPayloadResponse response; try { using (var web = new HttpClient()) { response = await web.NotifyEvent(googleSettings, registrationIds, ev); } } catch (WebException ex) { string errorMsgGCM; using (var respstream = ex.Response.GetResponseStream()) { using (StreamReader rdr = new StreamReader(respstream)) { errorMsgGCM = rdr.ReadToEnd(); rdr.Close(); } respstream.Close(); } if (errorMsgGCM == null) throw; throw new Exception(errorMsgGCM, ex); } return response; } public Task SendEmailAsync(SiteSettings siteSettings, SmtpSettings smtpSettings, string email, string subject, string message) { try { MimeMessage msg = new MimeMessage(); msg.From.Add(new MailboxAddress( siteSettings.Owner.Name, siteSettings.Owner.Address)); msg.To.Add(new MailboxAddress("", email)); msg.Body = new TextPart("plain") { Text = message }; msg.Subject = subject; using (SmtpClient sc = new SmtpClient()) { sc.Connect( smtpSettings.Host, smtpSettings.Port, SecureSocketOptions.None); sc.Send(msg); } } catch (Exception) { return Task.FromResult(false); } return Task.FromResult(true); } public Task ValidateAsync(string purpose, string token, UserManager manager, ApplicationUser user) { throw new NotImplementedException(); } /* SMS with Twilio: public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message) { var Twilio = new TwilioRestClient(twilioSettigns.AccountSID, twilioSettigns.Token); var result = Twilio.SendMessage( twilioSettigns.SMSAccountFrom, number, message); // Status is one of Queued, Sending, Sent, Failed or null if the number is not valid Trace.TraceInformation(result.Status); // Twilio doesn't currently have an async API, so return success. return Task.FromResult(result.Status != "Failed"); } */ } }