Sample code for 30+ languages & platforms
SQL Server

Generate a Unique Filename for an Email

See more Email Object Examples

Demonstrates the Chilkat Email.GenerateFilename method, which generates a unique filename for the email. The returned filename is different each time the method is called. This example generates one and prints it.

Background: When saving many messages to a folder — for example exporting a mailbox to .eml files — you need a distinct name for each so they do not overwrite one another. GenerateFilename produces a fresh, collision-resistant name on demand, sparing you from inventing your own scheme (timestamps, counters, GUIDs) just to keep files from clashing.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    --  Demonstrates the GenerateFilename method, which generates a unique filename for this
    --  email.  The filename is different each time the method is called.

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @email, 'Subject', 'Weekly report'

    --  Generate a unique filename (for example, to save the email to disk).
    DECLARE @fname nvarchar(4000)
    EXEC sp_OAMethod @email, 'GenerateFilename', @fname OUT


    PRINT 'Generated filename: ' + @fname

    EXEC @hr = sp_OADestroy @email


END
GO