gpt4 book ai didi

java - 使用 AuthenticatingSMTPClient 和 SSL?

转载 作者:行者123 更新时间:2023-12-04 18:15:10 24 4
gpt4 key购买 nike

我需要使用 SSL (SMTPS) 和身份验证发送电子邮件。然而,在 apache Commons Net 中似乎有 AuthenticatingSMTPClient(没有 SSL,虽然它扩展了 SMTPSClient?)或 SMTPSClient(没有身份验证?),我需要两者的组合(SSL + 身份验证)。任何人都知道我该怎么做?谢谢!

最佳答案

我知道现在回复这个为时已晚,但供其他人引用,AuthenticatingSMTPClient确实提供ssl + authentication因为它正在扩展SMTPSClient.下面是示例代码,必须在我用数字注释的地方进行修改(例如 //1 )。

代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient;
import org.apache.commons.net.smtp.AuthenticatingSMTPClient.AUTH_METHOD;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;

public final class SMTPMail
{
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException
{
String sender, recipient, subject, filename, server;
List<String> ccList = new ArrayList<String>();
FileReader fileReader = null;
Writer writer;
SimpleSMTPHeader header;
AuthenticatingSMTPClient client;

server = "<smtp server>"; // 1
try
{
sender = "<your user name>"; // 2
recipient = "<recipient>"; // 3
subject = "<mail subject>"; // 4

header = new SimpleSMTPHeader(sender, recipient, subject);
filename = "hello.txt"; //This will be the body of your mail //5

try
{
fileReader = new FileReader(filename);
}
catch (FileNotFoundException e)
{
System.err.println("File not found. " + e.getMessage());
}

client = new AuthenticatingSMTPClient("TLS", false);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out), true));

client.connect(server);

if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
{
client.disconnect();
System.err.println("SMTP server refused connection.");
System.exit(1);
}

client.login("hostname.testing.smtp.api"); //6

if(client.execTLS())
{
if(client.auth(AUTH_METHOD.LOGIN, "<your user name>", "<your password>")) //7
{
client.setSender(sender);
client.addRecipient(recipient);
for (String recpt : ccList) {
client.addRecipient(recpt);
}

writer = client.sendMessageData();

if (writer != null)
{
writer.write(header.toString());
Util.copyReader(fileReader, writer);
writer.close();
client.completePendingCommand();
}

if (fileReader != null ) {
fileReader.close();
}
}
}
client.logout();
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
}

关于java - 使用 AuthenticatingSMTPClient 和 SSL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com