-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
MailHeaders.java
58 lines (46 loc) · 1.99 KB
/
MailHeaders.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package io.vertx.example.mail;
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;
import io.vertx.launcher.application.VertxApplication;
import java.util.Arrays;
/**
* send a mail via a smtp service with a few headers to the mail (this can be used for example for mail api
* headers e.g. from mailgun or sendgrid or to add Reply-To and custom Received headers)
* <p>
* you can either supply all headers (setFixedHeaders(true)) or give a set of headers to be set over the headers that
* are set for the mime message (Message-ID, From, To etc)
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailHeaders extends VerticleBase {
// Convenience method so you can run it in your IDE
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.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.createShared(vertx, mailConfig);
MailMessage email = new MailMessage()
.setFrom("[email protected]")
.addHeader("X-Mailer", "Vert.x Mail-Client 5.0.0.CR1")
.addHeader("Message-ID", "[email protected]")
.addHeader("Reply-To", "[email protected]")
.addHeader("Received", "by vertx mail service")
.addHeader("Received", "from [192.168.1.1] by localhost")
.setText("This message should have a custom Message-ID");
return mailClient
.sendMail(email)
.onSuccess(result -> {
System.out.println(result);
System.out.println("Mail sent");
});
}
}