连接到 SMTP 服务器
连接支持 SSL 的 SMTP 服务器时需要设置以下属性。
以下示例展示了如何:
- 设置用户名。
- 设置密码。
- 设置端口。
- 设置安全选项。
以下代码片段展示了如何连接支持 SSL 的 SMTP 服务器。
// 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 代理服务器连接到服务器
有时我们使用代理服务器与外部世界通信。在此类情况下,邮件客户端如果不指定代理地址则无法通过互联网通信。Aspose.Email 提供对 SOCKS 代理协议 4、4a 和 5 版本的支持。本文提供了使用代理邮件服务器发送邮件的工作示例。通过代理服务器发送邮件:
- 使用所需信息(即代理地址、端口和 SOCKS 版本)初始化代理。
- 初始化 SmtpClient 并提供主机地址、用户名、密码以及其他设置。
- 将客户端的 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 代理服务器连接到服务器
// 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 类。此方法允许客户端确定哪些身份验证方法可用于与服务器建立安全连接。然后,使用 setAllowedAuthentication 方法用于获取(或设置)用户允许的身份验证类型枚举,选择最合适的客户端-服务器通信身份验证机制。这允许您明确设置邮件客户端的身份验证方式。
以下代码示例展示了如何自定义电子邮件客户端的身份验证:
smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.Login);
使用 CRAM-MD5 身份验证连接到服务器
为确保与 SMTP 服务器的安全身份验证和通信,您可以指定并强制使用 CRAM-MD5 作为 SMTP 客户端允许的身份验证方法。以下代码片段展示了如何为 SmtpClient:
smtpClient.setAllowedAuthentication(SmtpKnownAuthenticationType.CramMD5);
将 SMTP 客户端绑定到主机上的特定 IP 地址
主机可能拥有多个可用于发送邮件的端口,这种情况并不能排除。在此类情况下,可能需要将邮件发送客户端绑定到主机上的特定端口以发送邮件。这可以使用 Aspose.Email API 并通过 SmtpClient bindIPEndPoint 属性。API SmtpClient 可以通过指定特定的 IP 端点来设置使用主机上的特定 IP 地址。以下代码片段展示了如何将 SMTP 客户端绑定到主机上的特定 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
}
}
如何为邮件操作设置超时
每个邮件操作都需要一定时间,取决于多种因素(网络延迟、数据大小、服务器性能等)。您可以为所有邮件操作设置超时。下面的代码示例展示了如何使用 超时 属性。注意:不应设置过大的值,以免在应用程序中导致长时间等待。
try (SmtpClient smtpClient = new SmtpClient("host", 587, "username", "password", SecurityOptions.SSLExplicit))
{
smtpClient.setTimeout(60000); // 60 seconds
// some code...
}