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
|
Load MIME (or S/MIME) and Send as EmailDemonstrates how to load a MIME file and send it as email.
CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @sTmp0 nvarchar(4000) DECLARE @mime int EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @mailman int EXEC @hr = sp_OACreate 'Chilkat.MailMan2', @mailman OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- Any string argument automatically begins the 30-day trial. DECLARE @success int EXEC sp_OAMethod @mime, 'UnlockComponent', @success OUT, '30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- Any string argument automatically begins the 30-day trial. EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, '30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- Set the SMTP server. EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.chilkatsoft.com' -- Set the SMTP login/password (if required) EXEC sp_OASetProperty @mailman, 'SmtpUsername', 'myUsername' EXEC sp_OASetProperty @mailman, 'SmtpPassword', 'myPassword' -- Load the MIME (or S/MIME) from a file: EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'edifact_smime.txt' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END DECLARE @fromAddr nvarchar(4000) DECLARE @recipient nvarchar(4000) SELECT @fromAddr = 'admin@chilkatsoft.com' SELECT @recipient = 'support@chilkatsoft.com' -- Add email header fields to the MIME: EXEC sp_OAMethod @mime, 'AddHeaderField', NULL, 'From', @fromAddr EXEC sp_OAMethod @mime, 'AddHeaderField', NULL, 'To', @recipient EXEC sp_OAMethod @mime, 'AddHeaderField', NULL, 'Subject', 'Here is my EDIFACT signed and encrypted...' -- We want a Date header with the current date/time. The email object -- automatically generates it. Therefore we'll create an email object and then -- copy the Date header: DECLARE @email int EXEC @hr = sp_OACreate 'Chilkat.Email2', @email OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @dateStr nvarchar(4000) EXEC sp_OAMethod @email, 'GetHeaderField', @dateStr OUT, 'Date' EXEC sp_OAMethod @mime, 'AddHeaderField', NULL, 'Date', @dateStr -- It is not necessary to save the MIME to a file. -- We're doing it here just to have a look at the .eml in a text editor... EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, 'email.eml' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- Now send the MIME via SMTP: DECLARE @mimeContent nvarchar(4000) EXEC sp_OAMethod @mime, 'GetMime', @mimeContent OUT EXEC sp_OAMethod @mailman, 'SendMime', @success OUT, @fromAddr, @recipient, @mimeContent IF @success <> 1 BEGIN EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END EXEC sp_OAMethod @mailman, 'CloseSmtpConnection', @success OUT IF @success <> 1 BEGIN PRINT 'Connection to SMTP server not closed cleanly.' END PRINT 'Mail Sent!' END GO |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.