Code Examples

Here are the examples of sending emails with API V2 in different languages.

Python

send

send with xsmtpapi

send with attachment

send with template

send with template && address list

SMTP

Java

WEBAPI_4.4 code example is dependent on httpclient4.4

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
</dependency>

WEBAPI_4.4 code example is dependent on httpclient4.4

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.4</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
</dependency>

httpclient httpmime org.json

WEBAPI_4.2 code example

WEBAPI_4.4 code example

SMTP_code example SMTP_code example

SMTP_code example SMTP_code example 2

PHP

general delivery

general delivery _curl

general delivery &&attachment

template delivery

template delivery &&address list

SMTP code is dependent on email delivery module. You need to install pear first.

pear installation steps:
Download installation package: curl -o go-pear.php  http://pear.php.net/go-pear.phar
Install php go-pear.php
Install dependent libraries with pear:
pear install Mail
pear install Mail_Mime
pear install Net_SMTP

SMTP_code example 1 (quickly send,messageId is not required)

SMTP_code example 2 (messageId is required)

1.Find Mail/smyp.php file in php library
2.Change the returned value of “send” function in line 329. As shown below, change “return true” to “return $args”.
And then you can call following codes to send emails, and obtain messageId.

pic

Ruby

rest_client is required for dependent code

gem install rest_client

general delivery

general delivery&&attachment

template delivery

template delivery&&address list

SMTP_code example 1 (quickly send,messageId is not required)

SMTP_code example 2 (messageId is required)

SMTP delivery is dependent on net/smtp.rb module. As module doesn’t return sever information by default after delivery, to obtain returned messageId, do the follows: First, find net/smtp.rb file in ruby library, and add a return value to the function data in line 915, details as below

pic

Then, add a returned value to “send_message” in line 660. Details as below

pic

Perl

Template delivery is dependent on json package.

Download at
https://metacpan.org/pod/JSON

general delivery

general delivery &&attachment

template delivery

template delivery &&address list

SMTP delivery

CSharp

general delivery

general delivery &&attachment

template delivery

template delivery &&attachment

SMTP delivery

Go

general delivery

attachment delivery

template delivery

Node.js

general delivery

attachment delivery

xsmtpapi delivery

SMTP delivery