msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

Concurrent requests to send emails will generate duplicate emails, and the number does not match.

Open xiazeroll opened this issue 4 months ago • 0 comments

Hello!

I used Postman to iterate 160 times to send emails, and actually received 160+ emails. The number of emails received is random and does not match the number, which means duplicate emails are generated.

Image Image Image

@PostMapping("/test") public void SysEmailController() { GraphEmailSender.testInfo(); }

`

import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.microsoft.graph.models.EmailAddress; import com.microsoft.graph.models.ItemBody; import com.microsoft.graph.models.Message; import com.microsoft.graph.models.Recipient; import com.microsoft.graph.serviceclient.GraphServiceClient; import com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody;

import java.util.Collections; import java.util.List;

public class GraphEmailSender { // 配置参数 private static final String CLIENT_ID = ""; private static final String CLIENT_SECRET = ""; private static final String TENANT_ID = ""; private static final String SENDER_EMAIL = ""; private static final String[] SCOPES = new String[] { "https://graph.microsoft.com/.default" };

private static GraphServiceClient graphClient;


static {
    try {
        final ClientSecretCredential credential = new ClientSecretCredentialBuilder()
                .clientId(CLIENT_ID)
                .tenantId(TENANT_ID)
                .clientSecret(CLIENT_SECRET)
                .build();

        if (null == credential) {
            throw new Exception("Unexpected error");
        }

        graphClient = new GraphServiceClient(credential, SCOPES);
    } catch (Exception e) {
        System.err.println("init GraphServiceClient error: " + e.getMessage());
        e.printStackTrace();
    }
}

public static void testInfo() {
    try {

        Message message = new Message();
        message.setSubject("test mail - Microsoft Graph API");

        ItemBody body = new ItemBody();
        body.setContent("Microsoft Graph API mail。");
        message.setBody(body);

        Recipient recipient = new Recipient();
        EmailAddress emailAddress = new EmailAddress();
        emailAddress.setAddress("");
        recipient.setEmailAddress(emailAddress);
        List<Recipient> recipients = Collections.singletonList(recipient);
        message.setToRecipients(recipients);

        SendMailPostRequestBody sendMailPostRequestBody =
                new SendMailPostRequestBody();
        sendMailPostRequestBody.setMessage(message);

        //
        graphClient.users()
                .byUserId(SENDER_EMAIL)
                .sendMail()
                .post(sendMailPostRequestBody);

        System.out.println("send success!"+Thread.currentThread());

    } catch (Exception e) {
        System.err.println("error: " + e.getMessage());
        e.printStackTrace();
    }
}

}`

xiazeroll avatar Oct 11 '25 07:10 xiazeroll