コード例 #1
0
        /// <summary>
        /// Returns the first non-empty string.  If all are empty, returns empty string
        /// </summary>
        public static string GetNonEmptyString(params string[] strings)
        {
            foreach (string s in strings.Where(s => !StringUtils.IsBlank(s)))
            {
                return(s);
            }

            return(string.Empty);
        }
コード例 #2
0
        /// <summary>
        /// Transmits the file using the specified http context
        /// </summary>
        /// <param name="path">The file to be sent</param>
        /// <param name="downloadFilename">The filename the browser should see</param>
        /// <param name="context">The HttpContext to which the file should be streamed</param>
        public static void TransmitFile(string path, string downloadFilename, HttpContext context)
        {
            // Ensure we have a http context
            if (context == null)
            {
                throw new ArgumentException("context cannot be null");
            }

            // Ensure the file exists before trying to stream it
            if (!File.Exists(path))
            {
                throw new HttpException(500, "File not found: " + path);
            }

            // Get the filename
            if (StringUtils.IsBlank(downloadFilename))
            {
                downloadFilename = (Path.GetFileName(path) ?? "unknown");
            }

            // Workaround for problem in IE6 where it appends [1] onto filenames containing periods
            if (context.Request.Browser.Browser == "IE")
            {
                string f  = (Path.GetFileNameWithoutExtension(downloadFilename) ?? string.Empty);
                string f2 = f.Replace(".", "%2e");
                downloadFilename = downloadFilename.Replace(f, f2);
            }

            // Wrap it in quotes to keep spaces
            downloadFilename = string.Format("\"{0}\"", downloadFilename);

            // Get content type and content disposition
            string contentType        = MimeTypeManager.GetMimeType(Path.GetExtension(path));
            string contentDisposition = string.Format("attachment;filename={0}", downloadFilename);

            // Set the headers
            context.Response.Clear();
            context.Response.ContentType = contentType;
            context.Response.AppendHeader("Content-Disposition", contentDisposition);
            context.Response.AppendHeader("Content-Length", GetFileSize(path).ToString());

            // Send the file
            context.Response.TransmitFile(path);
            context.Response.End();
        }
コード例 #3
0
ファイル: Email.cs プロジェクト: MatfRS2/SeminarskiRadovi
		/// <summary>
		/// Sends the email message.
		/// </summary>
		public void Send()
		{
			// Initialize the body from the email template if we don't have a body set
			// and a template filename has been specified.

			if ((Body.Length == 0) && (TemplateFilename.Length > 0))
			{
				string filename = GetPhysicalPath(TemplateFilename);

				if (!File.Exists(filename))
					throw new EmailTemplateNotFoundException(TemplateFilename);

				Body = XmlUtils.Transform(BodyXmlDoc.InnerXml, filename, BodyParameters);
			}

			// Initialize the email message
			MailMessage message = new MailMessage
			{
				From = GetMailAddress(FromEmail, FromName),
				Subject = Subject,
				Body = Body,
				BodyEncoding = Encoding.ASCII,
				IsBodyHtml = IsHtml
			};

			message.Headers.Add("x-sender-application", "FocusOPEN");

			if (HasDebugMode && IsDebugMode)
			{
				// Add debug email addresses
				foreach (string debugEmailAddress in DebugEmail.Split(';').Where(StringUtils.IsEmail))
					message.To.Add(new MailAddress(debugEmailAddress));

				// List of actual recipients
				JoinableList jList = new JoinableList();

				// To recipients
				foreach (string recipient in Recipients)
					jList.Add(recipient);

				// CC recipients
				foreach (string recipient in CC.Where(recipient => !Recipients.Contains(recipient)))
					jList.Add("CC:" + recipient);

				// BCC recipients
				foreach (string recipient in BCC.Where(recipient => !Recipients.Contains(recipient) && !BCC.Contains(recipient)))
					jList.Add("BCC:" + recipient);

				// Build our debug message
				StringBuilder sb = new StringBuilder();

				if (IsHtml)
				{
					// Wrap our debug message in a DIV for better viewing
					sb.Append("<div style='margin:30px 0px 30px 0px;border:2px solid red;background:#eee;padding:10px;color:#000;'>");
					sb.Append("<p><strong>This email has been sent in debug mode.</strong></p>");
					sb.Append("<p><strong>Actual Recipients:</strong></p>");
					sb.Append(jList);
					sb.Append("</div>");
				}
				else
				{
					// Just add a few line breaks for text email messages
					sb.AppendFormat("\n\n\nThis email has been sent in debug mode.  Actual recipients: {0}", jList);
				}

				// Add debug message to the email message body
				message.Body += sb.ToString();
			}
			else
			{
				//---------------------------------------------------
				// Add all recipients
				//---------------------------------------------------

				foreach (string emailaddress in Recipients)
				{
					try
					{
						message.To.Add(emailaddress);
					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}
				}

				//---------------------------------------------------
				// Add all CC recipients which haven't
				// already been added into the recipients list
				//---------------------------------------------------

				foreach (string emailaddress in CC.Where(emailaddress => !Recipients.Contains(emailaddress)))
				{
					try
					{
						message.CC.Add(emailaddress);
					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}
				}

				//---------------------------------------------------
				// Add all BCC recipients which haven't
				// already been added into the CC or recipients list
				//---------------------------------------------------

				foreach (string emailaddress in BCC.Where(emailaddress => !Recipients.Contains(emailaddress) && !CC.Contains(emailaddress)))
				{
					try
					{
						message.Bcc.Add(emailaddress);
					}
					catch (Exception ex)
					{
						Debug.WriteLine(ex.Message);
					}
				}

				//---------------------------------------------------
				// Add all static BCC recipients which haven't
				// already been added into any other lists
				//---------------------------------------------------

				if (IncludeStaticBccRecipients)
				{
					foreach (string bccEmailAddress in BccEmail.Split(';').Where(bccEmailAddress => !Recipients.Contains(bccEmailAddress) && !CC.Contains(bccEmailAddress) && !BCC.Contains(bccEmailAddress)))
					{
						try
						{
							message.Bcc.Add(bccEmailAddress);
						}
						catch (Exception ex)
						{
							Debug.WriteLine(ex.Message);
						}
					}
				}
			}

			// Add attachments
			foreach (Attachment attachment in Attachments)
				message.Attachments.Add(attachment);

			// Only send the email message if the email engine is actually enabled
			if (EngineEnabled)
			{
				// Initialize the smtp client to send the email
				SmtpClient client = new SmtpClient(MailServer);

				// Add credentials to authenticate to mail server if required
				if (!StringUtils.IsBlank(MailServerUsername) || !StringUtils.IsBlank(MailServerPassword))
					client.Credentials = new System.Net.NetworkCredential(MailServerUsername, MailServerPassword, MailServerLogonDomain);

				// Send the message
				client.Send(message);
			}
		}