ภาพรวมของไคลเอนต์บริการเมลใน Java

เริ่มตั้งแต่เวอร์ชัน 26.10, Aspose.Email for Java มีคลไอเอนท์สำหรับบริการเมลบนคลาวด์และโปรโตคอลเมล, พร้อมกับ UnifiedMailClient, API เดียวที่ทำงานเหนือพวกมัน หน้านี้แสดงรายการคลไอเอนท์, อธิบายว่าพวกมันเกี่ยวข้องกับ API ที่มีอยู่อย่างไร, และแสดงวิธีการใช้

ไคลเอนต์ที่พร้อมใช้

ไคลเอนต์ทุกตัวสร้างด้วยบิลเดอร์และ implements java.io.Closeable.

| Service | Client | Package | Connection | |—|—|—|—| | IMAP | ImapClient | com.aspose.email.services.inetmail.client | ไลบรารีโปรโตคอลเมล | | SMTP | SmtpClient | com.aspose.email.services.inetmail.client | ไลบรารีโปรโตคอลเมล | | POP3 | Pop3Client | com.aspose.email.services.inetmail.client | ไลบรารีโปรโตคอลเมล | | SendGrid | SendGridClient | com.aspose.email.services.sendgrid.client | HTTP | | Mailgun | MailgunClient | com.aspose.email.services.mailgun.client | HTTP | | Amazon SES | SesClient | com.aspose.email.services.ses.client | HTTP | | Gmail | GmailClient | com.aspose.email.services.google.mail.client | HTTP | | Google Calendar | CalendarClient | com.aspose.email.services.google.calendar.client | HTTP | | Google People | PeopleClient | com.aspose.email.services.google.people.client | HTTP | | Microsoft Graph | MsGraphClient | com.aspose.email.services.msgraph.client | HTTP | | Exchange EWS | EwsClient | com.aspose.email.services.ews.client | HTTP |

ไคลเอนต์ที่เชื่อมต่อผ่าน HTTP ไม่ขึ้นกับไลบรารี HTTP เฉพาะใด ๆ เพื่อเรียนรู้วิธีเลือกหนึ่งดู HTTP Clients สำหรับบริการอีเมล.

ไคลเอนต์บริการและ API ที่มีอยู่

ไคลเอนต์บริการไม่ได้แทนที่คลาสใน com.aspose.email แพคเกจ เช่น ImapClient, SmtpClient, Pop3Client, EWSClient, GmailClient และ GraphClient. คลาสเหล่านี้ยังคงใช้งานได้และอธิบายในส่วนของคู่มือนี้ของตนเอง

ชุดคลาสสองชุดใช้ประเภทข้อมูลที่แตกต่างกัน:

  • ไคลเอนต์บริการทำงานกับโมเดลข้อมูลของบริการของมัน เช่น GraphMessage สำหรับ Microsoft Graph.
  • UnifiedMailClient ทำงานกับประเภท Aspose.Email: MailMessage, Appointment, VCardContact และ MapiTask.

ความสามารถของ UnifiedMailClient

UnifiedMailClient เข้าถึงแต่ละบริการผ่านผู้ให้บริการ ไม่ใช่ทุกบริการสนับสนุนทุกการดำเนินการ:

| ผู้ให้บริการ | บริการ | ส่ง | อ่าน | ปฏิทิน | ผู้ติดต่อ | งาน | |—|—|—|—|—|—|—| | MsGraphProvider | Microsoft Graph | มี | มี | มี | มี | มี | | ExchangeEwsProvider | Exchange EWS | มี | มี | มี | มี | มี | | GoogleProvider | Gmail, Google Calendar, Google People | มี | มี | มี | มี | ไม่ | | InetMailProvider | SMTP และ IMAP | มี | มี | ไม่ | ไม่ | ไม่ | | SendGridProvider | SendGrid | มี | ไม่ | ไม่ | ไม่ | ไม่ | | MailgunProvider | Mailgun | มี | ไม่ | ไม่ | ไม่ | ไม่ | | AmazonSesProvider | Amazon SES | มี | ไม่ | ไม่ | ไม่ | ไม่ |

InetMailProvider อ่านข้อความผ่าน IMAP. POP3 มีให้ใช้งานเฉพาะผ่านแบบแยกส่วน Pop3Client.

เพื่อตรวจสอบความสามารถใน runtime, เรียก supports ด้วยค่าของ UnifiedCapability enumeration. การเข้าถึงความสามารถที่ผู้ให้บริการไม่สนับสนุน, ตัวอย่างเช่นการเรียก calendar() บน SendGridProvider, โยน UnsupportedCapabilityException.

ข้อกำหนด

คลไอเอนท์ต้องใช้ Aspose.Email for Java 26.10 หรือใหม่กว่าและ Java 8 หรือใหม่กว่า สิ่งที่ต้องการอื่น ๆ ขึ้นอยู่กับคลไอเอนท์:

  • คลไอเอนท์ที่เชื่อมต่อผ่าน HTTP ทำงานโดยไม่มีไลบรารีเพิ่มเติม หากต้องการเรียนรู้การใช้ไลบรารี HTTP อื่น ๆ ดู HTTP Clients สำหรับบริการอีเมล.
  • ImapClient, SmtpClient และ Pop3Client ต้องการ Jakarta Mail หรือ JavaMail บน classpath สำหรับรายละเอียดดู Mail Protocol Libraries Setup.

ใช้ Service Client

ตัวอย่างโค้ดต่อไปนี้เชื่อมต่อกับ Microsoft Graph ด้วยแอปพลิเคชันที่ลงทะเบียนใน Microsoft Entra ID และแสดงหัวข้อของข้อความห้าข้อความจากกล่องเมล:

import com.aspose.email.services.msgraph.api.QueryOptions;
import com.aspose.email.services.msgraph.auth.ClientCredentialsTokenProvider;
import com.aspose.email.services.msgraph.client.MsGraphClient;
import com.aspose.email.services.msgraph.model.GraphMessage;
import java.util.List;

try (MsGraphClient graph = MsGraphClient.builder()
        .tokenProvider(new ClientCredentialsTokenProvider("tenant-id", "client-id", "client-secret"))
        .build()) {
    List<GraphMessage> messages = graph.users("user@example.com").messages()
            .list(QueryOptions.builder().top(5).build());
    for (GraphMessage message : messages) {
        System.out.println(message.getSubject());
    }
}

ใช้ UnifiedMailClient

ตัวอย่างโค้ดต่อไปนี้อ่านกล่องเมลเดียวกันผ่าน UnifiedMailClient. เพื่อทำงานกับบริการอื่น, เปลี่ยนผู้ให้บริการ:

import com.aspose.email.services.bridge.common.model.MailMessageInfo;
import com.aspose.email.services.bridge.msgraph.provider.MsGraphProvider;
import com.aspose.email.services.unified.UnifiedMailClient;
import com.aspose.ext.http.HttpClientAutoDetect;
import com.aspose.ext.http.IHttpClient;
import java.util.List;

try (IHttpClient http = HttpClientAutoDetect.detect()) {
    MsGraphProvider provider = MsGraphProvider.builder()
            .httpClient(http)
            .tenantId("tenant-id")
            .clientId("client-id")
            .clientSecret("client-secret")
            .userPrincipalName("user@example.com")
            .build();
    try (UnifiedMailClient client = UnifiedMailClient.builder().provider(provider).build()) {
        List<MailMessageInfo> messages = client.mail().inbox().list(5);
        for (MailMessageInfo message : messages) {
            System.out.println(message.getSubject());
        }
    }
}

เปิดใช้งานบันทึก

การบันทึกเป็นทางเลือก คลไอเอนท์ทำงานได้โดยไม่มีไลบรารีบันทึกใด ๆ และไม่เขียนอะไรลงบันทึก

เพื่อดูผลลัพธ์ของบันทึก, เพิ่ม SLF4J และการทำงานของ logger ลงใน classpath ชื่อของ logger เริ่มด้วย com.aspose.email หรือ com.aspose.ext.http.