代码示例

以下为各种语言使用 API V2 发送邮件的示例.

Python

普通发送

普通发送 xsmtpapi

附件发送

模板发送

模板发送&&地址列表

SMTP发送

Java

依赖 WEBAPI_4.2代码示例需要依赖如下jar包 httpclient4.2

<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代码示例需要依赖如下jar包 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代码示例

WEBAPI_4.4代码示例

SMTP_代码示例(需要获取messageId

SMTP_代码示例2(需要获取messageId

WEBHOOK解析_代码示例

PHP

普通发送

普通发送_curl

普通发送&&附件

模板发送

模板发送&&地址列表

SMTP 代码依赖邮件发送模块,首先需安装pear.

pear安装步骤:
下载安装包: curl -o go-pear.php  http://pear.php.net/go-pear.phar
安装: php go-pear.php
然后, 通过pear来安装依赖库:
pear install Mail 
pear install Mail_Mime
pear install Net_SMTP

SMTP_代码示例1 (快速发送,不需要获取messageId

SMTP_代码示例2 (需要获取messageId

1. php的lib库中, 找到Mail/smtp.php文件
2. 将 329 行 `send` 函数的返回值做修改. 如下图所示将 `return true` 改为 `return $args`.
接下来您就可以调用以下的代码进行邮件的发送, 并获取messageId了.

pic

Ruby

依赖 代码需要安装rest-client

gem install 'rest-client'

普通发送

普通发送&&附件

模板发送

模板发送&&地址列表

SMTP_代码示例1 (快速发送,不需要获取messageId

SMTP_代码示例2 (需要获取messageId

SMTP发送依赖net/smtp.rb模块, 由于模块默认在发送成功时,不返回服务器的信息. 
因此如果需要获得服务器返回的messageId,需要做如下操作. 首先在您的ruby的lib
库中, 找到net/smtp.rb文件, 在915行data函数中加一个返回值,操作如下

pic

接着在660行send_messages函数中增加一个返回值,修改如下

pic

Perl

依赖 模板发送需要依赖json包

从CPAN下载安装
https://metacpan.org/pod/JSON

普通发送

普通发送&&附件

模板发送

模板发送&&地址列表

SMTP发送

CSharp

普通发送

普通发送&&附件

模板发送

模板发送&&附件

SMTP发送

Go

普通发送

附件发送

模板发送

SMTP发送

Nodejs

package.json

{
  "name": "1.node-demo",
  "version": "1.0.0",
  "description": "",
  "main": "1.sendEmail.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.1.3",
    "form-data": "^4.0.0",
    "nodemailer": "^6.8.0"
  }
}

普通发送

附件发送

xsmtpapi发送

SMTP发送