eml ファイルを添付して ms グラフ API を使用して電子メールを送信すると、受信者側でエラーが発生する
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
Java の ms グラフ API (バージョン 6.4.0) を使用して、添付ファイル付きの電子メールを送信できます。ちなみに、eml ファイルを添付ファイルとして追加すると、このファイルは Gmail や Outlook 以外の他の電子メールにクライアントではプレビューできません。
添付ファイルの contentType を message/rfc822 に設定しましたが、受信したメッセージを表示すると、contentType が application/octet-stream であることがわかります。これは Gmail でファイルをプレビューできない問題だと思います。の代わりに、「添付メッセージのロード中にエラーが発生しました」というエラーメッセージがポップアップ表示されます。
もう 1 つ奇妙なのは、Gmail から自分にメールを転送すると、その転送されたメールで、添付された eml ファイルをプレビューとして開くことができることです。
添付ファイル付きの電子メールを送信するコードの重大な部分を抽出しました。
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.graph.models.*;
import com.microsoft.graph.serviceclient.GraphServiceClient;
import com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
public class GraphSendMail {
private static final String USER = "[email protected]";
public static void main(String[] args) {
var credential = new ClientSecretCredentialBuilder()
.clientId("clientId")
.tenantId("tenantId")
.clientSecret("clientSecret")
.build();
var client = new GraphServiceClient(credential, "https://graph.microsoft.com/.default");
var sendMailRequest = new SendMailPostRequestBody();
Message message = new Message();
Recipient to = new Recipient();
to.setEmailAddress(new EmailAddress());
to.getEmailAddress().setAddress("[email protected]");
message.setToRecipients(List.of(to));
var sender = new Recipient();
sender.setEmailAddress(new EmailAddress());
sender.getEmailAddress().setAddress(USER);
message.setSender(sender);
message.setFrom(sender);
message.setSubject("Test mail");
// Send HTML email
ItemBody body = new ItemBody();
body.setContentType(BodyType.Html);
body.setContent("<html><body><h1>Test body</h1></body></html>");
message.setBody(body);
String filePath = "C:/pathtofile/attached-mail.eml"; // Specify the path to your file
File file = new File(filePath);
byte[] fileBytes = readFileToByteArray(file);
fileBytes = Base64.getEncoder().encode(fileBytes);
FileAttachment attachment = new FileAttachment();
attachment.setName("attached-mail.eml");
attachment.setContentBytes(fileBytes);
attachment.setContentType("message/rfc822");
message.setAttachments(List.of(attachment));
sendMailRequest.setMessage(message);
client.users()
.byUserId(USER)
.sendMail()
.post(sendMailRequest);
System.out.println("Mail sent success");
}
public static byte[] readFileToByteArray(File file) {
FileInputStream fis = null;
byte[] byteArray = null;
try {
fis = new FileInputStream(file);
byteArray = new byte[(int) file.length()];
fis.read(byteArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return byteArray;
}
}
javax.mail と smtp サーバーでも同じことを試みましたが、正常に動作しました。 なお、smtp はこのプロジェクトのオプションではありません。 eml はディスクに保存できるので、ItemAttachment を使用する必要はないと思いますeml は最初から Outlook に含まれていない可能性があるため、参照する必要はありません。
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2609
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
-
浏览(166) 评论(0)