I have followed through the tutorials and set up the GCP firewalls for both ingress and outgress on the port 2525. Running the code locally on my machine successfully sends the email however, deploying the project to App Engine Standard (Java runtime) does not throw an error but also does not send the email.
I also have a GCP VM instance that is also able to send the email. Does anyone know what could be causing this?
Dependencies : Dependencies
String email = "target@email.com";
String API_KEY = "KEY";
HttpResponse<JsonNode> req = Unirest.post("https://api.mailgun.net/v3/" + "my.custom.domain" + "/messages")
.basicAuth("api", API_KEY)
.field("from","Admin <admin@my.custom.domain>")
.field("to", email)
.field("subject","Welcome!")
.field("text", "testing")
.asJson();
req.getBody();
throws :
Exception in thread "main" java.lang.ExceptionInInitializerError
at tech.incineratez.email.Main.main(Main.java:14)
Caused by: kong.unirest.UnirestException: It looks like you are using an
older version of Apache Http Client.
For security and performance reasons Unirest requires the most recent
version. Please upgrade.
at kong.unirest.Config.setDefaults(Config.java:109)
at kong.unirest.Config.<init>(Config.java:85)
at kong.unirest.Unirest.<clinit>(Unirest.java:30)
... 1 more
Caused by: java.lang.BootstrapMethodError: java.lang.IllegalAccessError:
no such constructor: kong.unirest.apache.ApacheAsyncClient.<init>
(Config)void/newInvokeSpecial
at kong.unirest.Config.setDefaults(Config.java:106)
... 3 more
Caused by: java.lang.IllegalAccessError: no such constructor:
kong.unirest.apache.ApacheAsyncClient.<init>(Config)void/newInvokeSpecial
at
java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:483)
... 4 more
Caused by: java.lang.NoClassDefFoundError:
org/apache/http/nio/reactor/ConnectingIOReactor
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1394)
at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1750)
at
java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:477)
... 4 more
Caused by: java.lang.ClassNotFoundException: org.apache.http.nio.reactor.ConnectingIOReactor
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
Working code if ran seperately on my computer and not on the Google Cloud Platform :
Properties props = System.getProperties();
props.put("mail.smtps.host", "smtp.mailgun.org");
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", "2525");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admin@my.custom.domain"));
InternetAddress[] addrs = InternetAddress.parse(email, false);
msg.setRecipients(Message.RecipientType.TO, addrs);
msg.setSubject("Welcome!");
msg.setText("Test");
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
t.connect("smtp.mailgun.org", "postmaster@my.custom.domain", "KEY");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Email sent" + t.getLastServerResponse());
t.close();
Dependencies : Dependencies2
The code above when ran on the Google Cloud Platform does nothing at all.