forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailImages.java
62 lines (51 loc) · 1.92 KB
/
MailImages.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
package io.vertx.example.mail;
import java.util.ArrayList;
import java.util.List;
import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;
import io.vertx.ext.mail.MailAttachment;
import io.vertx.ext.mail.MailClient;
import io.vertx.ext.mail.MailConfig;
import io.vertx.ext.mail.MailMessage;
/**
* send a mail with HTML and inline images
*
* @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a>
*/
public class MailImages extends AbstractVerticle {
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(MailImages.class);
}
@Override
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.start(2526);
MailClient mailClient = MailClient.createShared(vertx, new MailConfig().setPort(2526));
MailMessage email = new MailMessage()
.setFrom("[email protected] (Sender)")
.setTo("[email protected] (User Name)")
.setSubject("Test email")
.setText("full message is readable as html only")
.setHtml("visit vert.x <a href=\"http://vertx.io/\"><img src=\"cid:[email protected]\"></a>");
MailAttachment attachment = new MailAttachment()
.setData(vertx.fileSystem().readFileBlocking("logo-white-big.png"))
.setContentType("image/png")
.setName("logo-white-big.png")
.setDisposition("inline")
.addHeader("Content-ID", "<[email protected]>");
List<MailAttachment> list=new ArrayList<>();
list.add(attachment);
email.setInlineAttachment(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();
}
});
}
}