Have you ever wondered how an email message is programmatically sent out from an application? Here I will show three different ways that sent email can be coded in different software platforms.
The first programming snippet comes from a Windows Phone mobile app. It’s actually quite simple. This is for Windows Phone 8 and the C# Sharp coding. An email account needs to first be set up on the phone.
- view plainprint?
- // create email object.
- EmailComposeTask emailComposeTask = new EmailComposeTask();
- // add a subject.
- emailComposeTask.Subject = "test subject";
- // add a message.
- emailComposeTask.Body = "test message";
- // add a recipient.
- emailComposeTask.To = "[email protected]";
- // add a carbon copy recipient.
- emailComposeTask.Cc = "[email protected]";
- // add a blind carbon copy recipient.
- emailComposeTask.Bcc = "[email protected]";
- // bring up the email interface.
- emailComposeTask.Show();
- <?php
- session_start();
- // include this php file to process the ‘captcha’ code to verify the
- // sender is human, not a robot.
- include("simple-php-captcha.php");
- // initialize php error variables to check for valid fields of information.
- $flagvar = 0;
- $flagvar1 = 0;
- $flagvar2 = 0;
- $flagvar3 = 0;
- $flagvarx = 0;
- // assign the auto generated ‘captcha’ code to a
- // php session variable used to display it to the email sender.
- $_SESSION['captcha'] = captcha();
- // enter here after the user clicks the ‘Submit’ button
- // to send out the email.
- if ($_POST['submit_email']) {
- // check the sender’s email address to make sure it is valid and set error variables.
- if(strlen($_POST['EMAIL']) <= 0) {
- $flagvar1 = 1;
- $flagvar = 1;
- } else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $_POST['EMAIL'])) {
- $flagvar1 = 1;
- $flagvar = 1;
- }
- // make sure there is something in the subject line and set error variables.
- if ( emptyempty($_POST['SUBJECT']) ) {
- $flagvar2 = 1;
- $flagvar = 1;
- }
- // make sure there is something in the message body and set error variables.
- if ( emptyempty($_POST['MESSAGEBOARD']) ) {
- $flagvar3 = 1;
- $flagvar = 1;
- }
- // ‘captcha’ codes match, then proceed.
- if ( $_REQUEST['skip_CaptchaCode'] === $_SESSION['firstcaptcha'] && $flagvar == 0 ) {
- // take out all html and php tags from the message body.
- $text = strip_tags($_POST['MESSAGEBOARD']);
- // concatenate two line feeds followed by the text “Email Sender”
- // followed by the sender’s email address.
- $text = $text."\n\nEmail Sender: ".$_POST['EMAIL'];
- // set the headers to nothing.
- $headers = "";
- // use the php mail function to send the email to my email address so
- // I can receive inquiries from prospective customers.
- mail('[email protected]', $_POST['SUBJECT'], $text, $headers);
- // display my web page to notify the sender that the submitted email has
- // been successfully sent to me.
- header("Location: http://www.analyzohiosoftware.com/email-verify.html");
- }
- // ‘captcha’ codes do not match, so set an error flag.
- if ( $_REQUEST['skip_CaptchaCode'] != $_SESSION['firstcaptcha'] ) {
- $flagvarx = 1;
- }
- }
- ?>
- method pushButton(var eventInfo Event)
- var
- ;// declare variables and objects.
- EmailCounter Smallint
- tblVar Table
- tc Tcursor
- m MAIL
- emailvar String
- endVar
- ;// change cursor to hour glass.
- setMouseShape(MouseWait,TRUE)
- ;// open a Tcursor to the data table containing the list of
- ;// email addresses to blast to.
- if tc.open(":PDOXDATA:EmailBlast.db") then
- ;// log onto the mail system.
- m.logon("MyPassword", "MyMailProfile")
- ;// start edit mode
- tc.edit()
- ;// move Tcursor to the first record in the data table.
- tc.home()
- ;// initialize the counter for the number of emails sent out.
- EmailCounter = 0
- ;// begin the “while loop” for traversing the data table to send out
- ;// emails for each address.
- while NOT tc.eot()
- ;// if the record isn’t marked as already processed,
- ;// go ahead and construct the email.
- if (tc."Finished" <> "y") then
- ;// add email address.
- m.addAddress(tc."E-mail")
- ;// add subject line.
- m.setSubject("An Email Blast")
- ;// add email message.
- m.setMessage("Here is an email blast we are sending out…success is expected…")
- ;// add attachment.
- m.addAttachment("c:\\document_folder\\sample.doc")
- ;// send email.
- m.send()
- ;// clear away addresses and attachments.
- m.emptyAddresses()
- m.emptyAttachments()
- ;// mark the just sent email as being processed in the data table.
- tc."Finished" = "y"
- ;// increment the sent email counter by 1.
- EmailCounter = EmailCounter + 1
- ;// end of if-then logic for unsent emails.
- endif
- ;// move to the next sequential record in the data table.
- tc.nextRecord()
- ;// temporarily suspend processing if 25 emails have been blasted out.
- ;// reset the “EmailCounter” variable to 0 and then display message to console.
- if (EmailCounter > 25) then
- EmailCounter = 0
- msgStop("Message", "Click to continue after these 25 emails went out...")
- endif
- ;// loop around again to process the next sequential email in the data table.
- endWhile
- ;// end edit mode
- tc.endEdit()
- ;// close a Tcursor to the data table containing the list of email addresses to blast to.
- tc.close()
- ;// log off the mail system.
- m.logoff()
- ;// end of if-then logic for opening a Tcursor to
- ;// ":PDOXDATA:EmailBlast.db" data table.
- endif
- ;// change cursor back to mouse pointer.
- setMouseShape(MouseArrow,TRUE)
- EndMethod

Join the conversation! Your thoughts help the community grow.