การเชื่อมต่อไปยัง SMTP Server

คุณสมบัติต่อไปนี้ต้องตั้งค่าเมื่อเชื่อมต่อกับเซิร์ฟเวอร์ SMTP ที่รองรับ SSL

ในตัวอย่างด้านล่างเราจะแสดงวิธี:

  1. ตั้งค่าชื่อผู้ใช้
  2. ตั้งค่ารหัสผ่าน
  3. ตั้งค่าพอร์ต
  4. ตั้งค่าตัวเลือกความปลอดภัย

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเชื่อมต่อกับเซิร์ฟเวอร์ SMTP ที่เปิดใช้งาน SSL

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.gmail.com");

// Set username, password, port, and security options
client.setUsername("your.email@gmail.com");
client.setPassword("your.password");
client.setPort(587);
client.setSecurityOptions(SecurityOptions.SSLExplicit);

เชื่อมต่อกับเซิร์ฟเวอร์ผ่าน Socks Proxy Server

ในบางกรณีเราต้องใช้พร็อกซีเซิร์ฟเวอร์เพื่อสื่อสารกับโลกภายนอก ในกรณีเช่นนี้ไคลเอนต์อีเมลไม่สามารถสื่อสารผ่านอินเทอร์เน็ตได้หากไม่ได้ระบุที่อยู่พร็อกซี Aspose.Email รองรับเวอร์ชัน 4, 4a และ 5 ของโปรโตคอล SOCKS บทความนี้มีตัวอย่างการส่งอีเมลโดยใช้เซิร์ฟเวอร์เมลพร็อกซี เพื่อส่งอีเมลผ่านพร็อกซีเซิร์ฟเวอร์:

  1. Initialize Proxy ด้วยข้อมูลที่จำเป็น ได้แก่ ที่อยู่พร็อกซี พอร์ต และเวอร์ชัน SOCKS
  2. Initialize SmtpClient โดยระบุที่อยู่โฮสต์ ชื่อผู้ใช้และรหัสผ่าน พร้อมการตั้งค่าอื่น ๆ
  3. ตั้งค่าคุณสมบัติ Proxy ของไคลเอนต์เป็น Proxy วัตถุที่สร้างก่อนหน้านี้

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเชื่อมต่อกับเซิร์ฟเวอร์ผ่านพร็อกซีเซิร์ฟเวอร์

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.domain.com", "username", "password");
client.setSecurityOptions(SecurityOptions.SSLImplicit);
String proxyAddress = "192.168.203.142"; // proxy address
int proxyPort = 1080; // proxy port
SocksProxy proxy = new SocksProxy(proxyAddress, proxyPort, SocksVersion.SocksV5);
client.setProxy(proxy);
client.send(new MailMessage("sender@domain.com", "receiver@domain.com", "Sending Email via proxy",
        "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)"));

เชื่อมต่อกับเซิร์ฟเวอร์ผ่าน HTTP Proxy Server

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

HttpProxy proxy = new HttpProxy("18.222.124.59", 8080);
try (SmtpClient client = new SmtpClient("host", 587, "username", "password")) {
    client.setProxy(proxy);
    client.send(new MailMessage("sender@domain.com", "receiver@domain.com", "Sending Email via proxy",
            "Implement socks proxy protocol for versions 4, 4a, 5 (only Username/Password authentication)"));
}

ปรับแต่งกลไกการรับรองความถูกต้อง

ดึงรายการกลไกการรับรองความถูกต้องที่เซิร์ฟเวอร์ SMTP รองรับโดยใช้ getSupportedAuthentication เมธอดของ SmtpClient class. วิธีการนี้ทำให้ไคลเอนต์สามารถกำหนดว่ามีวิธีการรับรองความถูกต้องใดบ้างที่ใช้ได้สำหรับการสร้างการเชื่อมต่อที่ปลอดภัยกับเซิร์ฟเวอร์ จากนั้นโดยใช้ setAllowedAuthentication method ที่รับ (หรือกำหนด) การ列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列列ต่อ…

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีปรับแต่งการรับรองความถูกต้องของไคลเอนต์อีเมล:

smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.Login);

การใช้การรับรองความถูกต้อง CRAM-MD5 เพื่อเชื่อมต่อกับเซิร์ฟเวอร์

เพื่อให้การรับรองความถูกต้องและการสื่อสารแบบปลอดภัยกับเซิร์ฟเวอร์ SMTP คุณสามารถระบุและบังคับให้ใช้ CRAM-MD5 เป็นวิธีการรับรองความถูกต้องที่อนุญาตสำหรับ SMTP client ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกำหนดประเภทการรับรองความถูกต้องที่อนุญาตสำหรับ SmtpClient:

smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.CramMD5);

ผูก SMTP Client ไปยังที่อยู่ IP เฉพาะบนโฮสต์

ความเป็นไปได้ที่โฮสต์จะมีพอร์ตหลายพอร์ตสำหรับส่งอีเมลไม่สามารถปฏิเสธได้ ในกรณีเช่นนี้อาจจำเป็นต้องผูกไคลเอนต์การส่งอีเมลกับพอร์ตเฉพาะบนโฮสต์เพื่อส่งอีเมล สามารถทำได้ด้วย Aspose.Email API โดยใช้ SmtpClient bindIPEndPoint property. The API SmtpClient สามารถตั้งค่าให้ใช้ที่อยู่ IP เฉพาะบนโฮสต์ได้โดยระบุ IP Endpoint เฉพาะ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการผูก SMTP Client กับที่อยู่ IP เฉพาะบนโฮสต์

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

SmtpClient client = new SmtpClient("smtp.domain.com", // host
        587, // port
        "username", // username
        "password", // password
        SecurityOptions.Auto // Security Options
);
try {
    client.bindIPEndPoint(new BindIPEndPointHandler() {
        public InetSocketAddress invoke(InetSocketAddress remoteEndPoint) {
            return new InetSocketAddress(0);
        }
    });
    client.noop();
} finally {
    client.dispose();
}

ตรวจสอบข้อมูลประจำตัวเซิร์ฟเวอร์เมลโดยไม่ส่งอีเมล

บางครั้งจำเป็นต้องตรวจสอบข้อมูลประจำตัวโดยไม่ส่งอีเมล Aspose.Email ให้ validateCredentials() เมธอดเพื่อทำการดำเนินการนี้ หากการตรวจสอบสำเร็จ โค้ดภายในเงื่อนไข if จะถูกดำเนินการ ซึ่งมักใช้เพื่อทำการต่อหรือดึงข้อมูลจากเซิร์ฟเวอร์ IMAP ตัวอย่างโค้ดต่อไปนี้แสดงการตรวจสอบข้อมูลประจำตัวโดยไม่ส่งอีเมล:

try (SmtpClient smtpClient = new SmtpClient(
        server.SmtpUrl, server.SmtpPort, "username", "password", SecurityOptions.Auto)) {
    smtpClient.setTimeout(4000);

    if (smtpClient.validateCredentials()) {
        // to do something
    }
}

วิธีตั้งค่า Timeout สำหรับการดำเนินการเมล

การดำเนินการเมลแต่ละครั้งใช้เวลาตามปัจจัยหลายอย่าง (ความล่าช้าเครือข่าย, ขนาดข้อมูล, ประสิทธิภาพเซิร์ฟเวอร์ เป็นต้น) คุณสามารถตั้งค่าเวลาจำกัดสำหรับการดำเนินการเมลทั้งหมด ตัวอย่างโค้ดด้านล่างแสดงวิธีทำโดยใช้ เวลาจำกัด คุณสมบัติ หมายเหตุ: ไม่ควรกำหนดค่าขนาดใหญ่เพื่อหลีกเลี่ยงการรอคอยนานในแอปพลิเคชันของคุณ

try (SmtpClient smtpClient = new SmtpClient("host", 587, "username", "password", SecurityOptions.SSLExplicit))
{
    smtpClient.setTimeout(60000); // 60 seconds

    // some code...
}