实用功能 - SMTP 客户端
Contents
[
Hide
]
使用 Smtp 客户端列出扩展服务器
Aspose.Email SmtpClient 让您检索服务器支持的扩展,例如 IDLE、UNSELECT、QUOTA 等。这有助于在使用客户端的特定功能之前识别扩展的可用性。该 getCapabilities() 方法以字符串数组的形式返回支持的扩展类型。
检索服务器扩展
以下代码片段展示了如何检索服务器扩展。
// 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",587,"username","password");
client.setSecurityOptions(SecurityOptions.Auto);
String[] caps = client.getCapabilities();
for (String str:caps)
System.out.println(str);
使用已签名消息
Aspose.Email API 提供使用证书创建已签名消息的能力。该 attachSignature 方法的 MailMessage 类可用于对消息进行签名以便保存或使用 SmtpClient.
请注意,Aspose.Email for Java API 依赖 Bouncy Castle 提供加密功能。
Bouncy Castle Maven 依赖
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.60</version>
</dependency>
启用 Bouncy Castle 安全提供者
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
if (Security.getProvider("BC") == null)
Security.addProvider(new BouncyCastleProvider());
对消息签名
以下代码片段展示了如何对消息进行签名。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
byte[] privateCert = Files.readAllBytes(new File("sample.pfx").toPath());
byte[] publicCert = Files.readAllBytes(new File("sample.cer").toPath());
MailMessage msg = new MailMessage("userfrom@gmail.com", "userto@gmail.com", "Signed message only", "Test Body of signed message");
MailMessage signed = msg.attachSignature(privateCert, "password");
MailMessage encrypted = signed.encrypt(publicCert, "password");
MailMessage decrypted = encrypted.decrypt(privateCert, "password");
MailMessage unsigned = decrypted.removeSignature();// The original message with proper body
MapiMessage mapi = MapiMessage.fromMailMessage(unsigned);
使用分离证书选项
基于 Web 的电子邮件客户端可能无法显示已签名邮件的正文内容。可以通过在发送给基于 Web 的邮箱客户端之前分离证书来解决此问题。分离标志位于重载方法的 attachSignature 可用于实现此目的。如果设置为 true,证书将从电子邮件中分离,反之亦然。要在基于 Web 的客户端中查看已签名邮件正文,需要创建 MailMessage 使用分离签名。以下代码片段展示了如何使用分离证书选项。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MailMessage msg = new MailMessage("dr38445@gmail.com", "dr38445@gmail.com", "subject:Signed message only by AE", "body:Test Body of signed message by AE");
MailMessage signed = msg.attachSignature(privateCert, "password", true);
SmtpClient smtp = getSmtpClient(); //some test smtp client
smtp.send(signed);