当前位置: 首页 > 知识库问答 >
问题:

蓝色发送:将带有附件的邮件发送到sendinblue

长孙正卿
2023-03-14

我正在尝试使用sendTransacEmail()方法将附件发送到sendinblue(版本3),下面是我发送事务性电子邮件的代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;


import sendinblue.ApiClient;
import sendinblue.ApiException;
import sendinblue.Configuration;
import sendinblue.auth.ApiKeyAuth;
import sibApi.SmtpApi;
import sibModel.CreateSmtpEmail;
import sibModel.SendSmtpEmail;
import sibModel.SendSmtpEmailAttachment;
import sibModel.SendSmtpEmailBcc;
import sibModel.SendSmtpEmailReplyTo;
import sibModel.SendSmtpEmailSender;
import sibModel.SendSmtpEmailTo;

/**
 * Handler for requests to Lambda function.
 */
public class App implements RequestHandler<Object, Object> {

    public Object handleRequest(final Object input, final Context context) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("X-Custom-Header", "application/json");
        try {
            final String pageContents = this.getPageContents("https://checkip.amazonaws.com");
            String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);

            String access_key = "#########";

            ApiClient defaultClient = Configuration.getDefaultApiClient();
            ApiKeyAuth apiKey = (ApiKeyAuth) defaultClient.getAuthentication("api-key");
            apiKey.setApiKey(access_key);

            SmtpApi apiInstance = new SmtpApi();
            Long templateId = 1L; // Long | Id of the template

            Map<String, String> params = new HashMap<String, String>();
            params.put("TITLE", "Testing  ,His name");
            params.put("SUBJECT", "Testing This is my subject");

            SendSmtpEmailBcc sendSmtpEmailBcc = new SendSmtpEmailBcc();

            SendSmtpEmailReplyTo sendSmtpEmailReplyTo = new SendSmtpEmailReplyTo();
            sendSmtpEmailReplyTo.setEmail("Testing.123@abcdxyz.com");
            sendSmtpEmailReplyTo.setName("Test-EMailReplyTo");

            SendSmtpEmailTo sendSmtpEmailTo = new SendSmtpEmailTo();
            sendSmtpEmailTo.setEmail("Testing.123@abcdxyz.com");
            sendSmtpEmailTo.setName("sendSmtpEmailTo");

            SendSmtpEmailSender sendSmtpEmailSender = new SendSmtpEmailSender();
            sendSmtpEmailSender.setEmail("Testing.123@abcdxyz.com");
            sendSmtpEmailSender.setName("Test-Sender");


            byte[] bFile = readBytesFromFile("/home/Testing123/Downloads/Testing123.jpg");
            byte[] bArr =Base64.getEncoder().encodeToString(bFile).getBytes();

            SendSmtpEmail sendSmtpEmail = new SendSmtpEmail();

            SendSmtpEmailAttachment sendSmtpEmailAttachment1 = new SendSmtpEmailAttachment();
            sendSmtpEmailAttachment1.setUrl("/home/Testing123/Downloads/Testing123.jpg");
            sendSmtpEmailAttachment1.setName("My attachment 1");
            sendSmtpEmailAttachment1.setContent(bArr);

            sendSmtpEmail.sender(sendSmtpEmailSender);
            sendSmtpEmail.to(Arrays.asList(sendSmtpEmailTo));
            sendSmtpEmail.replyTo(sendSmtpEmailReplyTo);
            sendSmtpEmail.params(params);
            sendSmtpEmail.templateId(1L);

            //sendSmtpEmail.setAttachment(Arrays.asList(sendSmtpEmailAttachment1));



            System.out.println(sendSmtpEmail);

            CreateSmtpEmail result = apiInstance.sendTransacEmail(sendSmtpEmail);
            System.out.println(result);


            return new GatewayResponse(output, headers, 200);
        } catch (ApiException e) {
            System.err.println("Exception when calling SmtpApi#sendTransacEmail");

            e.printStackTrace();
            return new GatewayResponse("{}", headers, 500);
        } catch (IOException e) {
            return new GatewayResponse("{}", headers, 500);
        } catch (Exception e) {
            e.printStackTrace();
            return new GatewayResponse("{}", headers, 500);
        }
    }

    private String getPageContents(String address) throws IOException {
        URL url = new URL(address);
        try (BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()))) {
            return br.lines().collect(Collectors.joining(System.lineSeparator()));
        }
    }

}

这行给我错误请求,

sendSmtpEmail.setAttachment(Arrays.asList(sendSmtpEmailAttachment1));

在给定的API中https://developers.sendinblue.com/reference#sendtransacemail附件是一个对象数组,其中URL、名称

如何用附件发送电子邮件,1.使用字节数组内容?2.使用网址?

共有1个答案

艾志尚
2023-03-14

问题是这行sendSmtpEmailAttachment1。setName(“我的附件1”);

它应该是sendSmtpEmail Attachment1.setName("Testing123.jpg");或任何允许的有效扩展。

 类似资料:
  • 问题内容: 我遇到了一个问题:直到今天,我使用包含以下内容的标头使用PHP发送HTML邮件 现在,我添加了添加附件的功能。为此,我不得不将此行更改为 现在,使用,邮件的其余部分(即普通文本)将显示为文本/纯文本。我怎么能知道附件有效并且邮件文本仍然是HTML? 问题答案: 要发送带有附件的电子邮件,我们需要使用multipart / mixed MIME类型,该类型指定混合类型将包含在电子邮件中。

  • 问题内容: 是否有用于NodeJS的库,用于发送带有附件的邮件? 问题答案: 是的,这非常简单,我使用nodemailer: 然后阅读文件并发送电子邮件:

  • 问题内容: 我似乎找不到我编写的应该发送带有附件的电子邮件的php函数的问题。我已经为此苦苦挣扎了一段时间了。 编辑 问题是邮件的消息与文件混合在一起并作为附件发送。 问题答案: Artefacto让我更加关注输出,并且找到了解决方法:

  • 问题内容: 我正在尝试在带有附件的codeigniter上发送电子邮件。 我总是成功收到电子邮件。但是,我从未收到带有附件的文件。以下是代码,非常感谢所有评论。 问题答案: $ this- >电子邮件-> attach() 使您可以发送附件。将文件路径/名称放在第一个参数中。注意:使用文件路径,而不是URL。对于多个附件,请多次使用该功能。例如:

  • 本文向大家介绍Codeigniter实现发送带附件的邮件,包括了Codeigniter实现发送带附件的邮件的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Codeigniter实现发送带附件的邮件的方法。分享给大家供大家参考。具体分析如下: attach() 方法允许你的发邮件时带上附件,下面是演示代码 希望本文所述对大家基于Codeigniter的php程序设计有所帮助。

  • 尝试发送带有pdf附件的电子邮件,尝试使用swickmailer,但没有成功,此代码使用zip但不使用PDF:( 邮件被发送罚款,我得到的邮件:但附件是不存在的,在meial有所有的bas64编码在电子邮件像: onatatent-Type: Application/octet-stream; name="media.pdf"Content-transver-Encode: base 64 Con