Send a mail...

As a more complex example of interacting with a server, here is an example of how to send e-mail using an open relay. You can send e-mail

Select and build the project

In the examples for your board, you'll find a Networking Projects solution, and within that a Send E-mail project. Double-click that project to make it active and press F7 to build.

SMTP server

You need to configure the SMTP server for this example to work. In the example you will need to configure SMTP_SERVER with the domain name or dotted-decimal IP address of your SMTP server, and also set USER_EMAIL_ADDRESS to the e-mail address of the recipient.

The code

There is nothing new in this example, it's just a little longer than retrieving a web page in the previous example.

/* Copyright (c) 2004-2013 Rowley Associates Limited.
*/

#include <string.h>
#include "example_support.h"
#include "libnet/ctl_net_api.h"
#include "libnet/extras/ctl_smtp_client.h"
#include "libplatform/platform.h"
#include "libplatform/platform_network.h"

// TODO: Example SMTP server address.  Replace with yours, either
// dotted-decimal or DNS name.
#define SMTP_SERVER \
  "your.mailserver.here"

// TODO: Example e-mail delivery address.  Replace with yours.
#define USER_EMAIL_ADDRESS  \
  "somebody@home.com"

// Resolved SMTP server.
static CTL_NET_IPv4_ADDR_t smtp_server_addr;

int
main(void)
{
  CTL_STATUS_t stat;
  char dot_ipaddr[16];

  // Initialize platform.
  platform_initialize();
  example_initialize();

  // Start networking, wait for an IP address.
  example_check_status(example_bring_up_full_networking());
  example_check_status(example_await_assigned_ip_address());

  // Wait 5s to see if we can resolve our mail server.  If you
  // use a dotted-decimal IPv4 address, there is no name lookup
  // and this completes immediately.
  printf("DNS: Resolving %s, maximum wait for DNS reply is 5 seconds.\n",
         SMTP_SERVER);
  example_check_status(ctl_dns_get_host_by_name(SMTP_SERVER,
                                                &smtp_server_addr,
                                                5000));
  printf("DNS: Resolved %s to %s\n",
        SMTP_SERVER,
        ctl_ip_sprint_addr(dot_ipaddr, smtp_server_addr));

  // Attempt to send some mail.
  stat = ctl_smtp_client_send_mail(smtp_server_addr,
                                   USER_EMAIL_ADDRESS,
                                   "crossworks@rowley.co.uk",  // fake
                                   "Hello from the CrossWorks TCP/IP Library!",
                                   0,
                                   "Hello!\n\nThis is the CrossWorks TCP/IP Library"
                                     "sending an e-mail to you.\n\n"
                                     "Regards,\n\n-- The CrossWorks Team.");

  // Say whether it worked.
  if (stat < CTL_NO_ERROR)
    example_terminate("SMTP: Didn't send that e-mail correctly.  Sorry.");
  else
    printf("SMTP: E-mail sent OK!\n");

  // Done.
  return example_finish();
}