OpenFile() protected method

protected OpenFile ( string path ) : Stream
path string
return System.IO.Stream
コード例 #1
0
 public MailMessage CreateMailMessage(string recipients, IDictionary replacements, Control owner)
 {
     if (owner == null)
     {
         throw new ArgumentNullException("owner");
     }
     string body = string.Empty;
     string bodyFileName = this.BodyFileName;
     if (!string.IsNullOrEmpty(bodyFileName))
     {
         string path = bodyFileName;
         if (!UrlPath.IsAbsolutePhysicalPath(path))
         {
             path = UrlPath.Combine(owner.AppRelativeTemplateSourceDirectory, path);
         }
         TextReader reader = new StreamReader(owner.OpenFile(path));
         try
         {
             body = reader.ReadToEnd();
         }
         finally
         {
             reader.Close();
         }
     }
     return this.CreateMailMessage(recipients, replacements, body, owner);
 }
コード例 #2
0
 public MailMessage CreateMailMessage(string recipients, IDictionary replacements, string body, Control owner)
 {
     MailMessage message2;
     if (owner == null)
     {
         throw new ArgumentNullException("owner");
     }
     string from = this.From;
     if (string.IsNullOrEmpty(from))
     {
         SmtpSection smtp = RuntimeConfig.GetConfig().Smtp;
         if (((smtp == null) || (smtp.Network == null)) || string.IsNullOrEmpty(smtp.From))
         {
             throw new HttpException(System.Web.SR.GetString("MailDefinition_NoFromAddressSpecified"));
         }
         from = smtp.From;
     }
     MailMessage message = null;
     try
     {
         message = new MailMessage(from, recipients);
         if (!string.IsNullOrEmpty(this.CC))
         {
             message.CC.Add(this.CC);
         }
         if (!string.IsNullOrEmpty(this.Subject))
         {
             message.Subject = this.Subject;
         }
         message.Priority = this.Priority;
         if ((replacements != null) && !string.IsNullOrEmpty(body))
         {
             foreach (object obj2 in replacements.Keys)
             {
                 string pattern = obj2 as string;
                 string replacement = replacements[obj2] as string;
                 if ((pattern == null) || (replacement == null))
                 {
                     throw new ArgumentException(System.Web.SR.GetString("MailDefinition_InvalidReplacements"));
                 }
                 replacement = replacement.Replace("$", "$$");
                 body = Regex.Replace(body, pattern, replacement, RegexOptions.IgnoreCase);
             }
         }
         if (this.EmbeddedObjects.Count > 0)
         {
             string mediaType = this.IsBodyHtml ? "text/html" : "text/plain";
             AlternateView item = AlternateView.CreateAlternateViewFromString(body, null, mediaType);
             foreach (EmbeddedMailObject obj3 in this.EmbeddedObjects)
             {
                 string path = obj3.Path;
                 if (string.IsNullOrEmpty(path))
                 {
                     throw ExceptionUtil.PropertyNullOrEmpty("EmbeddedMailObject.Path");
                 }
                 if (!UrlPath.IsAbsolutePhysicalPath(path))
                 {
                     path = VirtualPath.Combine(owner.TemplateControlVirtualDirectory, VirtualPath.Create(path)).AppRelativeVirtualPathString;
                 }
                 LinkedResource resource = null;
                 try
                 {
                     Stream contentStream = null;
                     try
                     {
                         contentStream = owner.OpenFile(path);
                         resource = new LinkedResource(contentStream);
                     }
                     catch
                     {
                         if (contentStream != null)
                         {
                             contentStream.Dispose();
                         }
                         throw;
                     }
                     resource.ContentId = obj3.Name;
                     resource.ContentType.Name = UrlPath.GetFileName(path);
                     item.LinkedResources.Add(resource);
                 }
                 catch
                 {
                     if (resource != null)
                     {
                         resource.Dispose();
                     }
                     throw;
                 }
             }
             message.AlternateViews.Add(item);
         }
         else if (!string.IsNullOrEmpty(body))
         {
             message.Body = body;
         }
         message.IsBodyHtml = this.IsBodyHtml;
         message2 = message;
     }
     catch
     {
         if (message != null)
         {
             message.Dispose();
         }
         throw;
     }
     return message2;
 }