gpt4 book ai didi

java - 使用 java api 在 apache james 中创建用户帐户

转载 作者:行者123 更新时间:2023-12-01 12:27:02 26 4
gpt4 key购买 nike

我需要使用 Java API 在 Apache James 邮件服务器中添加新用户,但在整个互联网上没有任何如何使用它的示例。仅此http://james.apache.org/server/2.3.1/adding_users.html使用Java邮件API您可以将邮件发送到Apache James邮箱读取消息,但仅此而已。

String user = "user";  // Newly created user on JAMES  
String password = "user"; // user password

String fromAddress = "user@localhost"; // newlycreateduser@localhost
String toAddress = "usver@gmail.com";


// Create a mail session
Properties properties = new Properties();
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.username", user);
properties.put("mail.smtp.password", password);
Session session = Session.getDefaultInstance(properties, null);

try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));

message.setSubject("Email from our JAMES Server");
message.setText("Hello world again and again");
Transport.send(message);

System.out.println("Email sent successfully");
}
catch (MessagingException e)

{
e.printStackTrace();
}

有人知道或使用过 James Java API 吗?有例子吗?

最佳答案

下面是等效的 java 代码:

public static void main(String [] args){

addUser(args[0]);
}

static void addUser(String email){
try{
String serverUrl = "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi";
String beanNameUser = "org.apache.james:type=component,name=usersrepository";
String beanNameDomain = "org.apache.james:type=component,name=domainlist";

MBeanServerConnection server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl)).getMBeanServerConnection();

UsersRepositoryManagementMBean userBean = MBeanServerInvocationHandler.newProxyInstance(server, new ObjectName(beanNameUser), UsersRepositoryManagementMBean.class, false);
DomainListManagementMBean domainBean = MBeanServerInvocationHandler.newProxyInstance(server, new ObjectName(beanNameDomain), DomainListManagementMBean.class, false);

if(domainBean.containsDomain(email.split("@")[1])
&& !userBean.verifyExists(email)){
System.out.println("creating email : "+email );
userBean.addUser(email,"password");
}else{
System.out.println("domain does not exist or user already exists !!");
}

}catch (Exception e){
System.out.println("Something went wrong");
}
}

关于java - 使用 java api 在 apache james 中创建用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26252000/

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