How to Create a Direct Mail Item

This topic shows how to create a DirectMail object. You can use a DirectMail object to create direct mail campaigns.

How to create a DirectMail object in Agent Mode

  1. In Visual Studio, create a new Commerce Server Core Systems Web application.

  2. Add the Microsoft.CommerceServer.CrossTierTypes and Microsoft.CommerceServer.Marketing.CrossTierTypes references to the application.

  3. Add a using directive for the Microsoft.CommerceServer and Microsoft.CommerceServer.Marketing namespaces.

  4. Define the Marketing Web Service URL.

  5. Create the MarketingContext object.

    For more information, see How to Connect to the Marketing System. In this example, the MarketingContext is named marketingSystem.

  6. Look up the default Campaign ID.

  7. Create a new StaticList object.

  8. Add e-mail names to the StaticList object.

  9. Create a new DirectMail object.

  10. Supply the StaticList object that you created in step 4 to the TestList and DestinationList properties of the DirectMail object.

Example

The following code example illustrates how to create a DirectMail object. The code first creates a new static mailing list, and then uses the static list in a new DirectMail object. The DefaultFormat property specifies the output format, and the SourceLocation property specifies the file that contains the body text of the mail message. After the DirectMail object is created, the code uses the DirectMail.Test method to send a test mailing to the static list e-mail members.

For personalized e-mail messages, the SourceLocation property must point to an .aspx file that provides content, and the IsPersonalized property of the DirectMail object must be set to true. For an example, see the C:\Microsoft Commerce Server 2007\SDK\Samples\Marketing\DirectMailPage.aspx page in the SDK directory.

using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Marketing;

namespace CommerceServerMarketingExamples
{
    public class CSMarketingExamples
    {
        public void NewDirectMail()
        {
            try
            {
                // Set the URL to the Web service.
                string marketingWebServiceUrl = @"https://localhost/MarketingWebService/MarketingWebService.asmx";
                // Create an instance of the Marketing System agent.
                MarketingServiceAgent ma = new MarketingServiceAgent(marketingWebServiceUrl);
                MarketingContext marketingSystem = MarketingContext.Create(ma);

                // Look up the Default Campaign ID.
                SearchClauseFactory CsearchClauseFactory = marketingSystem.Campaigns.GetSearchClauseFactory();
                SearchClause Csc = CsearchClauseFactory.CreateClause(ExplicitComparisonOperator.Equal, "Name", "Default Campaign");
                DataSet CdataSet = marketingSystem.Campaigns.Search(Csc);
                int CampaignId = (int)CdataSet.Tables["SearchResults"].Rows[0][0];

                // Create a new static list.
                StaticList sl = marketingSystem.MailingLists.NewStaticList();
                sl.Name = "TestList";
                sl.Description = "Description for TestList";
                sl.Save();
                sl.AddUser(new MailingListUser("testuser@fabrikam.com"));
                sl.AddUser(new MailingListUser("testuser2@adventureworks.com"));

                // Create a new direct mail.
                DirectMail dm = marketingSystem.CampaignItems.NewDirectMail(CampaignId);
                dm.Name = "Test Mailing";
                dm.Description = "Test a direct mail.";
                dm.From = "mailer@adventureworks.com";
                dm.ReplyTo = "mailer@adventureworks.com";
                dm.DefaultCultureCode = "en";
                dm.DefaultCharacterSet = "utf-8";
                dm.IgnoreGlobalOptOutList = true;
                // Create subject lines in different languages.
                dm.MultilingualSubject.Add(new LanguageString("english string", "en"));
                dm.MultilingualSubject.Add(new LanguageString("french string", "fr"));
                dm.DefaultFormat = MessageFormat.Text;
                dm.StartDate = DateTime.Now;
                dm.EndDate = DateTime.Now + TimeSpan.FromDays(14);
                dm.TestList = sl;
                dm.DestinationList = sl;
                dm.SourceLocation = "https://localhost/startersite/Message1.txt";
                dm.Save(false);
                dm.Test();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}\r\nMessage: {1} " + ex.GetType() + " " + ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine("\r\nInner Exception: {0}\r\nMessage: {1}" + ex.InnerException.GetType() + ex.InnerException.Message);
                Console.WriteLine("\n");
            }
        }
    }
}

See Also

Other Resources

Marketing Management Scenarios