การส่งข้อความโดยใช้ SMTP Service Client ใน Java
SmtpClient จาก com.aspose.email.services.inetmail.client แพคเกจส่งข้อความผ่านเซิร์ฟเวอร์ SMTP. ต้องการไลบรารีโปรโตคอลเมลบน classpath. รายละเอียดดูที่ Mail Protocol Libraries Setup.
com.aspose.email แพคเกจมีคลาสชื่อเดียวกัน ซึ่งอธิบายใน ทำงานกับ SMTP Client. ใช้การนำเข้าที่ชัดเจนเพื่อเลือกคลาสที่ต้องการ.
การส่งข้อความข้อความธรรมดา
สร้างข้อความด้วย InetMessage.builder(), แล้วส่งต่อให้ send. ตั้งผู้ส่งและผู้รับอย่างน้อยหนึ่งคน. สำหรับข้อความที่ไม่มีผู้รับ, send ขว้าง MailClientException ก่อนที่มันจะเชื่อมต่อกับเซิร์ฟเวอร์. ข้อความที่ไม่มีผู้ส่งจะถึงผู้รับโดยไม่มี From ที่อยู่, และไคลเอนต์เขียนคำเตือนลงบันทึก.
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);
}
แต่ละครั้งที่เรียก send เปิดการเชื่อมต่อของตนเองกับเซิร์ฟเวอร์และปิดเมื่อข้อความส่งแล้ว.
การเชื่อมต่อโดยไม่มีการยืนยันตัวตน
เซิร์ฟเวอร์บางตัว, เช่น รีเลย์ในเครือข่ายท้องถิ่น, ยอมรับข้อความโดยไม่มีการยืนยันตัวตน. เพื่อเชื่อมต่อกับเซิร์ฟเวอร์ดังกล่าว, อย่าเรียก 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());
}
การตั้งค่าชื่อโฮสต์ท้องถิ่น
เมื่อไคลเอนต์เชื่อมต่อ, จะส่งชื่อคอมพิวเตอร์ท้องถิ่นไปยังเซิร์ฟเวอร์. เพื่อรับชื่อนี้, ไลบรารีเมลจะค้นหาที่อยู่ใน DNS. หากคอมพิวเตอร์ไม่มีบันทึก DNS กลับ, การค้นหาอาจใช้หลายวินาทีและจะทำซ้ำทุกครั้งที่เรียก send. เพื่อลบการค้นหา, ตั้งชื่อด้วย 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();
การตั้งค่าผู้รับและชื่อผู้ส่ง
นี้ to, cc และ bcc เมธอดรับที่อยู่หนึ่งหรือหลายที่อยู่. เพื่อแสดงชื่อผู้ส่ง, ส่งชื่อก่อนและที่อยู่ถัดไป:
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);
การส่งข้อความ HTML
ใช้ htmlBody เพื่อกำหนดเวอร์ชัน HTML ของข้อความ. หากคุณตั้งค่า textBody, ข้อความประกอบด้วยทั้งสองเวอร์ชัน และไคลเอนต์เมลของผู้รับจะแสดงเวอร์ชันที่รองรับ:
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);
การเพิ่มไฟล์แนบ
เพื่อเพิ่มไฟล์แนบ, สร้าง InetAttachment ออบเจ็กต์ที่มีชื่อไฟล์, ประเภทเนื้อหาและข้อมูล, แล้วส่งต่อให้ 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);
การจัดการข้อผิดพลาด
เมื่อไม่สามารถส่งข้อความได้, เช่น เซิร์ฟเวอร์ปฏิเสธเอกสารประจำตัว, send ขว้าง MailClientException, ซึ่งขยาย 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());
}