Setting Up Mail Protocol Libraries for IMAP, SMTP and POP3 Clients in Java
ImapClient, SmtpClient และ Pop3Client จาก com.aspose.email.services.inetmail.client package work through Jakarta Mail or JavaMail. Aspose.Email does not include these libraries, so add one of them to your project. Without a library, the build method of the client builder throws IllegalStateException with a message that names the libraries to add.
Choosing a Library
| Library | Package | Java | |—|—|—| | JavaMail 1.6 | javax.mail | 8 or later | | Jakarta Mail 2.0 | jakarta.mail | 8 or later | | Jakarta Mail 2.1 with Eclipse Angus 2 | jakarta.mail | 11 or later |
The following dependencies add each of these libraries:
<!-- JavaMail 1.6 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- Jakarta Mail 2.0 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Jakarta Mail 2.1 with Eclipse Angus 2 -->
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
Maven adds the libraries that these artifacts depend on. If you add JAR files to the classpath manually, also add their dependencies. For example, JavaMail 1.6 on Java 11 or later also requires the JavaBeans Activation Framework (javax.activation).
jakarta.mail-api is on the classpath, build ขว้าง IllegalStateException: No provider of jakarta.mail.util.StreamProvider was found. Do not use Eclipse Angus 1 on Java 8, because sending messages does not work with this combination. On Java 8, use JavaMail 1.6 or Jakarta Mail 2.0.
การเลือกอัตโนมัติ
คุณไม่ต้องระบุไลบรารีในโค้ด. เมื่อคุณเรียก build, ลูกค้าจะตรวจสอบคลาสที่มีอยู่ที่เวลาเรียกใช้และเลือกไลบรารีตามลำดับต่อไปนี้:
- ไลบรารีที่มี
jakarta.mailแพคเกจ. - ไลบรารีที่มี
javax.mailแพคเกจ.
การเลือกขึ้นอยู่กับแพคเกจและไม่ใช่ชื่อของ artifact. ตัวอย่างเช่น com.sun.mail:jakarta.mail:1.6.7 มี javax.mail แพคเกจ, ดังนั้นลูกค้าจะใช้เป็น JavaMail.
การตั้งค่าการเชื่อมต่อ
ผู้สร้างของลูกค้าทั้งสามมีการตั้งค่าการเชื่อมต่อเดียวกัน. ImapClient และ Pop3Client ต้องการ host และ credentials. SmtpClient ต้องการเพียง host: โดยไม่มีเอกสารประจำตัว จะเชื่อมต่อกับเซิร์ฟเวอร์โดยไม่มีการยืนยันตัวตน.
| การตั้งค่า | วิธีสร้าง | ImapClient default | SmtpClient default | Pop3Client default | |—|—|—|—|—| | ชื่อเซิร์ฟเวอร์ | host | ไม่มี | ไม่มี | ไม่มี | | พอร์ต | port | 993 | 587 | 995 | | ความปลอดภัย | security, ssl, starttls | SSL | STARTTLS | SSL | | เอกสารประจำตัว | credentials | ไม่มี | ไม่มี | ไม่มี | | เวลารอการเชื่อมต่อ (มิลลิวินาที) | connectTimeout | 30 000 | 30 000 | 30 000 | | เวลาอ่านหมด (มิลลิวินาที) | readTimeout | 60 000 | 60 000 | 60 000 |
ความปลอดภัยของการเชื่อมต่อถูกตั้งค่าด้วย SecurityType enumeration:
| ค่า | การเชื่อมต่อ | |—|—| | SSL | เข้ารหัสตั้งแต่เริ่มต้น | | STARTTLS | Starts unencrypted and then switches to encryption. If the server does not support this, the operation fails. | | NONE | Not encrypted |
The following code sample sets all connection settings for ImapClient:
import com.aspose.email.services.inetmail.SecurityType;
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.port(993)
.security(SecurityType.SSL)
.credentials(new PasswordCredentials("user@example.com", "password"))
.connectTimeout(10000)
.readTimeout(30000)
.build()) {
System.out.println(imap.listFolders().size());
}
A client connects when it performs its first operation. For this reason, build succeeds even if the server is unavailable or the credentials are wrong, and the error appears at the first operation as MailClientException.
Authenticating with a Password
To sign in with a user name and a password, use PasswordCredentials:
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
System.out.println(imap.listFolders().size());
}
Authenticating with OAuth 2.0
To sign in with an OAuth 2.0 access token through the XOAUTH2 mechanism, use OAuth2Credentials. Get the token from your identity provider before you create the client. The client does not refresh the token, so create a new client when the token expires.
import com.aspose.email.services.inetmail.auth.OAuth2Credentials;
import com.aspose.email.services.inetmail.client.ImapClient;
try (ImapClient imap = ImapClient.builder()
.host("imap.example.com")
.credentials(new OAuth2Credentials("user@example.com", "access-token"))
.build()) {
System.out.println(imap.listFolders().size());
}
Next Steps
To learn how to use the clients, see the following pages: