×
Community Blog A Comprehensive Guide to Using Alibaba Cloud DirectMail

A Comprehensive Guide to Using Alibaba Cloud DirectMail

In this article, we will provide a complete guide to using the Alibaba Cloud DirectMail service, including using JAVA SDK for DirectMail service to se.

By Sai Sarath Chandra, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This article provides a complete guide to the Alibaba Cloud DirectMail service. You will also find some information and experiences which you can't find in the documentation. We will discuss in depth about the following topics:

  1. How to Set Up DirectMail Server
  2. How to Use JAVA SDK for DirectMail Service to Send Emails
  3. How to Use SMTP Protocol via Java for using DirectMail Service

How to Set Up DirectMail Server

To set up DirectMail, all you need to do is follow these steps:

  1. Buy a domain name (If you don't have one already)
  2. Set up subdomain for the mail server
  3. Verifying mail server configuration
  4. Creating sender addresses
  5. Creating recipient lists
  6. Creating email templates
  7. Sending emails

Buying and Setting up a Domain Name

You can skip this if you already own a domain name. Hit the below URI to buy a domain name suitable for your business if you don't have one already: https://www.alibabacloud.com/domain

1

Once you have searched for a suitable domain name, you can choose to "Add to Cart" and checkout the cart.

2

What I like about Alibaba Cloud is that there are no hidden charges with the purchase. The domain name also comes along with privacy and security features like Anti-DDoS, direct configuration with DirectMail, monitoring and alarm facilities, and many more.

Once you complete the process (which is pretty straightforward), you should see your domain name on your Alibaba Cloud Domain Console.

3

For this complete tutorial we will use "actestdomain.xyz". Any domain name would do as well; you just need to make the appropriate changes where necessary.

Setting up Mail Server on the Subdomain

Navigate to Direct Mail from the Alibaba Cloud Console. Then choose to activate the service if prompted.

Once you are into the Direct Mail console, choose Email Domains:

4

As per Alibaba Cloud recommended practice, you always need to associate a subdomain when configuring the mails.

5

I have associated the "mail.actestdomain.xyz" subdomain as shown above. If you are new to the console, you need to add the Domain to the Direct Mail by clicking on the "New Domain"

6

Once you click ok you can see the domain in the list of "Email Domains". Click "Configure", to see a screen similar to below.

7

Now head to the Alibaba Cloud DNS Console and add the corresponding records.

  1. 2 TXT Records
  2. 1 MX Record
  3. 1 CNAME Record

Verification of Mail Server Configuration

After you have done the above-mentioned addition you need to verify the configuration by clicking verify button

8

Create a Sender Address

Sender address is the email address what the recipient see when they receive the mail.

Go to "Sender Address" and select "Create Sender Address".

9

Fill out the form to create an address.

10

Email Domains: Select the domain created at the initial step.

Account: The name of the email account without domain.

Reply-To Address: DirectMail cannot serve as an inbox, so you'll need some third-party mail to receive emails.

Mail Type: There are two options to choose from "Triggered Emails" & "Batch Emails". Choose based on your needs.

Once the email is created you can create or edit a "Return Address". You have to verify the return address before you receive mails to the Triggered/Batch Emails.

11

You also need to setup the SMTP password. This helps you to access the email from the custom program/code you create.

Creating Recipient Lists

12

Navigate to "Send Emails" > "Recipient Lists"

List Name: This is a unique identifier in the account. I recommended creating a name without spaces, as we will be using this name in the code/program further.

Alias Address: The primary email address to identify the list

Description: This is the description you give to the list for identification.

Once you have given the details you will have an entry similar to this

13

Click on the details to add the list of email addresses. You will have an option of uploading all the email addresses via a TXT file or a CSV file. Make sure you download the files from the templates as shown below.

14

If you would like to send the batch emails from the console, you need to use the "Email Tasks". However, before that, you need to have an "Email Template" defined.

Creating Email Templates

Navigate to "Email Templates" > "Create Email Template". You can create the template using the form

15

Template Name: This will be the name to uniquely identify the template.

Mail Subject: This is the subject of the mail.

Sender Name: The name you want to display when you send the mail.

Description of Variables: Will explain simply what and how you can use the variables mentioned in the mail.

Mail Body: This is the actual mail you will create to address the users. You can use the variables available to draft the mail. The below is the sample mail which uses all variables available.

Hi {UserName}
Welcome to the family, This is the personal data we hold about you.
Name : {Ge¬¬nder} {UserName}
DisplayName : {NickName}
Email ID : {EAddr}
Birthdata : {Birthday}
Mobile Phone : {Mobile}
Please review the data and let us know in case of any discrepancies. You can always manage the data in the console.
 
Regards,
Sai Sarath Chandra

The above variables mentioned in {} are variables that changes with the data we mentioned in the Recipient lists. Do remember that you have to submit the template for review. It will take 3 days at maximum for the review to complete.

Sending Emails

You can send Emails in two ways: by using the console or by code/program.

Sending Emails via Console

You can use "Email Tasks" to send the mails using the console.

Navigate to "Email Tasks"> "New Email Task"

16

Recipient Lists: This drop down menu consists of the list names of the Recipient lists.

Template Name: This is the template that we have created in the last step.

Sender Address: This is the address we have created at the initial step.

Sender Address Type: We can choose the type of addresses we want the recipient receive: Random Address or Sender Address.

Email Tags: These are the tags we created during the creation of Recipient Lists.

Via Code/Program

You can create application using the Java SDK or several other SDK's available for different languages for DirectMail. You can use the plain SMTP to protocol to trigger the emails from the console.

I have created a simple Maven project the below, called "pom.xml".

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.ssc.direstmail</groupId>
    <artifactId>restapi</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>sonatype-nexus-staging</id>
            <name>Sonatype Nexus Staging</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-dm</artifactId>
        <version>3.1.1</version>
    </dependency>
    </dependencies>

</project>

And these are the methods which you can use to access the SMTP & JAVA SDK.

SMTP

public static void useSMTP(){
    // Configure the mail environment properties
    final Properties props = new Properties();
    // Indicate that identity authentication is required for sending mails via SMTP
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", ALIDM_SMTP_HOST);
    props.put("mail.smtp.port", ALIDM_SMTP_PORT);
    // If SSL is used, remove the configuration of using Port 25, as follows:
    // props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    // props.put("mail.smtp.socketFactory.port", "465");
    // props.put("mail.smtp.port", "465");
    // The sender account
    props.put("mail.user", "admin@mail.actestdomain.xyz");
    // The password required for accessing the SMTP service.
    props.put("mail.password", "ADmin12345");
    // Construct the authentication information for SMTP identity authentication.
    Authenticator authenticator = new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            // Username and password
            String userName = props.getProperty("mail.user");
            String password = props.getProperty("mail.password");
            return new PasswordAuthentication(userName, password);
        }
    };
    // Create the mail session using the environmental attributes and authentication information.
    Session mailSession = Session.getInstance(props, authenticator);
    //mailSession.setDebug(true);
    // Create the mail message.
    MimeMessage message = new MimeMessage(mailSession);
    try {
        // Set the sender.
        InternetAddress from = new InternetAddress("admin@mail.actestdomain.xyz");
        message.setFrom(from);
        Address[] a = new Address[1];
        a[0] = new InternetAddress("saisarathchandrap@gmail.com");
        message.setReplyTo(a);
        // Set the recipient.
        InternetAddress to = new InternetAddress("saisarath151@gmail.com");
        message.setRecipient(MimeMessage.RecipientType.TO, to);
        // Set the mail subject.
        message.setSubject("Test mail");
        // Set the mail body content.
        message.setContent("Test HTML mail", "text/html;charset=UTF-8");
        // Send the mail.
        Transport.send(message);
    }
    catch (MessagingException e) {
        String err = e.getMessage();
        // Here to deal with the message content, format is fixed.
        System.out.println(err);
    }
}

JAVA SDK

public static void useMailSDK(){
    IClientProfile profile = DefaultProfile.getProfile("ap-southeast-2", "LTAIgrzCUEOF91on", "92YaOr0VqfwQJDBaLGUyFTSEqGYmpNv");
    // Other region except China east 1 (hangzhou), such as Singapore region, need to do the following treatment.
    try {
    DefaultProfile.addEndpoint("dm.ap-southeast-2.aliyuncs.com", "ap-southeast-2", "Dm",  "dm.ap-southeast-2.aliyuncs.com");
    } catch (ClientException e) {
    e.printStackTrace();
    }
    IAcsClient client = new DefaultAcsClient(profile);
    SingleSendMailRequest request = new SingleSendMailRequest();
    try {
        request.setVersion("2017-06-22");//  Other region except China east 1 (hangzhou), such as Singapore region, need to be specified for the 2017-06-22.
        request.setAccountName("admin@mail.actestdomain.xyz");
        request.setFromAlias("Admin");
        request.setAddressType(1);
        request.setTagName("MeetupIndia");
        request.setReplyToAddress(true);
        request.setToAddress("saisarath151@gmail.com");
        request.setSubject("Subject from Java Program");
        request.setHtmlBody("<h1>This is the test mail sent from the Java Code<h1>");
        SingleSendMailResponse httpResponse = client.getAcsResponse(request);
        System.out.println(httpResponse);
        System.out.println(httpResponse.getEnvId());
        System.out.println(httpResponse.getRequestId());
    } catch (ServerException e) {
        e.printStackTrace();
    }
    catch (ClientException e) {
        e.printStackTrace();
    }
}

Additional Thoughts

DirectMail is mainly used to send promotional emails, which falls under "Batch Emails" as well as to trigger emails when the user is signing up or when they forgot their account login data. DirectMail is not an exchange server; you can't use it as an "inbox" service. If you are looking for an organizational email server you probably won't need this.

Alibaba Cloud delivers messages pretty promptly. You can use HTML in the code to deliver the messages. This is a huge benefit as we can have the freedom to create good looking emails.

If you are new to Alibaba Cloud or want to know more about DirectMail, sign up for an account and try over 40 products for free worth up to $1200. Or visit the Alibaba Cloud Getting Started channel to learn more about Alibaba Cloud.

0 0 0
Share on

Alibaba Clouder

1,141 posts | 190 followers

You may also like

Comments