PowerTCP Mail for .NET
Resource Class
Members  Example 




Implements base functionality for classes that have a header and a single part (not multi-part).
Object Model
Resource ClassContentDisposition ClassContentType ClassHeaderDictionary ClassHeaderField Class
Syntax
Public Class Resource 
   Inherits Part
Dim instance As Resource
public class Resource : Part 
public __gc class Resource : public Part 
public ref class Resource : public Part 
Remarks
This class can be used to customize a single part for any use.
Example
Demonstrates saving resources to disk. This example demonstrates creating a new MailMessage and populating it with HTML that references local files, such as a "Webpage, complete (*.htm;*.html)" as saved by Internet Explorer.
/// <summary>
/// Saves the email's resources (files intended for display within the HTML 
/// message body) to the specified directory.
/// </summary>
/// <param name="message">MailMessage containing resources to save.</param>
/// <param name="saveDirectory">Directory to save resources to.</param>
private void SaveResources(MailMessage message, string saveDirectory)
{
    foreach (Resource res in message.Resources)
    {
        string filePath = Path.Combine(saveDirectory, res.ContentType.Name);
        using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
            fs.Write(res.Content, 0, res.Content.Length);
    }
}
''' <summary>
''' Saves the email's resources (files intended for display within the HTML 
''' message body) to the specified directory.
''' </summary>
''' <param name="message">MailMessage containing resources to save.</param>
''' <param name="saveDirectory">Directory to save resources to.</param>
Private Sub SaveResources(ByVal message As MailMessage, ByVal saveDirectory As String)
    For Each res As Resource In message.Resources
        Dim filePath As String = Path.Combine(saveDirectory, res.ContentType.Name)
        Using fs As New FileStream(filePath, FileMode.CreateNew, FileAccess.Write)
            fs.Write(res.Content, 0, res.Content.Length)
        End Using
    Next res
End Sub
private MailMessage getMessageFromHtml(string pathToHtml, string pathToHtmlResources, string from, string to, string subject, bool includeTextPlain)
{
    //Note: to load a .mht (Mime HTML) file, use MailMessage.Open() instead.

    //Load HTML
    string html;
    using (StreamReader sr = new StreamReader(pathToHtml))
        html = sr.ReadToEnd();

    //Initialize a MailMessage object. includeTextPlain controls whether the message should include a text/plain part 
    //(which determines whether the message is multipart/alternative (when true) or multipart/related (when false))
    string textPlain = (includeTextPlain) ? "text/plain representation of included HTML" : null;
    MailMessage mailMsg = new MailMessage(textPlain, html, new DirectoryInfo(pathToHtmlResources).GetFiles());

    //Set sender, recipients and subject
    mailMsg.From = from;
    mailMsg.To = to;
    mailMsg.Subject = subject;
    return mailMsg;
}
Private Function getMessageFromHtml(ByVal pathToHtml As String, ByVal fromAddress As String, ByVal toAddress As String, ByVal subject As String) As MailMessage
     'Note: to load a .mht (Mime HTML) file, use MailMessage.Open() instead.

     'Load HTML
     Dim sr As New StreamReader(pathToHtml)
     Dim html As String = sr.ReadToEnd()
     sr.Close()

     'Initialize a MailMessage object. includeTextPlain controls whether the message should include a 
     'text/plain part (which determines whether the message is multipart/alternative (when true) or multipart/related (when false))
     Dim textPlain As String = If(includeTextPlain, "text/plain representation of included HTML", Nothing)
     Dim mailMsg As New MailMessage(textPlain, html, New DirectoryInfo(pathToHtmlResources).GetFiles())

     'Set sender, recipients and subject
     mailMsg.From = fromAddress
     mailMsg.To = toAddress
     mailMsg.Subject = subject
     Return mailMsg
End Function
Inheritance Hierarchy

System.Object
   Dart.Mail.Part
      Dart.Mail.Resource
         Dart.Mail.Attachment
         Dart.Mail.Messagepart
         Dart.Mail.Textpart

See Also

Reference

Resource Members
Dart.Mail Namespace


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic