Skip to content

Commit

Permalink
Initial version smtp,pop3 and imap
Browse files Browse the repository at this point in the history
  • Loading branch information
yersan committed Nov 15, 2023
1 parent faa9b7d commit b7e16f2
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 66 deletions.
14 changes: 14 additions & 0 deletions mail/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM linagora/james-jpa-sample:3.0.1

RUN chgrp -R 0 /root /logs /var && \
chmod -R g=u /root /logs /var && \
sed -i s/:143/:1143/g /root/conf/imapserver.xml && \
sed -i s/:993/:1993/g /root/conf/imapserver.xml && \
sed -i s/:110/:1110/g /root/conf/pop3server.xml && \
sed -i s/:25/:1025/g /root/conf/smtpserver.xml && \
sed -i s/:465/:1465/g /root/conf/smtpserver.xml && \
sed -i s/:587/:1587/g /root/conf/smtpserver.xml

RUN echo "touch /root/done.mark" >> /root/initialdata.sh

ENTRYPOINT ["/root/startup.sh"]
15 changes: 14 additions & 1 deletion mail/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,20 @@ include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]
[[configure_an_smtp_server_on_your_local_machine]]
== Configure an SMTP Server on Your Local Machine

This quickstart expects that you have an SMTP mail server running on your machine and configured for the default port `localhost:25`.
To run the Mail quickstart, we need an SMTP mail server running on your machine and configured for the port `localhost:1025`.
One way to accomplish it, is by starting a docker container. Execute the following command to have an Apache James Mail server configured with the expected ports used by this quickstart:

```

```



Kafka service we will use its Docker container

This quickstart expects that you have an SMTP mail server running on your machine and configured for the default port `localhost:1025`.


To configure an SMTP mail server, consult the documentation for your operating system. It is beyond the scope of this quickstart to provide these instructions.

If you do not configure an SMTP mail server on your local machine, you will see the exception `MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;` when you access the application and attempt to send an email.
Expand Down
10 changes: 5 additions & 5 deletions mail/configure-mail-session.cli
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ batch
/subsystem=mail/mail-session=MyOtherMail:add(jndi-name=java:jboss/mail/MyOtherMail,debug=true)

# Configure the custom SMTP, POP3, and IMAP socket binding groups
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-smtp-binding:add(host=localhost,port=25)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-pop3-binding:add(host=localhost,port=110)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-imap-binding:add(host=localhost,port=143)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-smtp-binding:add(host=localhost,port=1025)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-pop3-binding:add(host=localhost,port=1110)
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=my-imap-binding:add(host=localhost,port=1143)

# Add the custom socket binding groups to the custom MyOtherMail mail session
/subsystem=mail/mail-session=MyOtherMail/server=smtp:add(outbound-socket-binding-ref=my-smtp-binding,username=nobody,password=pass,tls=true)
/subsystem=mail/mail-session=MyOtherMail/server=smtp:add(outbound-socket-binding-ref=my-smtp-binding,username="[email protected]",password=1234)
/subsystem=mail/mail-session=MyOtherMail/server=pop3:add(outbound-socket-binding-ref=my-pop3-binding)
/subsystem=mail/mail-session=MyOtherMail/server=imap:add(outbound-socket-binding-ref=my-imap-binding,username=nobody,password=pass)
/subsystem=mail/mail-session=MyOtherMail/server=imap:add(outbound-socket-binding-ref=my-imap-binding, username="[email protected]",password=1234)

# Run the batch commands
run-batch
Expand Down
16 changes: 16 additions & 0 deletions mail/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
quickstart-mail-server:
image: "apache/james:demo-3.8.0"
container_name: "apache-james"
hostname: "james.local"
ports:
- "1465:465"
- "1993:993"
- "1025:25"
- "1110:110"
- "1587:587"
- "1143:143"
volumes:
- "./mail-server-conf/imapserver.xml:/root/conf/imapserver.xml"
- "./mail-server-conf/pop3server.xml:/root/conf/pop3server.xml"
- "./mail-server-conf/smtpserver.xml:/root/conf/smtpserver.xml"
17 changes: 17 additions & 0 deletions mail/mail-server-conf/imapserver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<imapservers>
<imapserver enabled="true">
<jmxName>imapserver</jmxName>
<bind>0.0.0.0:143</bind>
<connectionBacklog>200</connectionBacklog>
<tls socketTLS="false" startTLS="true">
<privateKey>file://conf/private.key</privateKey>
<certificates>file://conf/private.csr</certificates>
</tls>
<connectionLimit>0</connectionLimit>
<connectionLimitPerIP>0</connectionLimitPerIP>
<plainAuthDisallowed>false</plainAuthDisallowed>
<gracefulShutdown>false</gracefulShutdown>
</imapserver>
</imapservers>
19 changes: 19 additions & 0 deletions mail/mail-server-conf/pop3server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<pop3servers>
<pop3server enabled="true">
<jmxName>pop3server</jmxName>
<bind>0.0.0.0:110</bind>
<connectionBacklog>200</connectionBacklog>
<tls socketTLS="false" startTLS="true">
<privateKey>file://conf/private.key</privateKey>
<certificates>file://conf/private.csr</certificates>
</tls>
<connectiontimeout>1200</connectiontimeout>
<connectionLimit>0</connectionLimit>
<connectionLimitPerIP>0</connectionLimitPerIP>
<handlerchain>
<handler class="org.apache.james.pop3server.core.CoreCmdHandlerLoader"/>
</handlerchain>
</pop3server>
</pop3servers>
28 changes: 28 additions & 0 deletions mail/mail-server-conf/smtpserver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<smtpservers>
<smtpserver enabled="true">
<jmxName>smtpserver-global</jmxName>
<bind>0.0.0.0:25</bind>
<connectionBacklog>200</connectionBacklog>
<tls socketTLS="false" startTLS="true">
<privateKey>file://conf/private.key</privateKey>
<certificates>file://conf/private.csr</certificates>
</tls>
<connectiontimeout>360</connectiontimeout>
<connectionLimit>0</connectionLimit>
<connectionLimitPerIP>0</connectionLimitPerIP>
<auth>
<announce>never</announce>
<requireSSL>false</requireSSL>
</auth>
<verifyIdentity>false</verifyIdentity>
<maxmessagesize>0</maxmessagesize>
<addressBracketsEnforcement>true</addressBracketsEnforcement>
<smtpGreeting>Apache JAMES awesome SMTP Server</smtpGreeting>
<handlerchain>
<handler class="org.apache.james.smtpserver.fastfail.ValidRcptHandler"/>
<handler class="org.apache.james.smtpserver.CoreCmdHandlerLoader"/>
</handlerchain>
</smtpserver>
</smtpservers>
4 changes: 3 additions & 1 deletion mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
</dependencyManagement>

<dependencies>

<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
Expand Down Expand Up @@ -158,6 +157,9 @@
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.plugin.wildfly}</version>
<configuration>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Expand Down
182 changes: 160 additions & 22 deletions mail/src/main/java/org/jboss/as/quickstarts/mail/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@
*/
package org.jboss.as.quickstarts.mail;

import java.io.Serializable;

import jakarta.annotation.Resource;
import jakarta.enterprise.context.SessionScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import jakarta.mail.Address;
import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Store;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.io.Serializable;

/**
* <p>
* {@link Email} contains all the business logic for the application, and also serves as the controller for the JSF view.
Expand All @@ -36,15 +41,17 @@
* It contains address, subject, and content for the <code>email</code> to be sent.
* </p>
* <p>
* The {@link #send()} method provides the business logic to send the email
* The {@link #send()} method provides the business logic to send the email.
* The {@link #retrievePop3()} method retrieves all the emails sent to a specific email account using POP3.
* The {@link #retrieveImap()} method retrieves all the emails sent to an account whose credentials are configured in
* the Mail subsystem using IMAP.
* </p>
*
* @author Joel Tosi
*
*/

@Named
@SessionScoped
@ViewScoped
public class Email implements Serializable {

private static final long serialVersionUID = 1544680932114626710L;
Expand All @@ -56,14 +63,129 @@ public class Email implements Serializable {
@Resource(mappedName = "java:jboss/mail/MyOtherMail")
private Session mySession;

private String to;
private String to = "[email protected]";

private String from;
private String from = "[email protected]";

private String subject;

private String body;

private String pop3User = "[email protected]";
private String pop3Password = "1234";
private String pop3Emails;

private String imapEmails;

/**
* Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
* is not handled in this example.
*/
public void send() {
try {
Message message = new MimeMessage(mySession);
message.setFrom(new InternetAddress(from));
Address toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setContent(body, "text/plain");
Transport.send(message);

FacesContext context = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("Email sent to " + to);
context.addMessage(null, facesMessage);
} catch (Exception e) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("Error sending the Email. " + e.getMessage());
context.addMessage(null, facesMessage);
}
}

public void resetSmtp() {
from = "[email protected]";
to = "[email protected]";
subject = null;
body = null;
}

public void retrievePop3() throws Exception {
try {
pop3Emails = retrieveEmails("pop3", pop3User, pop3Password);
} catch (Exception e) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("Error retrieving emails using Pop3. " + e.getMessage());
context.addMessage(null, facesMessage);
}
}

public void resetPop3() {
pop3User = "[email protected]";
pop3Password = "1234";
pop3Emails = null;
}

public void retrieveImap() throws Exception {
try {
imapEmails = retrieveEmails("imap");
} catch (Exception e) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("Error retrieving emails using Pop3. " + e.getMessage());
context.addMessage(null, facesMessage);
}
}

public void resetImap() {
imapEmails = null;
}

private String retrieveEmails(String protocol) throws MessagingException {
return retrieveEmails(protocol, null, null);
}

private String retrieveEmails(String protocol, String user, String password) throws MessagingException {
Store store = mySession.getStore(protocol);
if (user != null && !user.trim().isEmpty()) {
store.connect(user, password);
} else {
// Users the credentials configured in the Mail Subsystem
store.connect();
}

Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);

// get the list of inbox messages
Message[] messages = inbox.getMessages();

if (messages.length == 0) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("No message found for " + user);
context.addMessage(null, facesMessage);

return null;
}

StringBuilder sb = new StringBuilder("Emails retrieved via ").append(protocol).append("\n");
for (int i = 0; i < messages.length; i++) {
// stop after listing ten messages
if (i > 10) {
inbox.close(true);
store.close();
}

sb.append("Message ").append((i + 1)).append("\n");
sb.append("From : ").append(messages[i].getFrom()[0]).append("\n");
sb.append("Subject : ").append(messages[i].getSubject()).append("\n");
sb.append("Sent Date : ").append(messages[i].getSentDate()).append("\n");
sb.append("----------------------------------").append("\n");
}

inbox.close(true);
store.close();

return sb.toString();
}

public String getTo() {
return to;
}
Expand Down Expand Up @@ -96,19 +218,35 @@ public void setBody(String body) {
this.body = body;
}

/**
* Method to send the email based upon values entered in the JSF view. Exception should be handled in a production usage but
* is not handled in this example.
*
* @throws Exception
*/
public void send() throws Exception {
Message message = new MimeMessage(mySession);
message.setFrom(new InternetAddress(from));
Address toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setContent(body, "text/plain");
Transport.send(message);
public String getPop3User() {
return pop3User;
}

public void setPop3User(String pop3User) {
this.pop3User = pop3User;
}

public String getPop3Password() {
return pop3Password;
}

public void setPop3Password(String pop3Password) {
this.pop3Password = pop3Password;
}

public String getPop3Emails() {
return pop3Emails;
}

public void setPop3Emails(String pop3Emails) {
this.pop3Emails = pop3Emails;
}

public String getImapEmails() {
return imapEmails;
}

public void setImapEmails(String imapEmails) {
this.imapEmails = imapEmails;
}
}
1 change: 0 additions & 1 deletion mail/src/main/webapp/WEB-INF/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Marker file indicating CDI should be enabled -->
<beans xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
bean-discovery-mode="all">
Expand Down
Loading

0 comments on commit b7e16f2

Please sign in to comment.