Gửi Thông điệp Bằng Client Dịch vụ SMTP trong Java
SmtpClient từ com.aspose.email.services.inetmail.client gói gửi tin qua máy chủ SMTP. Nó yêu cầu một thư viện giao thức thư trên classpath. Để biết chi tiết, xem Cấu hình Thư viện Giao thức Mail.
com.aspose.email gói có một lớp cùng tên, được mô tả trong Làm việc với SMTP Client. Sử dụng import rõ ràng để chọn lớp bạn cần.
Gửi Tin Văn Bản
Tạo tin với InetMessage.builder(), và sau đó truyền cho send. Đặt người gửi và ít nhất một người nhận. Đối với tin không có người nhận, send ném MailClientException trước khi nó kết nối tới máy chủ. Một tin không có người gửi sẽ tới người nhận mà không có From địa chỉ, và client ghi cảnh báo vào log.
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.port(587)
.starttls()
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
}
Mỗi lần gọi send mở kết nối riêng tới máy chủ và đóng lại khi tin đã được gửi.
Kết Nối Không Xác Thực
Một số máy chủ, ví dụ một relay trong mạng nội bộ, chấp nhận tin mà không cần xác thực. Để kết nối tới máy chủ như vậy, không gọi credentials:
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("relay.example.com")
.port(25)
.noSecurity()
.build()) {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
}
Cài đặt Tên Máy Chủ Địa Phương
Khi client kết nối, nó gửi tên máy tính địa phương tới máy chủ. Để lấy tên này, thư viện thư tra cứu địa chỉ địa phương trong DNS. Nếu máy tính không có bản ghi DNS ngược, việc tra cứu có thể mất vài giây và lặp lại mỗi khi gọi send. Để bỏ qua tra cứu, đặt tên với localHost:
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.credentials(new PasswordCredentials("user@example.com", "password"))
.localHost("client.example.com")
.build();
Cài đặt Người Nhận và Tên Người Gửi
Cái to, cc và bcc phương thức chấp nhận một hoặc nhiều địa chỉ. Để hiển thị tên người gửi, truyền tên trước và địa chỉ sau:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("Reports Team", "user@example.com")
.to("user@example.com", "user2@example.com")
.cc("user3@example.com")
.bcc("user4@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
Gửi Tin HTML
Sử dụng htmlBody để đặt phiên bản HTML của tin. Nếu bạn cũng đặt textBody, tin chứa cả hai phiên bản, và client thư của người nhận hiển thị phiên bản nó hỗ trợ:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.htmlBody("<p>The report is <b>ready</b>.</p>")
.build();
smtp.send(message);
Thêm Tệp Đính Kèm
Để thêm tệp đính kèm, tạo một InetAttachment đối tượng có tên tệp, loại nội dung và dữ liệu, sau đó truyền cho attach:
import com.aspose.email.services.inetmail.model.InetAttachment;
import com.aspose.email.services.inetmail.model.InetMessage;
import java.nio.charset.StandardCharsets;
byte[] data = "Total: 42".getBytes(StandardCharsets.UTF_8);
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is attached.")
.attach(InetAttachment.of("report.txt", "text/plain", data))
.build();
smtp.send(message);
Xử lý Lỗi
Khi một tin không thể gửi được, ví dụ vì máy chủ từ chối thông tin đăng nhập, send ném MailClientException, mở rộng java.io.IOException:
import com.aspose.email.services.inetmail.MailClientException;
import com.aspose.email.services.inetmail.model.InetMessage;
try {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("not a valid address")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
} catch (MailClientException e) {
System.out.println(e.getMessage() + ": " + e.getCause().getMessage());
}