Repository navigation
Expand file tree
/
Copy pathSendEmail.java
More file actions
95 lines (88 loc) · 3.86 KB
/
Copy pathSendEmail.java
File metadata and controls
95 lines (88 loc) · 3.86 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Create a CII invoice and email it with the InvoiceXML API, in one call.
// Builds and validates the invoice exactly like POST /v1/create/cii, then
// sends it as an attachment from your own domain. This sends a real email.
// The answer is the message id of the email, not the file.
//
// Before the first run, add and verify your domain:
// https://www.invoicexml.com/account/sending-domains
// Your plan must include email delivery: https://www.invoicexml.com/pricing
//
// Get API key: https://www.invoicexml.com/account/authentication
// Docs: https://www.invoicexml.com/docs/api/create/cii/email
//
// Requires Java 15+ (uses text blocks).
// Dependency: com.squareup.okhttp3:okhttp:4.12.0
import okhttp3.*;
import java.time.Duration;
public class SendEmail {
public static void main(String[] args) throws Exception {
// Raw key only, without the "Bearer " prefix (it is added below).
String apiKey = "YOUR_API_KEY";
// Totals and the VAT breakdown are calculated from the line items.
// "from" is any address on your verified domain; send the first test to yourself.
// Optional: cc and bcc (2 addresses together) and replyTo.
String json = """
{
"invoice": {
"invoiceNumber": "CII-2026-001",
"issueDate": "2026-05-18",
"currency": "EUR",
"seller": {
"name": "Acme GmbH",
"vatIdentifier": "DE123456789",
"legalRegistration": { "identifier": "HRB 12345" },
"postalAddress": {
"line1": "Hauptstraße 12",
"city": "Berlin",
"postCode": "10115",
"country": "DE"
}
},
"buyer": {
"name": "Globex SAS",
"postalAddress": {
"line1": "15 rue de Rivoli",
"city": "Paris",
"postCode": "75001",
"country": "FR"
}
},
"paymentDetails": { "paymentAccountIdentifier": "DE89370400440532013000" },
"lines": [
{
"quantity": 10,
"priceDetails": { "netPrice": 150.00 },
"vatInformation": { "rate": 19.00 },
"item": { "name": "Senior consulting" }
}
]
},
"email": {
"from": "invoices@yourcompany.com",
"fromName": "Acme Billing",
"to": "you@yourcompany.com",
"subject": "Invoice CII-2026-001",
"message": "Hello,\\n\\nPlease find attached invoice CII-2026-001.\\n\\nKind regards"
}
}
""";
RequestBody body = RequestBody.create(json, MediaType.parse("application/json"));
Request request = new Request.Builder()
.url("https://api.invoicexml.com/v1/create/cii/email")
.header("Authorization", "Bearer " + apiKey)
.post(body)
.build();
// The API can take up to 90 seconds. With OkHttp's default 10 second read timeout this
// call could report a failure after the email went out, and a rerun would send it twice.
OkHttpClient client = new OkHttpClient.Builder().readTimeout(Duration.ofSeconds(120)).build();
try (Response response = client.newCall(request).execute()) {
String text = response.body().string();
if (!response.isSuccessful()) {
System.err.println("InvoiceXML API error " + response.code() + ": " + text);
System.exit(1);
}
// {"status":"accepted","messageId":"..."}
System.out.println("Email accepted for delivery: " + text);
}
}
}