目录

[原创]Yisenmail—-伊森邮箱使用方法高级篇之API接口使用

骚年
骚年
管理员
23
文章
0
粉丝
伊森邮件教程评论2阅读模式

本教程是关于伊森邮箱,也就是YisenMail准备制作,如果需要可以直接联系QQ:63448630

前面介绍了:

[原创]Yisenmail—-伊森邮箱使用方法高级篇之官方批量收件工具使用

[原创] Yisenmail—-伊森邮箱使用方法高级篇之使用第三方IMAP工具收件

[原创] Yisenmail—-伊森邮箱使用方法高级篇之1账号管理N个邮箱(适合批量管理收件)

[原创] Yisenmail—-伊森邮箱使用方法之基础篇

正文

今天来介绍的是伊森邮箱的另外一个高级用法----API接口代码使用

这部分需要一定编程知识,如果您只是普通用户,可以直接不用看这部分,这里针对是需要在自己程序中继承收件功能的用户所写的示例。

以下所有{JWT登录凭据},都替换为自己的jwt登录凭据即可。

Shell

cUrl方式:

curl --location --request GET 'https://apimail.yisen.cloud/api/mails?limit=10&offset=0' \
--header 'Authorization: Bearer {JWT登录凭据}'

wget方式:

wget --no-check-certificate --quiet \
   --method GET \
   --timeout=0 \
   --header 'Authorization: Bearer {JWT登录凭据}' \
    'https://apimail.yisen.cloud/api/mails?limit=10&offset=0'

JavaScript

Axion方式:

var axios = require('axios');

var config = {
   method: 'get',
   url: 'https://apimail.yisen.cloud/api/mails?limit=10&offset=0',
   headers: { 
      'Authorization': 'Bearer {JWT登录凭据}'
   }
};

axios(config)
.then(function (response) {
   console.log(JSON.stringify(response.data));
})
.catch(function (error) {
   console.log(error);
});

Request方式:

var request = require('request');
var options = {
   'method': 'GET',
   'url': 'https://apimail.yisen.cloud/api/mails?limit=10&offset=0',
   'headers': {
      'Authorization': 'Bearer {JWT登录凭据}'
   }
};
request(options, function (error, response) {
   if (error) throw new Error(error);
   console.log(response.body);
});

JAVA

OKHttp:

OkHttpClient client = new OkHttpClient().newBuilder()
   .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
   .url("https://apimail.yisen.cloud/api/mails?limit=10&offset=0")
   .method("GET", body)
   .addHeader("Authorization", "Bearer {JWT登录凭据}")
   .build();
Response response = client.newCall(request).execute();

Swift

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

var semaphore = DispatchSemaphore (value: 0)

var request = URLRequest(url: URL(string: "https://apimail.yisen.cloud/api/mails?limit=10&offset=0")!,timeoutInterval: Double.infinity)
request.addValue("Bearer {JWT登录凭据}", forHTTPHeaderField: "Authorization")

request.httpMethod = "GET"

let task = URLSession.shared.dataTask(with: request) { data, response, error in 
   guard let data = data else {
      print(String(describing: error))
      semaphore.signal()
      return
   }
   print(String(data: data, encoding: .utf8)!)
   semaphore.signal()
}

task.resume()
semaphore.wait()

Go

Native方式:

package main

import (
   "fmt"
   "net/http"
   "io/ioutil"
)

func main() {

   url := "https://apimail.yisen.cloud/api/mails?limit=10&offset=0"
   method := "GET"

   client := &http.Client {
   }
   req, err := http.NewRequest(method, url, nil)

   if err != nil {
      fmt.Println(err)
      return
   }
   req.Header.Add("Authorization", "Bearer {JWT登录凭据}")

   res, err := client.Do(req)
   if err != nil {
      fmt.Println(err)
      return
   }
   defer res.Body.Close()

   body, err := ioutil.ReadAll(res.Body)
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println(string(body))
}

PHP

cUrl方式:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL => 'https://apimail.yisen.cloud/api/mails?limit=10&offset=0',
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_ENCODING => '',
   CURLOPT_MAXREDIRS => 10,
   CURLOPT_TIMEOUT => 0,
   CURLOPT_FOLLOWLOCATION => true,
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
   CURLOPT_CUSTOMREQUEST => 'GET',
   CURLOPT_HTTPHEADER => array(
      'Authorization: Bearer {JWT登录凭据}'
   ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Http_Request2方式:

<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://apimail.yisen.cloud/api/mails?limit=10&offset=0');
$request->setMethod(HTTP_Request2::METHOD_GET);
$request->setConfig(array(
   'follow_redirects' => TRUE
));
$request->setHeader(array(
   'Authorization' => 'Bearer {JWT登录凭据}'
));
try {
   $response = $request->send();
   if ($response->getStatus() == 200) {
      echo $response->getBody();
   }
   else {
      echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
      $response->getReasonPhrase();
   }
}
catch(HTTP_Request2_Exception $e) {
   echo 'Error: ' . $e->getMessage();
}

python

Requests方式:

import requests

url = "https://apimail.yisen.cloud/api/mails?limit=10&offset=0"

payload={}
headers = {
   'Authorization': 'Bearer {JWT登录凭据}'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

Http

GET /api/mails?limit=10&offset=0 HTTP/1.1
Host: apimail.yisen.cloud
Authorization: Bearer {JWT登录凭据}

C#

var client = new RestClient("https://apimail.yisen.cloud/api/mails?limit=10&offset=0");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer {JWT登录凭据}");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Objective-C

#import <Foundation/Foundation.h>

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://apimail.yisen.cloud/api/mails?limit=10&offset=0"]
   cachePolicy:NSURLRequestUseProtocolCachePolicy
   timeoutInterval:10.0];
NSDictionary *headers = @{
   @"Authorization": @"Bearer {JWT登录凭据}"
};

[request setAllHTTPHeaderFields:headers];

[request setHTTPMethod:@"GET"];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
   if (error) {
      NSLog(@"%@", error);
      dispatch_semaphore_signal(sema);
   } else {
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
      NSError *parseError = nil;
      NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
      NSLog(@"%@",responseDictionary);
      dispatch_semaphore_signal(sema);
   }
}];
[dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

Ruby

require "uri"
require "net/http"

url = URI("https://apimail.yisen.cloud/api/mails?limit=10&offset=0")

https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true

request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer {JWT登录凭据}"

response = https.request(request)
puts response.read_body

统一返回响应:

{
    "results": [
        {
            "id": 0,
            "message_id": "<{消息ID}>",
            "source": "{发件地址}",
            "address": "{收件地址}",
            "raw": "{邮件内容}",
            "created_at": "{时间}"
        }
    ],
    "count": 1
}

以上则是各个语言版本的取件API示例,和返回响应内容,所有代码需要在您自己的程序中进行相应的调整。还有任何以上没有包含的语言需要帮助,可以在留言区告诉我,我来尽量抽时间补充完整。

这小玩意还挺有意思?!是吧!

 
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证