我正在尝试使用 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()));
}
}
}
这行提示我Bad Request错误,
sendSmtpEmail.setAttachment(Arrays.asList(sendSmtpEmailAttachment1));
在给定的API https://developers.sendinblue.com/reference#sendtransacemail中附件是对象的数组,其中URL,名称和内容都是可选的。
如何发送带有附件的电子邮件, 1.使用字节数组的内容 2.使用网址?
答案 0 :(得分:0)
此行存在问题sendSmtpEmailAttachment1.setName(“我的附件1”);
应该是sendSmtpEmailAttachment1.setName(“ Testing123.jpg”);或任何允许的有效扩展名。