java 通过 microsoft graph 调用outlook(三)
这次会添加一个Reply接口,
并且使用6.10.0版本
直接上代码
一, POM
<!-- office 365 --> <dependency> <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph</artifactId> <version>6.10.0</version> <!--x-release-please-end--> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.11.0</version> </dependency> <!-- office 365 -->
二,Service / impl
主要讲一下Reply方法,Reply本质是添加评论,我们所有回复邮件,可以看到上下文时,实际都是添加了评论而已。(对比了outlook是一样的)
而Reply添加的附件,只有最后一份才会显示附件。
其他的附件,都只能在各自发送的邮件中找到,而Reply的连续回复中,只会有最后的附件。
package com.xxx.mail.service.impl; import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.xxx.mail.service.IMailOffice365Service; import com.microsoft.graph.models.*; import com.microsoft.graph.serviceclient.GraphServiceClient; import com.microsoft.graph.users.item.messages.item.reply.ReplyPostRequestBody; import com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody; import okhttp3.Request; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.PostConstruct; import java.util.*; import java.util.List; @Service("mailOffice365Util") public class MailOffice365Impl implements IMailOffice365Service { @Value("${mailOffice365.clientId}") private String clientId; //客户端ID
@Value("${mailOffice365.tenantId}") //租户ID private String tenantId;
@Value("${mailOffice365.clientSecret}") //证书密钥 private String clientSecret;
//scopes = "https://graph.microsoft.com/.default" @Value("${mailOffice365.scopes}") private String scopes; GraphServiceClient graphClient; /** * auth 授权 */ @PostConstruct public void auth() { ClientSecretCredential credential = new ClientSecretCredentialBuilder() .clientId(clientId) .tenantId(tenantId) .clientSecret(clientSecret) .build(); graphClient = new GraphServiceClient(credential, scopes); } /** * 获取用户信息 * * @param email 邮箱 * @return */ public User getUser(String email) { User user = graphClient .users() .byUserId(email) .get(requestConfiguration -> { requestConfiguration.queryParameters.select = new String[]{"displayName", "mail", "userPrincipalName"}; }); return user; } /** * 邮件文件夹 * * @param email 邮箱 * @return */ @Override public MailFolderCollectionResponse getMailFolders(String email) { MailFolderCollectionResponse result = graphClient .users() .byUserId(email) .mailFolders() .get(); return result; } /** * 获取文件夹邮件 * * @param email 邮箱 * @return */ public MessageCollectionResponse getFolderMails(String email, String folderId, int page, int size) { MessageCollectionResponse message = graphClient .users() .byUserId(email) .mailFolders() .byMailFolderId(folderId) .messages() .get(requestConfiguration -> { requestConfiguration.queryParameters.select = new String[]{"id", "from", "isRead", "receivedDateTime", "subject"}; requestConfiguration.queryParameters.skip = page * size; requestConfiguration.queryParameters.top = size; requestConfiguration.queryParameters.orderby = new String[]{"receivedDateTime DESC"}; }); return message; } /** * 获取邮件 * * @param email 邮箱 * @return */ public MessageCollectionResponse getMails(String email, int page, int size) { MessageCollectionResponse message = graphClient .users() .byUserId(email) .messages() .get(requestConfiguration -> { requestConfiguration.queryParameters.select = new String[]{"id", "from", "isRead", "receivedDateTime", "subject"}; requestConfiguration.queryParameters.skip = page * size; requestConfiguration.queryParameters.top = size; requestConfiguration.queryParameters.orderby = new String[]{"receivedDateTime DESC"}; }); return message; } /** * 邮件详情 * * @param email 邮箱 * @param messageId 邮件id * @return */ @Override public Message getMailById(String email, String messageId) { Message message = graphClient .users() .byUserId(email) .messages() .byMessageId(messageId) .get(requestConfiguration -> { requestConfiguration.queryParameters.select = new String[]{"id", "from", "isRead", "receivedDateTime", "subject","body","hasAttachments"}; requestConfiguration.queryParameters.expand = new String []{"attachments"}; }); //转换邮件中的图片 message = getMailByIdWithAttachment(message); return message; } @Override public List<Message> getMailByIds(String email, List<String> messageIds) { List<Message> messages = new ArrayList<>(); if (!CollectionUtils.isEmpty(messageIds)) { for (String messageId : messageIds) { Message message = getMailById(email, messageId); messages.add(message); } } return messages; } /** * 获取带有图片的邮件 * * @param message 邮件 * @return */ @Override public Message getMailByIdWithAttachment(Message message) { //邮件内图片展示 String emailBody = message.getBody().getContent(); if(!CollectionUtils.isEmpty(message.getAttachments())) { for (Attachment attachment : message.getAttachments()) { if (attachment.getIsInline().booleanValue()) { if ((attachment instanceof FileAttachment) && (attachment.getContentType().contains("image"))) { FileAttachment fileAttachment = (FileAttachment) attachment; byte[] contentBytes = fileAttachment.getContentBytes(); String imageContentIDToReplace = "cid:" + fileAttachment.getContentId(); emailBody = emailBody.replace(imageContentIDToReplace, String.format("data:image;base64,%s", Base64.getEncoder().encodeToString(contentBytes ))); } } } } message.getBody().setContent(emailBody); return message; } /** * 发送邮件 * * @param sender 发件人 * @param recipient 收件人 * @param subject 主题 * @param body 内容 * @throws Exception */ public Message sendMail(String sender, String recipient, String subject, String body) throws Exception { List<String> recipients = List.of(recipient); return sendMail(sender, recipients, subject, body, null); } /** * 发送邮件(多个收件人) * * @param sender 发件人 * @param recipients 多收件人 * @param subject 主题 * @param body 内容 * @throws Exception */ public Message sendMail(String sender, List<String> recipients, String subject, String body) throws Exception { return sendMail(sender, recipients, subject, body, null); } @Override public Message sendMail(String sender, List<String> recipients, String subject, String body, List<FileAttachment> files) throws Exception { // Ensure client isn't null if (graphClient == null) { throw new Exception("Graph has not been initialized for user auth"); } // Create a new message SendMailPostRequestBody sendMailPostRequestBody = new SendMailPostRequestBody(); Message message = new Message(); message.setSubject(subject); ItemBody itemBody=new ItemBody(); itemBody.setContent(body); itemBody.setContentType(BodyType.Html); message.setBody(itemBody); //收件人 if (!CollectionUtils.isEmpty(recipients)) { List<Recipient> toRecipients=new ArrayList<>(); for (String item : recipients) { Recipient recipient = new Recipient(); EmailAddress emailAddress = new EmailAddress(); emailAddress.setAddress(item); recipient.setEmailAddress(emailAddress); toRecipients.add(recipient); } message.setToRecipients(toRecipients); } //附件 message = addAttarchment(message, files); // Send the message sendMailPostRequestBody.setMessage(message); graphClient .users() .byUserId(sender) .sendMail() .post(sendMailPostRequestBody); return message; } public Message replyMail(String messageId, String sender, String recipient, String body) throws Exception { List<String> recipients = List.of(recipient); return replyMail(messageId, sender, recipients, body, null); } public Message replyMail(String messageId, String sender, List<String> recipients, String body) throws Exception { return replyMail(messageId, sender, recipients, body, null); } @Override public Message replyMail(String messageId, String sender, List<String> recipients, String body, List<FileAttachment> files) throws Exception { ReplyPostRequestBody replyPostRequestBody=new ReplyPostRequestBody(); Message message = new Message(); //附件 message = addAttarchment(message, files); //收件人 if (!CollectionUtils.isEmpty(recipients)) { List<Recipient> toRecipients=new ArrayList<>(); for (String item : recipients) { Recipient recipient = new Recipient(); EmailAddress emailAddress = new EmailAddress(); emailAddress.setAddress(item); recipient.setEmailAddress(emailAddress); toRecipients.add(recipient); } message.setToRecipients(toRecipients); } replyPostRequestBody.setMessage(message); replyPostRequestBody.setComment(body); graphClient .users() .byUserId(sender) .messages() .byMessageId(messageId) .reply() .post(replyPostRequestBody); return message; } @Override public Message addAttarchment(Message message, List<FileAttachment> files) { if (files != null && files.size() > 0) { List<Attachment> attachmentList = new ArrayList<>(); for (FileAttachment file : files) { FileAttachment attachment = new FileAttachment(); attachment.setName(file.getName()); attachment.setOdataType("#microsoft.graph.fileAttachment"); attachment.setContentType(file.getContentType()); attachment.setContentBytes(file.getContentBytes()); attachment.setIsInline(file.getIsInline()); attachment.setContentId(file.getContentId()); attachmentList.add(attachment); } message.setAttachments(attachmentList); } return message; } @Override public Message setRead(String sender, Message message) { if (!message.getIsRead()) { Message newMessage = new Message(); newMessage.setIsRead(true); graphClient .users() .byUserId(sender) .messages() .byMessageId(message.getId()) .patch(newMessage); } return message; } }
三, Controller
package com.xxx.mail.controller; import com.xxx.common.core.domain.R; import com.xxx.common.satoken.utils.LoginHelper; import com.xxx.common.web.core.BaseController; import com.xxx.mail.domain.MessageVo; import com.xxx.mail.service.IMailOffice365Service; import com.microsoft.graph.models.*; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; @Validated @RequiredArgsConstructor @RestController @RequestMapping("/mail365") public class MailController extends BaseController { @Autowired IMailOffice365Service mailOffice365Service; @GetMapping("/getUser") public R<User> getUser() { String account = LoginHelper.getLoginUser().getEmail(); User user = mailOffice365Service.getUser(account); return R.ok(user); } @GetMapping("/getMailFolders") public R<List<MailFolder>> getMailFolders() { String account = LoginHelper.getLoginUser().getEmail(); MailFolderCollectionResponse folder = mailOffice365Service.getMailFolders(account); return R.ok(folder.getValue()); } @GetMapping("/getFolderMails") public R<List<Message>> getFolderMails(String folderId, int pageIndex) { String account = LoginHelper.getLoginUser().getEmail(); MessageCollectionResponse mails = mailOffice365Service.getFolderMails(account, folderId, pageIndex, 10); return R.ok(mails.getValue()); } @GetMapping("/getMails") public R<List<Message>> getMails(int pageIndex) { String account = LoginHelper.getLoginUser().getEmail(); MessageCollectionResponse mails = mailOffice365Service.getMails(account, pageIndex, 10); return R.ok(mails.getValue()); } @GetMapping("/getMailById") public R<Message> getMailById(String messageId) { String account = LoginHelper.getLoginUser().getEmail(); Message mail = mailOffice365Service.getMailById(account, messageId); //设置已读 mailOffice365Service.setRead(account,mail); return R.ok(mail); } @GetMapping("/getMailByIds") public R<List<Message>> getMailByIds(List<String> messageIds){ String account = LoginHelper.getLoginUser().getEmail(); List<Message> mails = mailOffice365Service.getMailByIds(account, messageIds); for(Message mail : mails){ //设置已读 mailOffice365Service.setRead(account,mail); } return R.ok(mails); } @PostMapping("/sendMail") public R<Message> sendMail(@RequestBody MessageVo req) throws Exception { String account = LoginHelper.getLoginUser().getEmail(); mailOffice365Service.sendMail( account ,req.getRecipients() ,req.getSubject() ,req.getBody() ,req.getAttachments() ); return R.ok(); } @PostMapping("/replyMail") public R<Message> replyMail(@RequestBody MessageVo req) throws Exception { String account = LoginHelper.getLoginUser().getEmail(); Message message = mailOffice365Service.replyMail(req.getMessageId() , account , req.getRecipients() , req.getBody() , req.getAttachments()); return R.ok(message); } }
MessageVo
这个类,纯粹是为了发送邮件时,带上附件一起调用接口。
package com.xxx.mail.domain; import com.microsoft.graph.models.Attachment; import com.microsoft.graph.models.FileAttachment; import lombok.Data; import java.util.ArrayList; import java.util.List; @Data public class MessageVo { private String messageId; private List<String> recipients; private String subject; private String body; private List<FileAttachment> attachments=new ArrayList<>(); }