PowerTCP Mail for .NET
Clone Method (MailMessage)
Example 




Creates a deep copy of the MailMessage, but deep/shallow copies of its Parts. Attachments and Resources are shallow copies, Textparts and Htmlparts are deep copies.
Syntax
Public Overrides Function Clone() As Object
Dim instance As MailMessage
Dim value As Object
 
value = instance.Clone()
public override object Clone()
public: Object* Clone(); override 
public:
Object^ Clone(); override 

Return Value

A MailMessage.
Remarks
Note: Calling Dispose() on the original or cloned MailMessage will dispose of any shallow-copied parts that the original and clone reference.
Example
This example demonstrates how to create a custom reply message. The reply text is followed by some header fields and the text from the original message.
private MailMessage createCustomReplyMessage(MailMessage originalMessage, string replyText, string fromAddress)
{
    //Clone the original message
    MailMessage replyMessage = originalMessage.Clone() as MailMessage;

    //Set the recipient address
    if (!String.IsNullOrWhiteSpace(replyMessage.ReplyTo))
        replyMessage.To = replyMessage.ReplyTo;
    else if (!String.IsNullOrWhiteSpace(replyMessage.From))
        replyMessage.To = replyMessage.From;

    //Set the sender's address
    replyMessage.From = fromAddress;

    //Indicate that this is a reply in the subject
    replyMessage.Subject = "Re: " + originalMessage.Subject;

    //Clear some properties of the message
    replyMessage.ReplyTo = "";
    replyMessage.Cc = "";
    replyMessage.Sender = "";

    //Set the message text to be the reply followed by some of the original
    //message's header fields and the original text
    string messageText = replyText + Environment.NewLine + Environment.NewLine;
    messageText += "From: " + originalMessage.From + Environment.NewLine;
    messageText += "Sent: " + originalMessage.Date.ToString() + Environment.NewLine;
    messageText += "To: " + originalMessage.To + Environment.NewLine;
    messageText += "Subject: " + originalMessage.Subject + Environment.NewLine + Environment.NewLine;

    //Set the plain text
    replyMessage.Text = messageText + originalMessage.Text;

    //Set the HTML text
    string html = originalMessage.Html;

    //Locate place in HTML string to insert reply text
    int textStartIndex = html.ToLower().IndexOf("<body");
    if (textStartIndex == -1) 
        textStartIndex = html.ToLower().IndexOf("<html");
    textStartIndex = html.IndexOf(">", textStartIndex) + 1;

    //Insert reply text into HTML string and set the Body.Html
    messageText = "<div style=\"color:black\">" + messageText.Replace("\r\n", "<br />") + "</div>";
    html = html.Insert(textStartIndex, messageText);
    replyMessage.Html = html;
    return replyMessage;
}
Private Function createCustomReplyMessage(ByVal originalMessage As MailMessage, ByVal replyText As String, ByVal fromAddress As String) As MailMessage
    'Clone the original message
    Dim replyMessage As MailMessage = TryCast(originalMessage.Clone(), MailMessage)

    'Set the recipient address
    If Not String.IsNullOrWhiteSpace(replyMessage.ReplyTo) Then
        replyMessage.To = replyMessage.ReplyTo
    ElseIf Not String.IsNullOrWhiteSpace(replyMessage.From) Then
        replyMessage.To = replyMessage.From
    End If

    'Set the sender's address
    replyMessage.From = fromAddress

    'Indicate that this is a reply in the subject
    replyMessage.Subject = "Re: " & originalMessage.Subject

    'Clear some properties of the message
    replyMessage.ReplyTo = ""
    replyMessage.Cc = ""
    replyMessage.Sender = ""

    'Set the message text to be the reply followed by some of the original
    'message's header fields and the original text
    Dim messageText As String = replyText & Environment.NewLine & Environment.NewLine
    messageText &= "From: " & originalMessage.From & Environment.NewLine
    messageText &= "Sent: " & originalMessage.Date.ToString() & Environment.NewLine
    messageText &= "To: " & originalMessage.To & Environment.NewLine
    messageText &= "Subject: " & originalMessage.Subject & Environment.NewLine & Environment.NewLine

    'Set the plain text
    replyMessage.Text = messageText & originalMessage.Text

    'Set the HTML text
    Dim html As String = originalMessage.Html

    'Locate place in HTML string to insert reply text
    Dim textStartIndex As Integer = html.ToLower().IndexOf("<body")
    If textStartIndex = -1 Then
        textStartIndex = html.ToLower().IndexOf("<html")
    End If
    textStartIndex = html.IndexOf(">", textStartIndex) + 1

    'Insert reply text into HTML string and set the Body.Html
    messageText = "<div style=""color:black"">" & messageText.Replace(vbCrLf, "<br />") & "</div>"
    html = html.Insert(textStartIndex, messageText)
    replyMessage.Html = html
    Return replyMessage
End Function
See Also

Reference

MailMessage Class
MailMessage Members


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