forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailLogin.java
81 lines (66 loc) · 2.44 KB
/
MailLogin.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package io.vertx.example.mail;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.buffer.Buffer;
import io.vertx.example.util.Runner;
import io.vertx.ext.mail.LoginOption;
import io.vertx.ext.mail.MailAttachment;
import io.vertx.ext.mail.MailConfig;
import io.vertx.ext.mail.MailMessage;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.StartTLSOptions;
import java.util.ArrayList;
import java.util.List;
/**
* send a mail via a smtp server requiring TLS and Login we use an attachment and a text/html alternative mail body
* <p>
* Please put in your actual mail server and account to run this example
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailLogin extends AbstractVerticle {
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(MailLogin.class);
}
public void start() {
// Start a local STMP 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)
//.setStarttls(StartTLSOptions.REQUIRED)
.setLogin(LoginOption.REQUIRED)
.setAuthMethods("PLAIN")
.setUsername("username")
.setPassword("password");
MailClient mailClient = MailClient.createShared(vertx, mailConfig);
Buffer image = vertx.fileSystem().readFileBlocking("logo-white-big.png");
MailMessage email = new MailMessage()
.setFrom("user1@example.com")
.setTo("user2@example.com")
.setCc("user3@example.com")
.setBcc("user4@example.com")
.setBounceAddress("bounce@example.com")
.setSubject("Test email with HTML")
.setText("this is a message")
.setHtml("<a href=\"http://vertx.io\">vertx.io</a>");
List<MailAttachment> list = new ArrayList<MailAttachment>();
list.add(new MailAttachment()
.setData(image)
.setName("logo-white-big.png")
.setContentType("image/png")
.setDisposition("inline")
.setDescription("logo of vert.x web page"));
email.setAttachment(list);
mailClient.sendMail(email, result -> {
if (result.succeeded()) {
System.out.println(result.result());
System.out.println("Mail sent");
} else {
System.out.println("got exception");
result.cause().printStackTrace();
}
});
}
}