gpt4 book ai didi

java - 发送蓝色 : sending mail with attachement to sendinblue

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:26 27 4
gpt4 key购买 nike

我正在尝试使用 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 中Attachment 是一个对象数组,其中 URL 、 name 和 content 都是可选的。

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

最佳答案

问题出在这一行 sendSmtpEmailAttachment1.setName("我的附件 1");

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

关于java - 发送蓝色 : sending mail with attachement to sendinblue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56055320/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com