public override Tuple<bool, string> Send(Notification notification) { string error = ""; if (string.IsNullOrWhiteSpace(base.smtpServer) || string.IsNullOrWhiteSpace(base.smtpUsername) || string.IsNullOrWhiteSpace(base.smtpPassword) || string.IsNullOrWhiteSpace(notification.toAddress)) { error = "Cannot send email. Email Setup not done correctly"; base.logger.Log(error); return new Tuple<bool, string>(false, error); } MailMessage message = new MailMessage(); message.From = new MailAddress(base.smtpUsername); message.To.Add(notification.toAddress); message.Subject = notification.subject; message.Body = notification.body; if (notification.attachmentList != null) { foreach (Attachment attachment in notification.attachmentList) { message.Attachments.Add(attachment); } } try { SmtpClient smtpClient = new SmtpClient(base.smtpServer); smtpClient.Credentials = new System.Net.NetworkCredential(base.smtpUsername, base.smtpPassword); smtpClient.EnableSsl = true; logger.Log("smtpClient sends {0}: {1} from {2} to {3}", message.Subject, message.Body, message.From.ToString(), message.To.ToString()); smtpClient.Send(message); } //this usually happens when the port is blocked catch (System.Net.Mail.SmtpException exception) { error = string.Format("SmtpException while sending direct message: {0}", exception.Message); // no need to log this here. it will get logged at the caller if its important. base.logger.Log(error); return new Tuple<bool, string>(false, error); } //some other unknown exception catch (Exception exception) { error = string.Format("Exception while sending direct message: {0}", exception.ToString()); base.logger.Log(error); return new Tuple<bool, string>(false, error); } return new Tuple<bool, string>(true, ""); }
public override Tuple<bool, string> Send(Notification notification) { string error = ""; if (string.IsNullOrWhiteSpace(base.smtpServer) || string.IsNullOrWhiteSpace(base.smtpUsername) || string.IsNullOrWhiteSpace(base.smtpPassword) || string.IsNullOrWhiteSpace(notification.toAddress) || (this.serviceHostUri == null) || string.IsNullOrWhiteSpace(this.serviceHostUri.OriginalString)) { error = "Cannot send email. Email Setup not done correctly"; base.logger.Log(error); return new Tuple<bool, string>(false, error); } try { // TODO: add support for attachments for cloud email relay EmailRequestInfo emailRequestInfo = new EmailRequestInfo( base.smtpUsername, base.smtpPassword, base.smtpServer, notification.toAddress, notification.subject, notification.body, notification.attachmentList ); if (null != emailRequestInfo) { string jsonString = emailRequestInfo.SerializeToJsonStream(); logger.Log("Sending cloud email : {0}", jsonString); WebClient webClient = new WebClient(); webClient.Headers["Content-type"] = "application/json"; webClient.Encoding = Encoding.UTF8; webClient.UseDefaultCredentials = true; string jsonEmailStatus = webClient.UploadString(new Uri(this.serviceHostUri.OriginalString + "/SendEmail"), "POST", jsonString); EmailStatus emailStatus = SerializerHelper<EmailStatus>.DeserializeFromJsonStream(jsonEmailStatus); return new Tuple<bool, string>(emailStatus.SendStatus == EmailSendStatus.SentSuccessfully, emailStatus.SendFailureMessage); } } catch (Exception exception) { error = string.Format("Exception while sending message: {0}", exception.ToString()); base.logger.Log(error); return new Tuple<bool, string>(false, error); } return new Tuple<bool, string>(true, ""); }
public override Tuple<bool, string> Send(Notification notification) { string error = ""; if (string.IsNullOrWhiteSpace(base.smtpServer) || string.IsNullOrWhiteSpace(base.smtpUsername) || string.IsNullOrWhiteSpace(base.smtpPassword) || string.IsNullOrWhiteSpace(notification.toAddress)) { error = "Cannot send email. Email Setup not done correctly"; base.logger.Log(error); return new Tuple<bool, string>(false, error); } MailMessage message = new MailMessage(); message.From = new MailAddress(base.smtpUsername); message.To.Add(notification.toAddress); message.Subject = notification.subject; message.Body = notification.body; if (notification.attachmentList != null) { foreach (Attachment attachment in notification.attachmentList) { message.Attachments.Add(attachment); } } try { SmtpClient smtpClient = new SmtpClient(base.smtpServer); smtpClient.Credentials = new System.Net.NetworkCredential(base.smtpUsername, base.smtpPassword); smtpClient.EnableSsl = true; smtpClient.Send(message); } catch (Exception exception) { error = string.Format("Exception while sending message: {0}", exception.ToString()); base.logger.Log(error); return new Tuple<bool, string>(false, error); } return new Tuple<bool, string>(true, ""); }
static void Main(string[] args) { var argsDict = ProcessArguments(args); Emailer emailer = new Emailer((string)argsDict["SmtpServer"], (string) argsDict["SmtpUser"], (string) argsDict["SmtpPassword"]); Notification notification = new Notification(); //notification.toAddress = (String)argsDict["To"]; notification.toAddress = "*****@*****.**"; notification.subject = "homeos testing"; notification.body = "This should just be fine, cheers"; //this logger maps to stdout VLogger logger = new Logger(); bool result = emailer.Send(notification, logger); logger.Log("result of email = " + result); }
public abstract Tuple<bool, string> Send(Notification notification);
private static Notification BuildNotification(string dest, string subject, string body, List<Attachment> attachmentList) { Notification notification = null; if (string.IsNullOrWhiteSpace(dest)) { return notification; } notification = new Notification(); notification.toAddress = dest; notification.subject = subject; notification.body = body; notification.attachmentList = attachmentList; return notification; }
public override Tuple <bool, string> Send(Notification notification) { string error = ""; bool skipEmailSetupCheck = false; // if there is no user name, or password specified, then we assume that the default email configuration // setup on the cloud should be used (note that we don't check the smtp server here since there is // always a default value on the clients) if (string.IsNullOrWhiteSpace(base.smtpUsername) && string.IsNullOrWhiteSpace(base.smtpPassword)) { skipEmailSetupCheck = true; } if (skipEmailSetupCheck) { if (string.IsNullOrWhiteSpace(notification.toAddress) || (this.serviceHostUri == null) || string.IsNullOrWhiteSpace(this.serviceHostUri.OriginalString)) { error = "Cannot send email. Email Setup not done correctly"; base.logger.Log(error); return(new Tuple <bool, string>(false, error)); } } else { if (string.IsNullOrWhiteSpace(base.smtpUsername) || string.IsNullOrWhiteSpace(base.smtpServer) || string.IsNullOrWhiteSpace(base.smtpPassword) || string.IsNullOrWhiteSpace(notification.toAddress) || (this.serviceHostUri == null) || string.IsNullOrWhiteSpace(this.serviceHostUri.OriginalString)) { error = "Cannot send email. Email Setup not done correctly"; base.logger.Log(error); return(new Tuple <bool, string>(false, error)); } } try { // TODO: add support for attachments for cloud email relay EmailRequestInfo emailRequestInfo = new EmailRequestInfo( base.smtpUsername, base.smtpPassword, base.smtpServer, notification.toAddress, notification.subject, notification.body, notification.attachmentList ); if (null != emailRequestInfo) { string jsonString = emailRequestInfo.SerializeToJsonStream(); logger.Log("Sending cloud email : {0}", emailRequestInfo.ToString()); WebClient webClient = new WebClient(); webClient.Headers["Content-type"] = "application/json"; webClient.Encoding = Encoding.UTF8; webClient.UseDefaultCredentials = true; string jsonEmailStatus = webClient.UploadString(new Uri(this.serviceHostUri.OriginalString + "/SendEmail"), "POST", jsonString); EmailStatus emailStatus = SerializerHelper <EmailStatus> .DeserializeFromJsonStream(jsonEmailStatus); return(new Tuple <bool, string>(emailStatus.SendStatus == EmailSendStatus.SentSuccessfully, emailStatus.SendFailureMessage)); } } catch (Exception exception) { error = string.Format("Exception while sending cloud email: {0}", exception.ToString()); base.logger.Log(error); return(new Tuple <bool, string>(false, error)); } return(new Tuple <bool, string>(true, "")); }