Beispiel #1
0
        public ReturnClass UploadImageField(string filepath, string imagefieldparametername, string Sql)
        {
            ReturnClass   outcome   = new ReturnClass(true);
            SP_Parameters p         = new SP_Parameters();
            SqlCommand    cmd       = null;
            Stream        imgStream = null;
            FileInfo      file      = null;

            byte[] imgBinaryData = null;
            int    RowsAffected  = 0;
            int    filesize      = 0;
            int    n             = 0;

            if (!imagefieldparametername.StartsWith("@"))
            {
                imagefieldparametername = "@" + imagefieldparametername;
            }
            if (!File.Exists(filepath))
            {
                outcome.SetFailureMessage("The file does not exist or is not accessible.");
            }

            if (outcome.Success)
            {
                try {
                    file     = new FileInfo(filepath);
                    filesize = Convert.ToInt32(file.Length);
                } catch (Exception ex) {
                    outcome.Success = false;
                    outcome.Message = ex.Message;
                }
            }

            if (outcome.Success)
            {
                try {
                    imgStream     = File.OpenRead(filepath);
                    imgBinaryData = new byte[filesize];
                    n             = imgStream.Read(imgBinaryData, 0, filesize);
                } catch (Exception ex) {
                    outcome.Success = false;
                    outcome.Message = ex.Message;
                }
            }

            if (outcome.Success)
            {
                try {
                    cmd = new SqlCommand(Sql, conn);
                    if (commandtimeout > 0)
                    {
                        cmd.CommandTimeout = commandtimeout;
                    }
                    p.Add(imagefieldparametername, SqlDbType.Image, filesize, ParameterDirection.Input, imgBinaryData);
                    foreach (SqlParameter objparam1 in p)
                    {
                        cmd.Parameters.Add(objparam1);
                    }
                } catch (Exception ex) {
                    outcome.Success = false;
                    outcome.Message = ex.Message;
                }
            }

            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            if (outcome.Success)
            {
                try {
                    RowsAffected = cmd.ExecuteNonQuery();
                } catch (Exception ex) {
                    outcome.Success = false;
                    outcome.Message = ex.Message;
                }
            }
            try {
                imgStream.Close();
            } catch (Exception ex) {
            }
            return(outcome);
        }
        public ReturnClass Send(SimpleEmailVO VO, string deliveryemail)
        {
            LoggingClass  log        = new LoggingClass(logfolder);
            ReturnClass   outcome    = new ReturnClass(true);
            AlternateView av1        = null;
            AlternateView av2        = null;
            SmtpClient    mailSender = new SmtpClient();

            if (VO.FromName.Length == 0)
            {
                VO.FromName = VO.FromEmail;
            }
            if (VO.ToName.Length == 0)
            {
                VO.ToName = VO.ToEmail;
            }

            if (StringFuncs.IsValidEmail(VO.FromEmail) && StringFuncs.IsValidEmail(VO.ToEmail))
            {
                MailMessage mailMessage = new MailMessage(new MailAddress(VO.FromEmail, VO.FromName), new MailAddress(VO.ToEmail, VO.ToName));

                if (StringFuncs.IsValidEmail(deliveryemail))
                {
                    mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;
                    mailMessage.Headers.Add("Disposition-Notification-To", deliveryemail);

                    //mailMessage.Headers.Add("Errors-To", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("Delivered-To", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("Return-Receipt-Requested", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("Return-Receipt-To", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("Read-Receipt-Requested", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("Read-Receipt-To", Config.EmailerDeliveryEmail);
                    //mailMessage.Headers.Add("X-Confirm-reading-to", Config.EmailerDeliveryEmail);
                }

                try {
                    if (VO.HtmlBody.Length > 0)
                    {
                        if (VO.TextBody.Length > 0)
                        {
                            av2 = AlternateView.CreateAlternateViewFromString(VO.TextBody, null, MediaTypeNames.Text.Plain);
                            mailMessage.AlternateViews.Add(av2);
                            av1 = AlternateView.CreateAlternateViewFromString(VO.HtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
                            mailMessage.AlternateViews.Add(av1);
                        }
                        else
                        {
                            av1 = AlternateView.CreateAlternateViewFromString(VO.HtmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
                            mailMessage.AlternateViews.Add(av1);
                        }
                    }
                    else
                    {
                        av2 = AlternateView.CreateAlternateViewFromString(VO.TextBody, null, MediaTypeNames.Text.Plain);
                        mailMessage.AlternateViews.Add(av2);
                    }
                } catch (Exception ex) {
                    outcome.SetFailureMessage("Error setting email body:" + ex.ToString());
                }

                if (outcome.Success)
                {
                    try {
                        mailMessage.Subject = VO.Subject;
                        if (VO.HtmlBody.Length > 0)
                        {
                            mailMessage.IsBodyHtml = true;
                        }
                        else
                        {
                            mailMessage.IsBodyHtml = false;
                        }
                        mailMessage.Priority = MailPriority.Normal;

                        if (StringFuncs.IsValidEmail(VO.ReplyToEmail))
                        {
                            mailMessage.Headers.Add("Return-Path", VO.ReplyToEmail);
                            mailMessage.ReplyTo = new MailAddress(VO.ReplyToEmail, VO.ReplyToEmail);
                        }
                        else
                        {
                            mailMessage.Headers.Add("Return-Path", VO.FromEmail);
                            mailMessage.ReplyTo = new MailAddress(VO.FromEmail, VO.FromName);
                        }

                        mailSender.EnableSsl = UseSSL;
                    } catch (Exception ex) {
                        outcome.SetFailureMessage("Error setting message settings:" + ex.ToString());
                    }

                    if (outcome.Success)
                    {
                        try {
                            mailSender.Send(mailMessage);
                        } catch (SmtpException ex) {
                            log.Log("EmailClass - Error sending email to " + VO.ToName + " :" + ex.StatusCode + " [" + ex.Message + "][" + ex.InnerException + "]", "MHPNET2EmailLib.EmailClass");
                            outcome.SetFailureMessage("EmailClass - Email error " + ex.StatusCode + " [" + ex.Message + "]");
                        } catch (Exception ex) {
                            log.Log("EmailClass - Error sending email to " + VO.ToName + " :" + ex.Message + "[" + ex.InnerException + "]", "MHPNET2EmailLib.EmailClass");
                            outcome.SetFailureMessage("EmailClass - Email error  [" + ex.Message + "]");
                        }
                    }
                }
            }
            else
            {
                outcome.SetFailureMessage("Either the target (" + VO.ToEmail + ") or sender email (" + VO.FromEmail + ")is invalid");
            }

            return(outcome);
        }