如何通过SDK方式发送带附件的邮件?

说明

附件邮件总大小不超过15MB,一次最多不超过100个附件。

15MB是指发信邮件实际总大小(包括邮件头,正文,附件),由于base64编码邮件代码会膨胀1.5倍以上,总大小非客户侧看到的大小,附件限制建议按照8MB来准备。若需要发送大附件,建议内容里加超链接的方式发送。

# -*- coding: utf-8 -*-
import sys
from typing import List
from typing import BinaryIO
from alibabacloud_dm20151123.client import Client as Dm20151123Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dm20151123 import models as dm_20151123_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_dm20151123 import models as main_models

attachments = []


class Sample:
    def __init__(self) -> None:
        pass

    @staticmethod
    def create_client() -> Dm20151123Client:
        """
        使用凭据初始化账号Client
        @return: Client
        @throws Exception
        """
        # 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378659.html。
        config = open_api_models.Config(
            access_key_id='xxxxxx',
            access_key_secret='xxxxxx'
        )
        # Endpoint 请参考 https://api.aliyun.com/product/Dm
        config.endpoint = f'dm.aliyuncs.com'
        config.region_id = f'cn-hangzhou'
        return Dm20151123Client(config)

    @staticmethod
    def main(
            args: List[str],
    ) -> None:
        client = Sample.create_client()
        single_sendmail_request = dm_20151123_models.SingleSendMailAdvanceRequest(
            account_name='sender@example.com',
            address_type='1',
            reply_to_address='true',
            to_address='recipient@example.com',
            subject='主题',
            html_body='邮件内容',
            attachments=getAttachments()
        )
        try:
            # 复制代码运行请自行打印 API 的返回值
            response = client.single_send_mail_advance(single_sendmail_request, util_models.RuntimeOptions())
            print(response)
        except Exception as error:
            # 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.data)
            # # 诊断地址
            # print(error.data.get("Recommend"))
            # UtilClient.assert_as_string(error.message)

def getAttachments() -> List[main_models.SingleSendMailAdvanceRequestAttachments]:
    attach = main_models.SingleSendMailAdvanceRequestAttachments("test1.txt", getFile(r"C:\Users\Downloads\111.txt"))
    attach1 = main_models.SingleSendMailAdvanceRequestAttachments("test2.txt", getFile(r"C:\Users\Downloads\222.txt"))
    attachments.append(attach)
    attachments.append(attach1)
    return attachments


def getFile(v_file) -> BinaryIO:
    f = open(v_file, 'rb')
    return f


if __name__ == '__main__':
    Sample.main(sys.argv[1:])
package org.example;

import com.aliyun.dm20151123.models.SingleSendMailAdvanceRequest;
import com.aliyun.dm20151123.models.SingleSendMailResponse;
import com.aliyun.tea.TeaException;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class Sample {
    /**
     * <b>description</b> :
     * <p>使用凭据初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.dm20151123.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378657.html。
//        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId("xxxxxx")
                .setAccessKeySecret("xxxxxx");
        // Endpoint 请参考 https://api.aliyun.com/product/Dm
        config.endpoint = "dm.aliyuncs.com";
        config.regionId = "cn-hangzhou";
        return new com.aliyun.dm20151123.Client(config);
    }
    private static final Gson gson = new Gson(); // 新增 Gson 实例
    public static void main(String[] args_) throws Exception {


        com.aliyun.dm20151123.Client client = Sample.createClient();
        SingleSendMailAdvanceRequest singleSendMailAdvanceRequest = new SingleSendMailAdvanceRequest();
        List<SingleSendMailAdvanceRequest.SingleSendMailAdvanceRequestAttachments> attachments = new ArrayList<>();
        SingleSendMailAdvanceRequest.SingleSendMailAdvanceRequestAttachments attachment1 = new SingleSendMailAdvanceRequest.SingleSendMailAdvanceRequestAttachments();
        attachment1.setAttachmentName("test1.txt");
        attachment1.setAttachmentUrlObject(new FileInputStream(new File("C:\\Users\\Downloads\\111.txt")));


        SingleSendMailAdvanceRequest.SingleSendMailAdvanceRequestAttachments attachment2 = new SingleSendMailAdvanceRequest.SingleSendMailAdvanceRequestAttachments();
        attachment2.setAttachmentName("test2.txt");
        attachment2.setAttachmentUrlObject(new FileInputStream(new File("C:\\Users\\Downloads\\222.txt")));
        attachments.add(attachment1);
        attachments.add(attachment2);
        singleSendMailAdvanceRequest.setAttachments(attachments);

        singleSendMailAdvanceRequest.setAccountName("sender@example.com");
        singleSendMailAdvanceRequest.setAddressType(1);
        singleSendMailAdvanceRequest.setReplyToAddress(true);
        singleSendMailAdvanceRequest.setToAddress("recipient@example.com");
        singleSendMailAdvanceRequest.setSubject("主题");
        singleSendMailAdvanceRequest.setHtmlBody("邮件内容");
        singleSendMailAdvanceRequest.setClickTrace("1");


        try {
            // 复制代码运行请自行打印 API 的返回值
            SingleSendMailResponse response =  client.singleSendMailAdvance(singleSendMailAdvanceRequest, new com.aliyun.teautil.models.RuntimeOptions());

            System.out.println(gson.toJson(response));
        } catch (TeaException error) {
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
}

<?php

// This file is auto-generated, don't edit it. Thanks.
namespace AlibabaCloud\SDK\Sample;

use AlibabaCloud\SDK\Dm\V20151123\Dm;
// use AlibabaCloud\Credentials\Credential;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dm\V20151123\Models\SingleSendMailAdvanceRequest;
use AlibabaCloud\SDK\Dm\V20151123\Models\SingleSendMailAdvanceRequest\attachments;
use GuzzleHttp\Psr7\Stream;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

require_once __DIR__ . '\\..\\..\\vendor\\autoload.php';

class Sample {

    /**
     * 使用凭据初始化账号Client
     * @return Dm Client
     */
    public static function createClient(){
        // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/311677.html。
        $config = new Config([
            "accessKeyId" => "xxxxxx",
            "accessKeySecret" => "xxxxxxx"
        ]);
        // Endpoint 请参考 https://api.aliyun.com/product/Dm
        $config->endpoint = "dm.aliyuncs.com";        
        $config->regionId = "cn-hangzhou";        
        return new Dm($config);
    }
    // public static function createClient(){
    //     // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/311677.html。
    //     $credential = new Credential();
    //     $config = new Config([
    //         "credential" => $credential
    //     ]);
    //     // Endpoint 请参考 https://api.aliyun.com/product/Dm
    //     $config->endpoint = "dm.aliyuncs.com";
    //     $config->regionId = "cn-hangzhou";
    //     return new Dm($config);
    // }
    /**
     * 获取文件流
     * @param string $filePath 文件路径
     * @return Stream
     */
    public static function getFile($filePath) {
        $fileResource = fopen($filePath, 'rb');
        $fileSize = filesize($filePath); // 获取文件大小
        var_dump("文件大小:");
        var_dump($fileSize);
        return new Stream($fileResource);
    }

    /**
     * 获取附件列表
     * @return array
     */
    public static function getAttachments() {
        $attachments = [];
        
        // 创建第一个附件
        $attachment1 = new attachments();
        $attachment1->attachmentName = "test1.txt";
        $attachment1->attachmentUrlObject = self::getFile("C:\\Users\\Downloads\\111.txt");
        $attachments[] = $attachment1;
        
        // 创建第二个附件
        $attachment2 = new attachments();
        $attachment2->attachmentName = "test2.txt";
        $attachment2->attachmentUrlObject = self::getFile("C:\\Users\\Downloads\\222.txt");
        $attachments[] = $attachment2;
        //打印$attachments;
        var_dump($attachments);
        return $attachments;
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient();
        
        // 创建邮件请求
        $singleSendMailRequest = new SingleSendMailAdvanceRequest([
            "accountName" => "sender@example.com",
            "addressType" => 1,
            "replyToAddress" => true,
            "subject" => "主题",
            "toAddress" => "recipient@example.com",            
            "attachments" => self::getAttachments(),
            "htmlBody" => "<h1>内容</h1>",
            "textBody" => "内容"
        ]);
        
        // // 创建运行时选项
        // $runtime = new RuntimeOptions([
        //     "ca" => "C:\\Users\\cacert.pem" // 指定本地 CA 证书路径
        // ]);
        $runtime = new RuntimeOptions();
        
        try {
            // 发送邮件
            $client->singleSendMailAdvance($singleSendMailRequest, $runtime);
            var_dump("发送成功");
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message
            var_dump($error->message);
            // 诊断地址
            var_dump($error->data["Recommend"]);
            Utils::assertAsString($error->message);
        }
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Sample::main(array_slice($argv, 1));
package main

import (
	"fmt"
	"os"

	openapiutil "github.com/alibabacloud-go/darabonba-openapi/v2/utils"
	dm "github.com/alibabacloud-go/dm-20151123/v2/client"
	"github.com/alibabacloud-go/tea/dara"
	"github.com/aliyun/credentials-go/credentials"
)

// Description:
//
// 使用凭据初始化账号Client
//
// @return Client
//
// @throws Exception
func CreateClient() (_result *dm.Client, _err error) {
	config := new(credentials.Config).
		SetType("access_key").
		SetAccessKeyId("xxxxxx").
		SetAccessKeySecret("xxxxxx")

	akCredential, err := credentials.NewCredential(config)
	if err != nil {
		return _result, err
	}

	openapiConfig := &openapiutil.Config{
		Credential: akCredential,
	}
	// Endpoint 请参考 https://api.aliyun.com/product/Dm
	openapiConfig.Endpoint = dara.String("dm.aliyuncs.com")
	_result, _err = dm.NewClient(openapiConfig)
	return _result, _err
}

func main() {
	err := _main(dara.StringSlice(os.Args[1:]))
	if err != nil {
		panic(err)
	}
}

func _main(args []*string) (_err error) {
	client, _err := CreateClient()
	if _err != nil {
		return _err
	}

	// 创建附件
	attachments, _err := getAttachments()
	if _err != nil {
		return _err
	}

	// 使用SingleSendMailAdvanceRequest而不是SingleSendMailRequest
	singleSendMailRequest := &dm.SingleSendMailAdvanceRequest{
		AccountName:    dara.String("sender@example.com"),
		AddressType:    dara.Int32(1),
		ReplyToAddress: dara.Bool(true),
		ToAddress:      dara.String("recipient@example.com"),
		Subject:        dara.String("主题"),
		HtmlBody:       dara.String("邮件内容"),
		Attachments:    attachments,
	}

	runtime := &dara.RuntimeOptions{}
	// 使用SingleSendMailAdvance方法而不是SingleSendMailWithOptions
	resp, _err := client.SingleSendMailAdvance(singleSendMailRequest, runtime)
	if _err != nil {
		fmt.Printf("[ERROR] %s\n", _err.Error())
		return _err
	}

	fmt.Printf("[LOG] %s\n", dara.Stringify(resp))
	return _err
}

// getAttachments 获取附件列表
func getAttachments() ([]*dm.SingleSendMailAdvanceRequestAttachments, error) {
	// 注意:这里需要根据实际文件路径进行修改
	attach1, err := createAttachmentFromFile("test1.txt", `C:\Users\Downloads\111.txt`)
	if err != nil {
		return nil, fmt.Errorf("创建附件test1.txt失败: %v", err)
	}

	attach2, err := createAttachmentFromFile("test2.txt", `C:\Users\Downloads\222.txt`)
	if err != nil {
		return nil, fmt.Errorf("创建附件test2.txt失败: %v", err)
	}

	return []*dm.SingleSendMailAdvanceRequestAttachments{attach1, attach2}, nil
}

// createAttachmentFromFile 从文件创建附件
func createAttachmentFromFile(name, filePath string) (*dm.SingleSendMailAdvanceRequestAttachments, error) {
	// 打开文件
	file, err := os.Open(filePath)
	if err != nil {
		return nil, fmt.Errorf("无法打开文件 %s: %v", filePath, err)
	}

	// 获取文件信息
	fileInfo, err := file.Stat()
	if err != nil {
		file.Close()
		return nil, fmt.Errorf("无法获取文件信息 %s: %v", filePath, err)
	}

	// 检查文件大小
	// if fileInfo.Size() > 15*1024*1024 {
	// 	file.Close()
	// 	return nil, fmt.Errorf("文件 %s 大小超过限制", filePath)
	// }

	// 创建附件对象,使用文件作为io.Reader
	attachment := &dm.SingleSendMailAdvanceRequestAttachments{
		AttachmentName:      dara.String(name),
		AttachmentUrlObject: file, // 直接使用文件作为io.Reader
	}

	fmt.Printf("成功创建附件 %s,大小: %d 字节\n", name, fileInfo.Size())

	// 注意:在实际应用中,你可能需要保持文件打开直到发送完成
	// 或者将文件内容读取到内存中以避免文件句柄问题

	return attachment, nil
}
说明

请前往openapi下载最新的SDK包,才会有附件相关的方法。

若使用VPC服务器,需要走内网连接的用户,请设置相关参数config.setEndpointType("internal");

调用说明(暂未提供所有开发语言示例,基本逻辑类似)

python为例:

基本逻辑是读取文件的二进制流,多个附件封装到列表,传入附件字段,再调用single_send_mail_advance接口发信,非原来的singleSendMailWithOptions

  • SingleSendMailAdvanceRequestAttachments

作用:定义一个邮件附件,包含附件的文件名和文件内容(二进制流)

  • SingleSendMailAdvanceRequest

作用:封装发送邮件所需的所有参数,包括发件人、收件人、主题、正文以及附件等信息。

  • single_send_mail_advance发送邮件

作用:使用 Dm20151123Client 实例调用该方法,发送一封带附件的。

效果展示

image

可以使用诊断分析工具查询request-id,判断传参是否正确。

image

报错提示:code: 400, The attachment url is invalid

请检查附件路径、区域是否传值正确,调用方法是否切换为支持附件的方法,如pythonsingle_send_mail_advance,而不是single_send_mail_with_options

config.endpoint = "dm.aliyuncs.com";

config.regionId = "cn-hangzhou";

说明

endpointregionid请替换成实际使用的区域。