Microsoft Azure Open Source Development Support Team Blog

Support for Open Source Technologies on Microsoft Azure App Service

Send email on App Service using Office 365 (O365)

For PHP –

This is a Proof of Concept to send email using the PHP Mailer library and Office 365.

From the PHPMailer GitHub site, follow the “Minimal installation” section and download class.phpmailer.php and class.smtp.php.

Copy-paste the code from “A Simple Example” section or use the following:

require 'class.phpmailer.php';
require 'class.smtp.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to

$mail->setFrom('', 'Name');
$mail->addAddress(''); // Add a recipient

//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';

I did the above using kudu, so my folder looked like this:


And I ran the script from the command-line “php mail.php”.


Here’s the message that was sent:


For Java –

Please check the sample below and modify it according to your application –

import org.apache.commons.mail.DefaultAuthenticator;

import org.apache.commons.mail.EmailException;

import org.apache.commons.mail.HtmlEmail;

public class JavaMailer {   

    public static void main(String[] args) {
         HtmlEmail htmlemail = new HtmlEmail();       

         htmlemail.setAuthenticator(new DefaultAuthenticator(“user”, “password”));
         try {
             htmlemail.setSubject(“Test Subject”);           
             htmlemail.setMsg(“This is a test <b> html email </b> … :-)” );
             System.out.println(“Your email is sent successfully”);
         } catch (EmailException e) {


Make sure you add the commons-email-1.4.jar and javax.mail.jar in your classpath.

You can run this from command line like –

java –cp ./commons-email-1.4.jar;./javax.mail.jar JavaMailer