Beispiel #1
0
 /// <summary>
 /// Set To Address to the Mail
 /// </summary>
 /// <param name="mailMessage"></param>
 /// <param name="mailMsg"></param>
 private void SetToaddress(System.Web.Mail.MailMessage mailMessage, MailMessage mailMsg)
 {
     try
     {   //checking toAddress string is null or empty if it not null then add to mail
         if (!String.IsNullOrEmpty(ToAddress))
         {
             //split the toAddress string and add to an array
             string[] toArray = ToAddress.Split(";".ToCharArray());
             ValidateRecipientEmail(toArray);//validating the email address
             //if mail is net.mail
             if (IsSmtpClientMail)
             {
                 foreach (string to in toArray.Where(to => !to.Equals("")))
                 {
                     mailMsg.To.Add(to); //add toAddress array to mailmessage.To
                 }
             }
             else
             {
                 //if mail is web.mail set toAddress string to MailMessage.To
                 ToAddress      = ToAddress.TrimEnd(';');
                 mailMessage.To = ToAddress; //multiple to address
             }
         }
         else
         {   //Toaddress is null the set ToFlag=1
             ToFlag = 1;
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Beispiel #2
0
        /// <summary>
        /// Sending mail in GF Networks
        /// </summary>
        /// <param name="recipeient">Receiver(Optional wil set to default recipients)</param>
        /// <param name="subject">Subject for the mail</param>
        /// <param name="bodyText">Content for the mail body</param>
        public static bool SendMail(string subject, string body)
        {
            try
            {
                if (EnableMails != "true")
                {
                    return(false);
                }

                MailMessage mMailMessage = new MailMessage();
                mMailMessage.From = new MailAddress(FromAddress);

                foreach (var item in ToAddress.Split(';'))
                {
                    mMailMessage.To.Add(item);
                }

                foreach (var item in CCAddress.Split(';'))
                {
                    mMailMessage.CC.Add(item);
                }

                mMailMessage.Subject = subject;
                mMailMessage.Body    = body;

                mMailMessage.Priority = MailPriority.High;

                SmtpClient mSmtpClient = new SmtpClient();
                mSmtpClient.Host = SmtpServer;

                if (Port != string.Empty)
                {
                    mSmtpClient.Port = Convert.ToInt32(Port);// 587
                }
                mSmtpClient.EnableSsl = true;

                ServicePointManager.ServerCertificateValidationCallback =
                    delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                { return(true); };
                mSmtpClient.Send(mMailMessage);
            }
            catch (SmtpException ex)
            {
                string strError = ex.Message;
                return(false);
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
                return(false);
            }
            return(true);
        }
Beispiel #3
0
        public bool SendEmailToServer()
        {
            SmtpClient email = new SmtpClient
            {
                DeliveryMethod        = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                EnableSsl             = enableSsl,
                Host        = host,
                Port        = port,
                Credentials = new NetworkCredential(userName, password)
            };

            string[] adrs = ToAddress.Split(';');

            MailMessage msg = new MailMessage(userName, adrs[0]);

            msg.Subject = Subject;
            msg.Body    = Body;

            if (adrs.Length > 1)
            {
                for (int i = 1; i < adrs.Length; i++)
                {
                    msg.To.Add(adrs[i]);
                }
            }


            if (!string.IsNullOrEmpty(CcAddress))
            {
                string[] adrscc = CcAddress.Split(';');
                foreach (string adr in adrscc)
                {
                    msg.To.Add(adr);
                }
            }


            foreach (string filename in attachments)
            {
                msg.Attachments.Add(new Attachment(filename));
            }

            try
            {
                email.Send(msg);
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Beispiel #4
0
        public Boolean SendMessageWithQP()
        {
            using (var MailMessage = new MailMessage())
            {
                MailMessage.From       = new MailAddress(FromAddress);
                MailMessage.Subject    = Subject;
                MailMessage.Body       = Body;
                MailMessage.Priority   = MailPriority;
                MailMessage.IsBodyHtml = IsBodyHTML;


                string[] toAddressSplit = ToAddress.Split(';');
                var      toContacts     = (from item in toAddressSplit where !String.IsNullOrWhiteSpace(item) select item).ToArray();
                if (toContacts.Length > 0)
                {
                    MailMessage.To.Add(String.Join(",", toContacts));
                }


                // Add the To CC Email Address in Bulk
                string[] toCCAddressSplit = ToAddressCC.Split(';');
                var      ccContacts       = (from item in toCCAddressSplit where !String.IsNullOrWhiteSpace(item) select item).ToArray();
                if (ccContacts.Length > 0)
                {
                    MailMessage.CC.Add(String.Join(",", ccContacts));
                }

                // Add message attachments in bulk
                if (Attachments != null && Attachments != string.Empty)
                {
                    string[] attachmentsTemp = Attachments.Split(';');

                    foreach (string attachTemp in attachmentsTemp)
                    {
                        Attachment attach = new Attachment(attachTemp);
                        attach.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
                        MailMessage.Attachments.Add(new Attachment(attachTemp));
                    }
                }


                using (SmtpClient client = new SmtpClient(SMTPServerName))
                {
                    client.Send(MailMessage);
                    // MailMessage.Attachments.Dispose();
                }
                return(true);
            }
        }
Beispiel #5
0
        /// <summary>
        /// save the message.
        /// </summary>
        /// <returns></returns>
        public Boolean SaveMessage(string DocumentPath)
        {
            using (var MailMessage = new MailMessage())
            {
                MailMessage.From       = new MailAddress(FromAddress);
                MailMessage.Subject    = Subject;
                MailMessage.Body       = Body;
                MailMessage.Priority   = MailPriority;
                MailMessage.IsBodyHtml = IsBodyHTML;


                string[] toAddressSplit = ToAddress.Split(';');
                var      toContacts     = (from item in toAddressSplit where !String.IsNullOrWhiteSpace(item) select item).ToArray();
                if (toContacts.Length > 0)
                {
                    MailMessage.To.Add(String.Join(",", toContacts));
                }


                // Add the To CC Email Address in Bulk
                string[] toCCAddressSplit = ToAddressCC.Split(';');
                var      ccContacts       = (from item in toCCAddressSplit where !String.IsNullOrWhiteSpace(item) select item).ToArray();
                if (ccContacts.Length > 0)
                {
                    MailMessage.CC.Add(String.Join(",", ccContacts));
                }

                // Add message attachments in bulk
                if (Attachments != null && Attachments != string.Empty)
                {
                    string[] attachmentsTemp = Attachments.Split(';');

                    foreach (string attachTemp in attachmentsTemp)
                    {
                        MailMessage.Attachments.Add(new Attachment(attachTemp));
                    }
                }


                using (SmtpClient client = new SmtpClient(SMTPServerName))
                {
                    client.PickupDirectoryLocation = DocumentPath;
                    client.DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    client.Send(MailMessage);
                }
                return(true);
            }
        }
Beispiel #6
0
        public void Send()
        {
            var message      = new MailMessage();
            var toAddresses  = ToAddress.Split(';');
            var ccAddresses  = CCAddress.Split(';');
            var bccaddresses = BccAddress.Split(';');

            try
            {
                foreach (String address in toAddresses)
                {
                    message.To.Add(address);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The To Address field contained bad input.");
                Console.WriteLine("A valid email address may only contain letters, numbers, an underscore, a dash, a period, or an @ symbol.");
            }

            try
            {
                foreach (String address in ccAddresses)
                {
                    message.CC.Add(address);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The CC Address field contained bad input.");
                Console.WriteLine("A valid email address may only contain letters, numbers, an underscore, a dash, a period, or an @ symbol.");
            }

            try
            {
                foreach (String address in bccaddresses)
                {
                    message.Bcc.Add(address);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The BCC Address field contained bad input.");
                Console.WriteLine("A valid email address may only contain letters, numbers, an underscore, a dash, a period, or an @ symbol.");
            }

            try
            {
                message.From = new MailAddress(FromAddress);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The From Address field contained bad input.");
                Console.WriteLine("A valid email address may only contain letters, numbers, an underscore, a dash, a period, or an @ symbol.");
            }

            message.Subject    = Subject;
            message.Body       = MessageBody;
            message.IsBodyHtml = IsBodyHtml;
            client.Send(message);
        }
Beispiel #7
0
//		public EMailResponseObjectModel(string hostName, int port)
//		{
//			mailMessageToSend = new MailMessage();
//			smtpClient = new SmtpClient(hostName, port);
//		}

//		public void SendMessage(MailMessage message)
//		{
//			smtpClient.SendAsync(message, message.GetHashCode().ToString());
//		}

//		public void SendMessage(string from, string to, string subject, string message)
//		{
//			MailAddress fromMailAddr;
//			MailAddress toMailAddr;
//			MailMessage mailMessage;
//
//			if(from.IndexOf(',') != -1)
//			{
//				string fromAddr = from.Remove(from.IndexOf(','),from.Length);
//				string fromAddrDisplayAs = from.Remove(0,from.IndexOf(','));
//				fromMailAddr = new MailAddress(fromAddr,
//				                               fromAddrDisplayAs,
//				                               System.Text.Encoding.UTF8);
//			}
//			else
//			{
//				fromMailAddr = new MailAddress(from, from, System.Text.Encoding.UTF8);
//			}
//
//			toMailAddr = new MailAddress(to);
//
//			mailMessage = new MailMessage(fromMailAddr.Address,toMailAddr.Address, subject, message);
//			mailMessage.BodyEncoding = Encoding.UTF8;
//			mailMessage.SubjectEncoding = Encoding.UTF8;
//
//			smtpClient.UseDefaultCredentials = useDefaultCredentials;
//
//			if(smtpCredentials != null)
//			{
//				smtpClient.Credentials = smtpCredentials;
//			}
//
//			smtpClient.SendAsync(mailMessage, mailMessage.GetHashCode().ToString());
//
//		}

        /// <summary>
        ///
        /// </summary>
        public async void SendMessage()
        {
            try{
                smtpClient = new SmtpClient(this.SMTPServerName, this.PortNumber);

                smtpClient.SendCompleted += (s, e) => {
                    if (e.Error != null)
                    {
                        LogWriter.CreateLogEntry(string.Format("{0}\n{1}", e.Error.Message, e.Error.InnerException != null ? e.Error.InnerException.Message : ""));
                    }
                    //smtpClient.Dispose();
                    //mailMessageToSend.Dispose();
                };

                MailAddress fromMailAddr;
                MailAddress toMailAddr;
                MailMessage mailMessage = new MailMessage();

                if (FromAddress.IndexOf(',') != -1)
                {
                    string fromAddr          = FromAddress.Remove(FromAddress.IndexOf(','), FromAddress.Length);
                    string fromAddrDisplayAs = FromAddress.Remove(0, FromAddress.IndexOf(','));
                    fromMailAddr = new MailAddress(fromAddr,
                                                   fromAddrDisplayAs,
                                                   System.Text.Encoding.UTF8);
                }
                else
                {
                    fromMailAddr = new MailAddress(FromAddress, FromAddress, System.Text.Encoding.UTF8);
                }

                mailMessage.From = fromMailAddr;

                string[] receipents = ToAddress.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                if (this.UseDefaultCredentials)
                {
                    smtpClient.UseDefaultCredentials = this.UseDefaultCredentials;
                }
                else
                {
                    smtpClient.UseDefaultCredentials = this.UseDefaultCredentials;
                    smtpClient.Credentials           = new NetworkCredential(this.SMTPCredentials.UserName, this.SMTPCredentials.SecurePassword);
                }

                smtpClient.EnableSsl      = this.IsSSLEnabled;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                foreach (string receipent in receipents)
                {
                    if (receipent.IndexOf(',') != -1)
                    {
                        string toAddr          = receipent.Remove(receipent.IndexOf(','), receipent.Length);
                        string toAddrDisplayAs = receipent.Remove(0, receipent.IndexOf(','));
                        toMailAddr = new MailAddress(toAddr,
                                                     toAddrDisplayAs,
                                                     System.Text.Encoding.UTF8);
                    }
                    else
                    {
                        toMailAddr = new MailAddress(receipent, receipent, System.Text.Encoding.UTF8);
                    }

                    mailMessage.To.Add(toMailAddr);
                }
                mailMessage.Body    = MessageBody;
                mailMessage.Subject = Subject;

                mailMessage.BodyEncoding    = Encoding.UTF8;
                mailMessage.SubjectEncoding = Encoding.UTF8;


                await smtpClient.SendMailAsync(mailMessage);
            }

            catch (Exception e)
            {
                LogWriter.CreateLogEntry(string.Format("{0}; {1}; {2}", DateTime.Now, e.Message, e.InnerException.Message));
            }
        }