예제 #1
0
        public EmailSendStatus SendMail(string userEmail, string emailSubject, string emailBody)
        {
            EmailSendStatus emailSendStatus = new EmailSendStatus();

            try
            {
                using (MailMessage msg = new MailMessage())
                    using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(APIConf.WowCharacterComparerEmail, APIConf.WoWCharacterComparerEmailPassword),
                        Timeout = 100000,
                        EnableSsl = true
                    })
                    {
                        msg.To.Add(userEmail);
                        msg.From       = new MailAddress(APIConf.WowCharacterComparerEmail);
                        msg.IsBodyHtml = true;
                        msg.Subject    = emailSubject;
                        msg.Body       = emailBody;
                        client.Send(msg);
                    }

                emailSendStatus.SendSuccessfully = true;
            }
            catch (Exception ex)
            {
                emailSendStatus.SendEmailException = ex;
                emailSendStatus.SendSuccessfully   = false;
            }

            return(emailSendStatus);
        }
예제 #2
0
 public Email(int id,
              string guid,
              EmailAccount from,
              EmailContact to,
              string subject,
              string message,
              bool isHtml,
              string batchId,
              DateTime initialSendDate,
              bool isSigned,
              bool isEncrypted,
              EmailSendStatus sendingStatus,
              DateTime dateCreated,
              string createdBy,
              DateTime dateLastEdited,
              string lastEditedBy)
 {
     Id   = id;
     Guid = guid;
     From = from;
     To.Add(to);
     Subject        = subject;
     Message        = message;
     IsHtml         = isHtml;
     BatchId        = batchId;
     SendDate       = initialSendDate;
     IsSigned       = isSigned;
     IsEncrypted    = isEncrypted;
     SendingStatus  = sendingStatus;
     DateCreated    = dateCreated;
     CreatedBy      = createdBy;
     DateLastEdited = dateLastEdited;
     LastEditedBy   = lastEditedBy;
 }
예제 #3
0
        internal EmailSendStatus SendEmail(string userEmail, string emailSubject, string emailBody)
        {
            EmailSendStatus emailSendStatus = new EmailSendStatus();

            try
            {
                string decryptedEmail = CryptographyHelper.DecryptSentence(Properties.Settings.Default.Email);
                using (MailMessage msg = new MailMessage())
                    using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                    {
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials = false,
                        Credentials = new NetworkCredential(decryptedEmail, CryptographyHelper.DecryptSentence(Properties.Settings.Default.EmailPassword)),
                        Timeout = 100000,
                        EnableSsl = true
                    })
                    {
                        msg.To.Add(userEmail);
                        msg.From       = new MailAddress(decryptedEmail);
                        msg.IsBodyHtml = true;
                        msg.Subject    = emailSubject;
                        msg.Body       = emailBody;
                        client.Send(msg);
                    }
                emailSendStatus.SendSuccessfully = true;
            }

            catch (Exception ex)
            {
                emailSendStatus.SendSuccessfully   = false;
                emailSendStatus.SendEmailException = ex;
            }

            return(emailSendStatus);
        }
        public async Task <UpdateResult> UpdateStatus(string id, EmailSendStatus newStatus, string failMessage = null)
        {
            var query = _builder
                        .Eq(x => x.State, MREntityState.Active)
                        .Eq(x => x.Id, id)
                        .UpdateSet(x => x.UpdateTime, DateTime.UtcNow)
                        .UpdateSet(x => x.Status, newStatus)
                        .UpdateSet(x => x.FailMessage, failMessage);

            await UpdateByQuery(query);

            return(new UpdateResult.Acknowledged(1, 1, id));
        }
        public async Task <IEnumerable <EmailSendTask> > GetByStatus(EmailSendStatus status, int?limit = null)
        {
            var query = _builder
                        .Eq(x => x.State, MREntityState.Active)
                        .Eq(x => x.Status, status)
                        .Sorting(x => x.CreateTime, false);

            if (limit.HasValue && limit.Value > 0)
            {
                query.Limit(limit.Value);
            }

            return(await GetByQuery(query));
        }
        public void UpdateEmailStatus(int id, EmailSendStatus status, string lastEditedBy)
        {
            if (id == 0)
            {
                throw new ArgumentException(string.Format("Could not update email status - invalid email [{0}].", id));
            }
            EmailTableAdapter adapter = new EmailTableAdapter();

            EmailData.EmailDataTable table = adapter.GetEmail(id);
            if (table.Count == 0)
            {
                throw new ArgumentException(string.Format("Could not update email - email does not exist emailId=[{0}]", id));
            }
            EmailData.EmailRow row = table[0];
            row.SendStatus = status.ToString();
            adapter.Update(row);
        }
예제 #7
0
 private void SendEmail(string to, string from, string password, string subject, string body)
 {
     using (MailMessage mm = new MailMessage(from, to))
     {
         mm.Subject    = subject;
         mm.Body       = body;
         mm.IsBodyHtml = true;
         SmtpClient smtp = new SmtpClient();
         smtp.Host      = "smtp.gmail.com";
         smtp.EnableSsl = true;
         NetworkCredential NetworkCred = new NetworkCredential(from, password);
         smtp.UseDefaultCredentials = true;
         smtp.Credentials           = NetworkCred;
         smtp.Port = 587;
         using (var context = new WebApiPruebaContext())
         {
             try
             {
                 smtp.Send(mm);
                 var emailStatus = new EmailSendStatus
                 {
                     Email = to,
                     Send  = true
                 };
                 context.EmailSendStatus.Add(emailStatus);
                 context.SaveChanges();
             }
             catch (Exception ex)
             {
                 var emailStatus = new EmailSendStatus
                 {
                     Email = to,
                     Send  = false
                 };
                 context.EmailSendStatus.Add(emailStatus);
                 context.SaveChanges();
             }
         }
     }
 }
        public async Task <UpdateResult> MultiplyUpdateStatus(IEnumerable <string> ids, EmailSendStatus newStatus)
        {
            if (ids == null || !ids.Any())
            {
                return(UpdateResult.Unacknowledged.Instance);
            }

            var query = _builder
                        .Eq(x => x.State, MREntityState.Active)
                        .In(x => x.Id, ids)
                        .UpdateSet(x => x.UpdateTime, DateTime.UtcNow)
                        .UpdateSet(x => x.Status, newStatus);

            await UpdateManyByQuery(query);

            return(new UpdateResult.Acknowledged(ids.Count(), ids.Count(), ""));
        }
예제 #9
0
 public void UpdateEmailStatus(int id, EmailSendStatus status, string lastEditedBy)
 {
     throw new NotImplementedException();
 }
예제 #10
0
        /// <summary>
        /// send email pass the SendingStatus parameter with this methods
        /// O for failed, 1 for sucess
        /// </summary>
        /// <param name="SendingStatus"></param>
        internal string SendMail(out EmailSendStatus SendingStatus)
        {
            MailMessage objMailMessage = new System.Net.Mail.MailMessage();

            try
            {
                #region //Add Sender Address
                if (!string.IsNullOrEmpty(_MailFrom))
                {
                    objMailMessage.From = _MailFromName == null ? new MailAddress(_MailFrom) : new MailAddress(_MailFrom, _MailFromName);
                }

                #endregion

                #region //Add Receipient Address (TO)

                foreach (string to in _MailTo)
                {
                    if (to.Contains(";"))
                    {
                        foreach (string toadd in to.Split(';'))
                        {
                            if (!string.IsNullOrEmpty(toadd))
                            {
                                objMailMessage.To.Add(toadd);
                            }
                        }
                    }
                    else if (to.Contains(","))
                    {
                        foreach (string toadd in to.Split(','))
                        {
                            if (!string.IsNullOrEmpty(toadd))
                            {
                                objMailMessage.To.Add(toadd);
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(to))
                    {
                        objMailMessage.To.Add(to);
                    }
                }
                #endregion

                #region //Add Receipient Address (CC)
                foreach (string cc in _MailCC)
                {
                    if (cc.Contains(";"))
                    {
                        foreach (string add in cc.Split(';'))
                        {
                            if (!string.IsNullOrEmpty(add))
                            {
                                objMailMessage.CC.Add(add);
                            }
                        }
                    }
                    else if (cc.Contains(","))
                    {
                        foreach (string add in cc.Split(','))
                        {
                            if (!string.IsNullOrEmpty(add))
                            {
                                objMailMessage.CC.Add(add);
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(cc))
                    {
                        objMailMessage.CC.Add(cc);
                    }
                }
                #endregion

                #region //Add Receipient Address (BCC)
                foreach (string bcc in _MailBCC)
                {
                    if (bcc.Contains(";"))
                    {
                        foreach (string add in bcc.Split(';'))
                        {
                            if (!string.IsNullOrEmpty(add))
                            {
                                objMailMessage.Bcc.Add(add);
                            }
                        }
                    }
                    else if (bcc.Contains(","))
                    {
                        foreach (string add in bcc.Split(','))
                        {
                            if (!string.IsNullOrEmpty(add))
                            {
                                objMailMessage.Bcc.Add(add);
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(bcc))
                    {
                        objMailMessage.Bcc.Add(bcc);
                    }
                }

                #endregion

                #region //Add Subject
                objMailMessage.Subject = _Subject;
                #endregion

                #region //Message Body Html or Text
                objMailMessage.IsBodyHtml = _IsBodyHtml;
                #endregion

                #region //Message Priority
                objMailMessage.Priority = _MailPriority;
                #endregion

                #region // Message Delivery Notification
                objMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                #endregion

                #region //======== Message Body ==========
                if (_IsBodyHtml == true)
                {
                    #region // This way prevent message to be SPAM
                    objMailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    AlternateView plainView = AlternateView.CreateAlternateViewFromString
                                                  (System.Text.RegularExpressions.Regex.Replace(_Message, @"<(.|\n)*?>", string.Empty),
                                                  null, "text/plain");
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(_Message, null, "text/html");

                    objMailMessage.AlternateViews.Add(plainView);
                    objMailMessage.AlternateViews.Add(htmlView);
                    #endregion
                }
                else
                {
                    objMailMessage.Body = _Message;
                }
                #endregion

                #region //Message Encoding
                objMailMessage.BodyEncoding = _MessageEncoding;
                #endregion

                #region //Attachment
                foreach (Attachment ObjAttachment in _MailAttachments)
                {
                    objMailMessage.Attachments.Add(ObjAttachment);
                }
                #endregion

                #region //SSL checking
                objSmtpClient.EnableSsl = _EnableSSL;
                #endregion
                #region // Message Sent
                objSmtpClient.Send(objMailMessage);
                #endregion
                SendingStatus = EmailSendStatus.SuccessFullySent;
            }
            catch (SmtpFailedRecipientsException ex)
            {
                if (ex.InnerExceptions.Length > 0)
                {
                    SmtpStatusCode status = ex.InnerExceptions[0].StatusCode;
                    if (status == SmtpStatusCode.MailboxBusy)
                    {
                        SendingStatus = EmailSendStatus.MailboxBusyError;
                    }
                    else if (status == SmtpStatusCode.MailboxUnavailable)
                    {
                        SendingStatus = EmailSendStatus.MailboxUnavailableError;
                    }
                    else
                    {
                        SendingStatus = EmailSendStatus.OtherError;
                    }
                    return(ex.InnerExceptions[0].Message);
                }
                else
                {
                    SendingStatus = EmailSendStatus.OtherError;
                    return(ex.Message);
                }
            }
            catch (Exception ex)
            {
                SendingStatus = EmailSendStatus.OtherError;
                if (ex.InnerException != null)
                {
                    return(ex.InnerException.Message);
                }
                return(ex.Message);
            }
            finally
            {
                if (objMailMessage != null)
                {
                    objMailMessage.Dispose();
                }
            }
            return(null);
        }