本文整理汇总了C#中System.Net.Mail.LinkedResource.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# LinkedResource.Dispose方法的具体用法?C# LinkedResource.Dispose怎么用?C# LinkedResource.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Mail.LinkedResource
的用法示例。
在下文中一共展示了LinkedResource.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateMailMessage
/// <devdoc>
/// Creates a MailMessage using the body parameter.
/// </devdoc>
public MailMessage CreateMailMessage(string recipients, IDictionary replacements, string body, Control owner) {
if (owner == null) {
throw new ArgumentNullException("owner");
}
string from = From;
if (String.IsNullOrEmpty(from)) {
System.Net.Configuration.SmtpSection smtpSection = RuntimeConfig.GetConfig().Smtp;
if (smtpSection == null || smtpSection.Network == null || String.IsNullOrEmpty(smtpSection.From)) {
throw new HttpException(SR.GetString(SR.MailDefinition_NoFromAddressSpecified));
}
else {
from = smtpSection.From;
}
}
MailMessage message = null;
try {
message = new MailMessage(from, recipients);
if (!String.IsNullOrEmpty(CC)) {
message.CC.Add(CC);
}
if (!String.IsNullOrEmpty(Subject)) {
message.Subject = Subject;
}
message.Priority = Priority;
if (replacements != null && !String.IsNullOrEmpty(body)) {
foreach (object key in replacements.Keys) {
string fromString = key as string;
string toString = replacements[key] as string;
if ((fromString == null) || (toString == null)) {
throw new ArgumentException(SR.GetString(SR.MailDefinition_InvalidReplacements));
}
// DevDiv 151177
// According to http://msdn2.microsoft.com/en-us/library/ewy2t5e0.aspx, some special
// constructs (starting with "$") are recognized in the replacement patterns. References of
// these constructs will be replaced with predefined strings in the final output. To use the
// character "$" as is in the replacement patterns, we need to replace all references of single "$"
// with "$$", because "$$" in replacement patterns are replaced with a single "$" in the
// final output.
toString = toString.Replace("$", "$$");
body = Regex.Replace(body, fromString, toString, RegexOptions.IgnoreCase);
}
}
// If there are any embedded objects, we need to construct an alternate view with text/html
// And add all of the embedded objects as linked resouces
if (EmbeddedObjects.Count > 0) {
string viewContentType = (IsBodyHtml ? MediaTypeNames.Text.Html : MediaTypeNames.Text.Plain);
AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, viewContentType);
foreach (EmbeddedMailObject part in EmbeddedObjects) {
string path = part.Path;
if (String.IsNullOrEmpty(path)) {
throw ExceptionUtil.PropertyNullOrEmpty("EmbeddedMailObject.Path");
}
if (!UrlPath.IsAbsolutePhysicalPath(path)) {
VirtualPath virtualPath = VirtualPath.Combine(owner.TemplateControlVirtualDirectory,
VirtualPath.Create(path));
path = virtualPath.AppRelativeVirtualPathString;
}
// The FileStream will be closed by MailMessage.Dispose()
LinkedResource lr = null;
try {
Stream stream = null;
try {
stream = owner.OpenFile(path);
lr = new LinkedResource(stream);
}
catch {
if (stream != null) {
((IDisposable)stream).Dispose();
}
throw;
}
lr.ContentId = part.Name;
lr.ContentType.Name = UrlPath.GetFileName(path);
view.LinkedResources.Add(lr);
}
catch {
if (lr != null) {
lr.Dispose();
}
throw;
}
}
message.AlternateViews.Add(view);
}
else if (!String.IsNullOrEmpty(body)) {
message.Body = body;
}
message.IsBodyHtml = IsBodyHtml;
return message;
}
//.........这里部分代码省略.........
示例2: CreateMailMessage
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
{
//.........这里部分代码省略.........
示例3: SendEmail
/// <summary>
/// This method takes the input from the form, creates a mail message and sends it to the smtp server
/// </summary>
private void SendEmail()
{
// make sure we have values in user, password and To
if (ValidateForm() == false)
{
return;
}
// create mail, smtp and mailaddress objects
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient();
MailAddressCollection mailAddrCol = new MailAddressCollection();
try
{
// set the From email address information
mail.From = new MailAddress(txtBoxEmailAddress.Text);
// set the To email address information
mailAddrCol.Clear();
_logger.Log("Adding To addresses: " + txtBoxTo.Text);
mailAddrCol.Add(txtBoxTo.Text);
MessageUtilities.AddSmtpToMailAddressCollection(mail, mailAddrCol, MessageUtilities.addressType.To);
// check for Cc and Bcc, which can be empty so we only need to add when the textbox contains a value
if (txtBoxCC.Text.Trim() != "")
{
mailAddrCol.Clear();
_logger.Log("Adding Cc addresses: " + txtBoxCC.Text);
mailAddrCol.Add(txtBoxCC.Text);
MessageUtilities.AddSmtpToMailAddressCollection(mail, mailAddrCol, MessageUtilities.addressType.Cc);
}
if (txtBoxBCC.Text.Trim() != "")
{
mailAddrCol.Clear();
_logger.Log("Adding Bcc addresses: " + txtBoxBCC.Text);
mailAddrCol.Add(txtBoxBCC.Text);
MessageUtilities.AddSmtpToMailAddressCollection(mail, mailAddrCol, MessageUtilities.addressType.Bcc);
}
// set encoding for message
if (Properties.Settings.Default.BodyEncoding != "")
{
mail.BodyEncoding = MessageUtilities.GetEncodingValue(Properties.Settings.Default.BodyEncoding);
}
if (Properties.Settings.Default.SubjectEncoding != "")
{
mail.SubjectEncoding = MessageUtilities.GetEncodingValue(Properties.Settings.Default.SubjectEncoding);
}
if (Properties.Settings.Default.HeaderEncoding != "")
{
mail.HeadersEncoding = MessageUtilities.GetEncodingValue(Properties.Settings.Default.HeaderEncoding);
}
// set priority for the message
switch (Properties.Settings.Default.MsgPriority)
{
case "High":
mail.Priority = MailPriority.High;
break;
case "Low":
mail.Priority = MailPriority.Low;
break;
default:
mail.Priority = MailPriority.Normal;
break;
}
// add HTML AltView
if (Properties.Settings.Default.AltViewHtml != "")
{
ContentType ctHtml = new ContentType("text/html");
htmlView = AlternateView.CreateAlternateViewFromString(Properties.Settings.Default.AltViewHtml, ctHtml);
// add inline attachments / linked resource
if (inlineAttachmentsTable.Rows.Count > 0)
{
foreach (DataRow rowInl in inlineAttachmentsTable.Rows)
{
LinkedResource lr = new LinkedResource(rowInl.ItemArray[0].ToString());
lr.ContentId = rowInl.ItemArray[1].ToString();
lr.ContentType.MediaType = rowInl.ItemArray[2].ToString();
htmlView.LinkedResources.Add(lr);
lr.Dispose();
}
}
// set transfer encoding
htmlView.TransferEncoding = MessageUtilities.GetTransferEncoding(Properties.Settings.Default.htmlBodyTransferEncoding);
mail.AlternateViews.Add(htmlView);
}
// add Plain Text AltView
if (Properties.Settings.Default.AltViewPlain != "")
{
ContentType ctPlain = new ContentType("text/plain");
//.........这里部分代码省略.........