Skip to content

Commit

Permalink
Improve mail client examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 19, 2024
1 parent afe0e6d commit fdbf454
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.vertx.example.mail;

import org.apache.commons.io.IOUtils;
import org.subethamail.smtp.auth.LoginFailedException;
import org.subethamail.smtp.auth.PlainAuthenticationHandlerFactory;
import org.subethamail.smtp.auth.UsernamePasswordValidator;
import org.subethamail.smtp.helper.SimpleMessageListener;
import org.subethamail.smtp.helper.SimpleMessageListenerAdapter;
import org.subethamail.smtp.server.SMTPServer;

import java.io.IOException;
import java.io.InputStream;

/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
Expand All @@ -30,4 +35,18 @@ public static void startWithAuth(int port) {
server.start();
}

private static class MyMessageListener implements SimpleMessageListener {


@Override
public boolean accept(String from, String recipient) {
return true;
}

@Override
public void deliver(String from, String recipient, InputStream data) throws IOException {
System.out.println("Sending mail from " + from + " to " + recipient
+ " (size: " + IOUtils.toByteArray(data).length + " bytes)");
}
}
}
28 changes: 14 additions & 14 deletions mail-examples/src/main/java/io/vertx/example/mail/MailHeaders.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.mail;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
import io.vertx.ext.mail.MailMessage;
Expand All @@ -17,20 +18,24 @@
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailHeaders extends AbstractVerticle {
public class MailHeaders extends VerticleBase {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
VertxApplication.main(new String[]{MailHeaders.class.getName()});
}

public void start() {
// Start a local SMTP server, remove this line if you want to use your own server.
// It just prints the sent message to the console
LocalSmtpServer.start(2528);

VertxApplication.main(new String[]{MailHeaders.class.getName()});
}

private MailClient mailClient;

public Future<?> start() {
MailConfig mailConfig = new MailConfig().setHostname("localhost").setPort(2528);

MailClient mailClient = MailClient.createShared(vertx, mailConfig);
mailClient = MailClient.createShared(vertx, mailConfig);

MailMessage email = new MailMessage()
.setFrom("user1@example.com")
Expand All @@ -42,16 +47,11 @@ public void start() {
.addHeader("Received", "from [192.168.1.1] by localhost")
.setText("This message should have a custom Message-ID");

mailClient
return mailClient
.sendMail(email)
.onComplete(result -> {
if (result.succeeded()) {
System.out.println(result.result());
.onSuccess(result -> {
System.out.println(result);
System.out.println("Mail sent");
} else {
System.out.println("got exception");
result.cause().printStackTrace();
}
});
}

Expand Down
32 changes: 16 additions & 16 deletions mail-examples/src/main/java/io/vertx/example/mail/MailImages.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.mail;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.mail.MailAttachment;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
Expand All @@ -15,19 +16,21 @@
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailImages extends AbstractVerticle {
public class MailImages extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{MailImages.class.getName()});
}

@Override
public void start() {
// Start a local SMTP server, remove this line if you want to use your own server.
// It just prints the sent message to the console
LocalSmtpServer.start(2526);

MailClient mailClient = MailClient.createShared(vertx, new MailConfig().setPort(2526));
VertxApplication.main(new String[]{MailImages.class.getName()});
}

private MailClient mailClient;

@Override
public Future<?> start() {
mailClient = MailClient.createShared(vertx, new MailConfig().setPort(2526));

MailMessage email = new MailMessage()
.setFrom("user@example.com (Sender)")
Expand All @@ -47,14 +50,11 @@ public void start() {
list.add(attachment);
email.setInlineAttachment(list);

mailClient.sendMail(email).onComplete(result -> {
if (result.succeeded()) {
System.out.println(result.result());
System.out.println("Mail sent");
} else {
System.out.println("got exception");
result.cause().printStackTrace();
}
return mailClient
.sendMail(email)
.onSuccess(result -> {
System.out.println(result);
System.out.println("Mail sent");
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.mail;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
import io.vertx.ext.mail.MailMessage;
Expand All @@ -13,19 +14,21 @@
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailLocalhost extends AbstractVerticle {
public class MailLocalhost extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{MailLocalhost.class.getName()});
}

@Override
public void start() {
// Start a local SMTP server, remove this line if you want to use your own server.
// It just prints the sent message to the console
LocalSmtpServer.start(2525);

MailClient mailClient = MailClient.createShared(vertx, new MailConfig().setPort(2525));
VertxApplication.main(new String[]{MailLocalhost.class.getName()});
}

private MailClient mailClient;

@Override
public Future<?> start() {
mailClient = MailClient.createShared(vertx, new MailConfig().setPort(2525));

MailMessage email = new MailMessage()
.setFrom("user@example.com (Sender)")
Expand All @@ -36,15 +39,12 @@ public void start() {
.setSubject("Test email")
.setText("this is a test email");

mailClient.sendMail(email).onComplete(result -> {
if (result.succeeded()) {
System.out.println(result.result());
return mailClient
.sendMail(email)
.onSuccess(result -> {
System.out.println(result);
System.out.println("Mail sent");
} else {
System.out.println("got exception");
result.cause().printStackTrace();
}
});
});
}

}
28 changes: 14 additions & 14 deletions mail-examples/src/main/java/io/vertx/example/mail/MailLogin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.mail;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.mail.*;
import io.vertx.launcher.application.VertxApplication;
Expand All @@ -15,18 +16,23 @@
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailLogin extends AbstractVerticle {
public class MailLogin extends VerticleBase {

public static void main(String[] args) {
// Start a local SMTP server, remove this line if you want to use your own server.
// It just prints the sent message to the console
LocalSmtpServer.startWithAuth(5870);

VertxApplication.main(new String[]{MailLogin.class.getName()});
}

public void start() {
private MailClient mailClient;

public Future<?> start() {
// Start a local SMTP server, remove this line if you want to use your own server.
// It just prints the sent message to the console
LocalSmtpServer.startWithAuth(5870);


MailConfig mailConfig = new MailConfig()
.setHostname("localhost")
.setPort(5870)
Expand All @@ -36,7 +42,7 @@ public void start() {
.setUsername("username")
.setPassword("password");

MailClient mailClient = MailClient.createShared(vertx, mailConfig);
mailClient = MailClient.createShared(vertx, mailConfig);

Buffer image = vertx.fileSystem().readFileBlocking("io/vertx/example/mail/logo-white-big.png");

Expand All @@ -61,17 +67,11 @@ public void start() {

email.setAttachment(list);

mailClient
return mailClient
.sendMail(email)
.onComplete(result -> {
if (result.succeeded()) {
System.out.println(result.result());
.onSuccess(result -> {
System.out.println(result);
System.out.println("Mail sent");
} else {
System.out.println("got exception");
result.cause().printStackTrace();
}
});
}

}

0 comments on commit fdbf454

Please sign in to comment.