Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
Forwarding an Email
This example discusses the CreateForward method and why it is too simplistic for forwarding email. A better solution using ChilkatMime is presented. Rather than using CreateForward, it creates a new message/rfc822 MIME message that contains the original email, and sends a new email with the original email message attached. Dim mailman As New ChilkatMailMan2 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.) Dim email As New ChilkatEmail2 email.From = "support@chilkatsoft.com" email.AddTo "Chilkat Software", "admin@chilkatsoft.com" email.Subject = "simple email" email.Body = "Body of simple email" & vbCrLf & "line 2 of the body..." Text1.Text = email.GetMime() ' Text1.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 'line 2 of the body... Dim forward As ChilkatEmail2 Set forward = email.CreateForward() Text2.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 'line 2 of the body... ' 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) ' A more sophisticated solution is to use ChilkatMime to help: Dim mime As New ChilkatMime 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. Dim originalEmailAsMimeObj As ChilkatMime Set 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... Dim email2 As New ChilkatEmail2 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. Dim email2Mime As ChilkatMime Set 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" success = mailman.SendEmail(email2) If (success = 0) Then MsgBox mailman.LastErrorText Else MsgBox "Mail Sent!" End If ' Save our email to a .eml file so we can examine the MIME structure if desired. email2.SaveEml "email2.eml"
|
© 2000-2012 Chilkat Software, Inc. All Rights Reserved.