Beispiel #1
0
        public async Task Send(string subject, string body, IEnumerable <string> toAddresses = null, string fromAddress = null)
        {
            MailOptions options = MailOptionsRepository.Get();

            if (options.Equals(MailOptions.Empty))
            {
                return;
            }

            toAddresses = toAddresses ?? options.ToAddresses;
            fromAddress = fromAddress ?? options.FromAddress;

            SmtpClient smtpClient = new SmtpClient(options.SmtpHost);

            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials           = new NetworkCredential(options.SmtpUsername, options.SmtpPassword);
            smtpClient.Port = options.SmtpPort;

            MailMessage message = new MailMessage();

            message.Body       = body;
            message.From       = new MailAddress(options.FromAddress);
            message.IsBodyHtml = false;
            message.Subject    = subject;

            foreach (string toAddress in toAddresses)
            {
                message.To.Add(toAddress);
            }

            await Task.Run(() =>
            {
                smtpClient.Send(message);
            });
        }
        public void Set(MailOptions options)
        {
            using (LiteDatabase db = new LiteDatabase(Options.GetFullDatabaseFileName()))
            {
                LiteCollection <MailOptions> optionsCollection = db.GetCollection <MailOptions>();

                optionsCollection.Delete(x => true);
                optionsCollection.Insert(options);
            }
        }