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.
I'm guessing you are talking about this tutorial to configure the sending of mails through Compute Engine (which explains why it works well on your VM instance).
That tutorial is for Compute Engine, in the case of App Engine Standard applications, you have the option of using Mail API, however, since Google is no longer accepting quota increases for that API, is advised to use a third-party service such as MailGun or SendGrid.
To connect your App Engine standard application (with Java runtime) with those third-party services you will need to specify the "dependicies" on your "pom.xml" file and modify your "app.yaml" with the API Key:
The Email API Key will be provided by the third-party service, for example, in the case of MailGun, their documentation specifies the following:
For the step by step instructions on how to configure your App Engine Standard App to connect with these third-party services, please refer to this documentation.
EDIT:
It looks like in your case, you could be using another dependencies (hence, the dependency error). When you created the code did you refereed to the App Engine Java 8 documentation samples or to the Mailgun samples? because I noticed they use different dependencies, and also the App Engine sample uses env variables (which according to the snippet code you shared, you are not using).
My advice is to follow the sample given for Google App Engine Standard Java 8.
These are the steps I took to get the sample successfully up and running on App Engine:
1. Downloaded the sample code.
2. Created an account on Mailgun (simple account, not credit data attached). You will need to verify your account through the specified email.
3. Once your are logged inside MailGun, you will see a menu on the left, navigate to Sending > Overview page.
4. At the right you will see an "Authorized Recipients" box, add the email or the person that will receive the email, they will need to confirm also on their side, so try adding an email on which you have access for the sake of the test. Note: you need to do this if you haven't verified your domain.
5. Copy the value of the domain you have for default (or the one you have verified).
6. On the same box there's a menu with the link to "Api Keys", click on it and copy the key under "HTTP webhook signing key" (this is also your private key, so be careful where you put it).
7. Head to the GCP sample that was downloaded, and edit the file "/mailgun/src/main/webapp/WEB-INF/appengine-web.xml" with those values (domain and key).
8. If you are using Maven run the commands:
or
note: some of these steps might have look rather obvious for you but I have explained it this way so other people can follow it as well.