MSDN Library
İçindekiler tablosunu daralt
İçindekiler tablosunu genişlet
Bu içerik dilinizde bulunmamaktadır ancak İngilizce sürümüne buradan bakabilirsiniz.

How to Send Email Using SendGrid from Java in an Azure Deployment

Updated: August 28, 2015

The following example shows you how you can use SendGrid to send emails from a web page hosted in Azure. The resulting application will prompt the user for email values, as shown in the following screen shot.

Email form

The resulting email will look similar to the following screen shot.

Email message

You’ll need to do the following to use the code in this topic:

  1. Obtain the javax.mail JARs, for example from

  2. Add the JARs to your Java build path.

  3. If you are using Eclipse to create this Java application, you can include the SendGrid libraries in your application deployment file (WAR) using Eclipse’s deployment assembly feature. If you are not using Eclipse to create this Java application, ensure the libraries are included within the same Azure role as your Java application, and added to the class path of your application.

You must also have your own SendGrid username and password, to be able to send the email. To get started with SendGrid, evaluate pricing and sign up at Azure customers receive a special offer of 25,000 free emails per month from SendGrid. To sign-up for this offer, or get more information, please visit For information about additional services provided by SendGrid, see

Additionally, familiarity with the information at Creating a Hello World Application for Azure in Eclipse, or with other techniques for hosting Java applications in Azure if you are not using Eclipse, is highly recommended.

The following code shows how to create a web form to retrieve user data for sending email. For purposes of this content, the JSP file is named emailform.jsp.

<%@ page language="java"
   contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Email form</title>
<p>Fill in all fields and click <b>Send this email</b>.</p>
<br />
<form action="sendemail.jsp" method="post">
         <td><input type="text" size="50" name="emailTo"> </td>
         <td><input type="text" size="50" name="emailFrom"> </td>
         <td><input type="text" size="100" name="emailSubject" value="My email subject"> </td>
         <td><input type="text" size="400" name="emailText" value="Hello,<p></p>This is my message.</p>Thank you." /> </td>
         <td>SendGrid user name:</td>
         <td><input type="text" name="sendGridUser"> </td>
         <td>SendGrid password:</td>
         <td><input type="password" name="sendGridPassword"> </td>
         <td colspan="2"><input type="submit" value="Send this email"> </td>
<br />

The following code, which is called when you complete the form in emailform.jsp, creates the email message and sends it. For purposes of this content, the JSP file is named sendemail.jsp.

<%@ page language="java"
   contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1" import="javax.activation.*, javax.mail.*, javax.mail.internet.*, java.util.Date, java.util.Properties" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Email processing happens here</title>
<p><b>This is my send mail page.</b></p>
   final String sendGridUser = request.getParameter("sendGridUser");
   final String sendGridPassword = request.getParameter("sendGridPassword");
   class SMTPAuthenticator extends Authenticator
      public PasswordAuthentication getPasswordAuthentication()
         String username = sendGridUser;
         String password = sendGridPassword;
         return new PasswordAuthentication(username, password);   
      // The SendGrid SMTP server.
      String SMTP_HOST_NAME = "";

      Properties properties;
      properties = new Properties();
      // Specify SMTP values.
      properties.put("mail.transport.protocol", "smtp");
      properties.put("", SMTP_HOST_NAME);
      properties.put("mail.smtp.port", 587);
      properties.put("mail.smtp.auth", "true");
      // Display the email fields entered by the user. 
      out.println("Value entered for email Subject: " + request.getParameter("emailSubject") + "<br/>");        
      out.println("Value entered for email      To: " + request.getParameter("emailTo") + "<br/>");
      out.println("Value entered for email    From: " + request.getParameter("emailFrom") + "<br/>");
      out.println("Value entered for email    Text: " + "<br/>" + request.getParameter("emailText") + "<br/>");

      // Create the authenticator object.
      Authenticator authenticator = new SMTPAuthenticator();
      // Create the mail session object.
      Session mailSession;
      mailSession = Session.getDefaultInstance(properties, authenticator);
      // Display debug information to stdout, useful when using the
      // compute emulator during development.

      // Create the message and message part objects.
      MimeMessage message;
      Multipart multipart;
      MimeBodyPart messagePart; 
      message = new MimeMessage(mailSession);
      multipart = new MimeMultipart("alternative");
      messagePart = new MimeBodyPart();
      messagePart.setContent(request.getParameter("emailText"), "text/html");

      // Specify the email To, From, Subject and Content. 
      message.setFrom(new InternetAddress(request.getParameter("emailFrom")));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("emailTo")));
      // Uncomment the following if you want to add a footer.
      // message.addHeader("X-SMTPAPI", "{\"filters\": {\"footer\": {\"settings\": {\"enable\":1,\"text/html\": \"<html>This is my <b>email footer</b>.</html>\"}}}}");

      // Uncomment the following if you want to enable click tracking.
      // message.addHeader("X-SMTPAPI", "{\"filters\": {\"clicktrack\": {\"settings\": {\"enable\":1}}}}");
      Transport transport;
      transport = mailSession.getTransport();
      // Connect the transport object.
      // Send the message.
      transport.sendMessage(message,  message.getRecipients(Message.RecipientType.TO));
      // Close the connection.
      out.println("<p>Email processing completed.</p>");
   catch (Exception e)
      out.println("<p>Exception encountered: " + 
         e.getMessage() + "</p>");

In addition to sending the email, emailform.jsp provides a result for the user; an example is the following screen shot:

Send mail result

Deploy your application to the compute emulator and within a browser run emailform.jsp, enter values in the form, click Send this email, and then see results in sendemail.jsp.

This code was provided to show you how to use SendGrid in Java on Azure. Before deploying to Azure in production, you may want to add more error handling or other features. For example:

  • You could use Azure storage blobs or SQL Azure to store email addresses and email messages, instead of using a web form.

  • You could use RoleEnvironment.getConfigurationSettings to retrieve the SendGrid username and password from your deployment’s configuration settings, instead of using the web form to retrieve those values. For information about the RoleEnvironment class, see Using the Azure Service Runtime Library in JSP and the Azure Service Runtime package documentation at

See Also

© 2015 Microsoft