C# Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

C# Examples

Bounced Mail
Bz2
Character Encoding
CSV
DKIM / DomainKey
Digital Certificates
Digital Signatures
Email
Email Object
FTP
HTML Conversion
HTTP
IMAP
Encryption
MHT / HTML Email
MIME
POP3
RSA
S/MIME
SMTP
Socket
Spider
SSH
SSH Tunnel
SSH Key
SFTP
Tar Archive
Upload
XML
XMP
Zip Compression


More Examples...
Amazon S3
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

Forwarding an Email as an Attached message/rfc822 Message

Download: Chilkat .NET Assemblies

This C# email example discusses the CreateForward method, why it is over-simplistic, and then discusses a way to attach an existing email as a message/rfc822 attachment to be forwarded.

        // This example discusses the Email.CreateForward method and why
        // it is too over-simplistic to realistically create an email for
        // forwarding.  It then describes how to forward an email as an attached
        // message, which is how many popular email clients behave.  However,
        // the advanced MIME manipulation required to do this involves the Chilkat.Mime
        // class, which is a commercial product licensed separately.
        private void button11_Click(object sender, EventArgs e)
        {
            Chilkat.MailMan mailman = new Chilkat.MailMan();
            mailman.UnlockComponent("anything for 30-day trial");

            // The CreateForward is a convenience method for creating a new email for
            // forwarding from an existing email.  It only works for plain-text emails.
            // Let's begin the example and explain why...
    
            // Create a simple email. (Normally we would get the email object by
            // reading email from a POP3 server.)
            Chilkat.Email email = new Chilkat.Email();
            email.From = "support@chilkatsoft.com";
            email.AddTo("Chilkat Software", "admin@chilkatsoft.com");
            email.Subject = "simple email";
            email.Body = "Body of simple email";
    
            textBox2.Text = email.GetMime();
// textBox2.Text now displays this:
/*
MIME-Version: 1.0
Date: Fri, 04 Aug 2006 08:33:30 -0500
Message-ID: <CHILKAT-MID-cf2afec9-e98e-484e-94a1-15e13ddffa9a@dotnet>
X-Mailer: Chilkat Software Inc (http://www.chilkatsoft.com)
X-Priority: 3 (Normal)
From: support@ chilkatsoft.com
return-path: support@chilkatsoft.com
To: "Chilkat Software" <admin@chilkatsoft.com>
subject: simple email
Content-Type: text/plain
Content-Transfer-encoding: quoted-printable

Body of simple email
*/    
            // Now create an email object for forwarding based...
            Chilkat.Email forward = email.CreateForward();
    
            textBox1.Text = forward.GetMime();
/*
The forward email looks like this:

return-path: support@chilkatsoft.com
Content-Type: text/plain
Content-Transfer-encoding: quoted-printable
Subject: FW: simple email
MIME-Version: 1.0
Date: Fri, 04 Aug 2006 08:35:12 -0500
Message-ID: <CHILKAT-MID-ce735a61-fc99-4789-9c47-fff5633b191d@dotnet>
X-Mailer: Chilkat Software Inc (http://www.chilkatsoft.com)
X-Priority: 3 (Normal)

-----Original Message-----
From: support@ chilkatsoft.com
Sent: Fri, 04 Aug 2006 08:35:12 -0500
To: "Chilkat Software" <admin@chilkatsoft.com>
Subject: simple email

Body of simple email
*/
        
        // The CreateForward method created a new email where the subject has
        // a "FW:" prepended.  Also, the body contains the original email with
        // the string "-----Original Message-----" followed by the critical
        // header information and body of the original email.  The "To" header
        // field has been removed from the forward email, because you will
        // specify the "TO" recipient for it:
        forward.AddTo("Matt", "matt@chilkatsoft.com");
        
        // The From address has also been removed.  You have to add yours to the
        // forward email:
        forward.From = "admin@chilkatsoft.com";
        
        // The CreateForward method is too simplistic for HTML email, or for emails
        // containing multiple alternative bodies (i.e. plain-text and HTML)
        // The reason is that CreateForward simply prepends the existing body with some additional
        // plain-text.  That doesn't usually work very well with HTML.

        // A more advanced (and correct) solution is to use ChilkatMime to add
        // append the original messaged as a "message/rfc822" attachment to the forwarding email.      
        // For this we need Chilkat.Mime
        Chilkat.Mime mime = new Chilkat.Mime();
        mime.UnlockComponent("anything for 30-day trial");
        
        // The goal here is to attach the original email as a message/rfc822
        // Get the original email as a MIME object.
        Chilkat.Mime originalEmailAsMimeObj = email.GetMimeObject();
        
        // Create a new message/rfc822 MIME message, with a payload containing
        // the original email.
        mime.NewMessageRfc822(originalEmailAsMimeObj);
        
        // Create a new email -- anything we want...
        Chilkat.Email email2 = new Chilkat.Email();
        email2.Subject = "This is a forwarded email, see attached message";
        email2.AddTo("Matt", "matt@chilkatsoft.com");
        email2.From = "admin@chilkatsoft.com";
        email2.AddPlainTextAlternativeBody("This is the plain-text alternative body of the forwarded email");
        email2.AddHtmlAlternativeBody("<html><body><b>This is the HTML alternative of the forwarded email</b></body></html>");
        email2.AddStringAttachment("blahblah.txt", "This is an attachment from from an in-memory string");
        
        // We'll want to attach the original email to this email, but we really need Chilkat Mime to do it...
        // So first convert email2 to a MIME object.
        Chilkat.Mime email2Mime = email2.GetMimeObject();
        
        // The "mime" variable contains our message/rfc822 MIME with the original email.
        // Append it as a new sub-part to email2Mime
        email2Mime.AppendPart(mime);
        
        // Load it back into email2.
        email2.SetFromMimeText(email2Mime.GetMime());
        
        // Send this new email which contains the original as an attached message.
        mailman.SmtpHost = "smtp.comcast.net";
        bool success = mailman.SendEmail(email2);
        if (!success)
        {
            MessageBox.Show(mailman.LastErrorText);
        }
        else
        {
            MessageBox.Show("Send Complete!");
        }
        
       // Save our email to a .eml file so we can examine the MIME structure if desired.
        email2.SaveEml("email2.eml");

        }

Important: The download for this example does not contain the ChilkatDotNet.dll which
must be downloaded and installed separately at http://www.chilkatsoft.com/downloads.asp.
Once installed, add a reference to the DLL in the project by following the instructions at
http://www.example-code.com/vbdotnet/step2.asp

 

© 2000-2012 Chilkat Software, Inc. All Rights Reserved.

Email Component · XML Parser