public static void NotifyTrackingStatusChanged(SubscriberMailSettings settings, string trackingNumber, string status, string comment) { var fromAddress = new MailAddress(settings.SourceEmailAddress, settings.SourceFullName); var toAddress = new MailAddress(settings.DestinationEmailAddress, settings.DestinationFullName); var smtp = new SmtpClient { Host = settings.SmtpUrl, Port = settings.SmtpPort, EnableSsl = settings.UseSsl, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, settings.SmtpPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = string.Format("Tracking status was changed - {0}", trackingNumber), Body = string.Format( "Tracking with a number {0} was chaned its status. \nThe new status is: {1}\nComment: {2}", trackingNumber, status, comment) }) { try { smtp.Send(message); } catch (Exception) { throw; } } }
public static void NotifyNewTracking(SubscriberMailSettings settings, string trackingNumber, string status, string comment, string trackingItemName, string qrUrl, string password) { var fromAddress = new MailAddress(settings.SourceEmailAddress, settings.SourceFullName); var toAddress = new MailAddress(settings.DestinationEmailAddress, settings.DestinationFullName); var smtp = new SmtpClient { Host = settings.SmtpUrl, Port = settings.SmtpPort, EnableSsl = settings.UseSsl, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, settings.SmtpPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = string.Format("New tracking has been created - {0}", trackingNumber), Body = ResourceTemplates.NewTrackingNotificationTemplate, BodyEncoding = Encoding.Unicode, IsBodyHtml = true }) { message.Body = message.Body.Replace("#ServiceProvider#", settings.SourceFullName); message.Body = message.Body.Replace("#TrackingItemName#", trackingItemName); message.Body = message.Body.Replace("#Status#", status); message.Body = message.Body.Replace("#Comment#", comment); message.Body = message.Body.Replace("#TrackingNumber#", trackingNumber); message.Body = message.Body.Replace("#Comment#", comment); message.Body = string.IsNullOrWhiteSpace(password) ? message.Body.Replace("#Password#", "Not applicable") : message.Body.Replace("#Password#", password); message.Body = message.Body.Replace("#QrCodeUrl#", qrUrl); byte[] bytes = Encoding.Default.GetBytes(message.Body); message.Body = Encoding.Unicode.GetString(bytes); try { smtp.Send(message); } catch (Exception) { throw; } } }
public ActionResult Create(ObjectId trackingId, Tracking tracking) { try { if (ModelState.IsValid) { var trackingItem = _trackableItemsCollection.AsQueryable().FirstOrDefault(ti => ti.Id == tracking.TrackingItemId); //delete customer info if tracking item does not support it if (!trackingItem.SupportsUserInformation) tracking.CustomerInformation = null; tracking.CreatedDate = DateTime.Now.Date; tracking.Id = trackingId; tracking.User = User.Identity.Name; tracking.History = new List<TrackingHistoryRecord>(){new TrackingHistoryRecord(){Comment = tracking.Comment, CreatedDate = DateTime.Now, StateId = tracking.StateId}} ; tracking.Password = trackingItem.IsSecured ? PasswordGenerator.CreateRandomPassword(8) : string.Empty; _trackingCollection.Save(tracking); } try { var username = Membership.GetUser().UserName.ToLower(); var user = _usersCollection.AsQueryable().FirstOrDefault(x => string.Equals(x.NameLowerSpace, username)); if (user.EnableSubscribersEmailNotifications) { if (tracking.CustomerInformation != null && !string.IsNullOrWhiteSpace(tracking.CustomerInformation.Email)) { var trackingItem = _trackableItemsCollection.AsQueryable().FirstOrDefault(x => x.UserId == User.Identity.Name && x.Id == tracking.TrackingItemId); var stateName = trackingItem.States.FirstOrDefault(s => s.Id == tracking.StateId).Name; SubscriberMailSettings emailSettings = new SubscriberMailSettings(); emailSettings.SourceEmailAddress = user.SmtpUserName; emailSettings.SmtpPassword = user.SmtpPassword; emailSettings.SmtpPort = user.SmtpPort; emailSettings.SmtpUrl = user.SmtpServerUrl; emailSettings.UseSsl = user.SmtpHttps; emailSettings.DestinationEmailAddress = tracking.CustomerInformation.Email; emailSettings.SourceFullName = string.Format("{0} {1}", user.FirstName, user.LastName); emailSettings.DestinationFullName = string.Format("{0} {1}", tracking.CustomerInformation.FirstName, tracking.CustomerInformation.LastName); var trackingData = QRCodeHtmlHelper.CreateQrData(tracking.TrackingNumber, tracking.Password); var qrUrl = QRCodeHtmlHelper.QRCode(null, trackingData, 150); Mailer.NotifyNewTracking(emailSettings, tracking.TrackingNumber, stateName, tracking.Comment, trackingItem.Name, qrUrl.ToString(), tracking.Password); } } } catch (Exception ex) { TempData["errorsOccured"] = string.Format("Unable to send a notification email: {0}", ex.Message); } return RedirectToAction("PrintableVersion", "Tracking", new { id = trackingId }); } catch { return View(); } }