Send an Email from my Account to a Specified Address

21 views
Skip to first unread message

Ed Betteridge

unread,
Jan 31, 2016, 1:33:50 PM1/31/16
to Google API JavaScript Client
I'd like to know how (if possible) to allow the user of my JavaScript web app to input their email address, and have some data generated from my app sent to them.

I've set up an API Key, OAuth Client, and Service Account in Google Dev. Whichever of these I need to use, I have no need for any authorization prompts to appear to the user, as I won't be accessing any of their personal data. I do however need to (presumably) programatically authorize my own account to allow my web app to send an email from it.

I have done some experimentation with GAPI already, and have succeeded in getting emails sent using a few methods, but I am still very new to even using APIs in general. 

Apologies if this is already a worn out topic.

- Thanks for reading. Ed

Renaud Tarnec

unread,
Feb 1, 2016, 2:21:51 PM2/1/16
to Google API JavaScript Client
Hi Ed,

I am not 100% sure to understand what you want to do, but if you want your application users to enter their e-mail address in a form, then click a "send" button and have your AppEngine backend fecthing data from Cloud datastore and send an email with this data to the user's e-mail address you can do it by running a background task using queues, as follow:

1/Write a servlet that will send the mail (i.e. write the code to perform the task.). Example:

public class SendConfirmationEmailServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String email = request.getParameter("email");   //User's email
              
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(“your...@gmail.com", “Your Name“));   
            msg.addRecipient(
        Message.RecipientType.TO,
                new InternetAddress(email, “User Name“));
            msg.setSubject(“Subject”);

            String htmlBody = “Hi” + e.g. some data from the datastore through Objectify for instance;
            Multipart mp = new MimeMultipart();
            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(htmlBody, "text/html");msg.setContent(mp);
            Transport.send(msg);
        } catch (....) {
              ...
        }
    }
}


2/ Configure the queue in queue.xml  See https://cloud.google.com/appengine/docs/java/taskqueue/

3/ Add the task to the the queue from another Servlet or an Endpoint , like

queue.add(TaskOptions.Builder.withURL(URL_for_task).
    param(param1).param(param2);

4/ From you HTML front end, through the JavaScript Library call your Servlet or Endpoint with the desired parameters.

Tell me if this is what you are looking for: If yes I could describe the process with much more details and/or point you to some examples.

Hope this helps.
Renaud

Renaud Tarnec

unread,
Feb 1, 2016, 3:00:39 PM2/1/16
to Google API JavaScript Client
Hi again,

I've just noticed that I mixed up the "App Engine" Group with the "API JavaScript Client" one :-) So maybe my response with AppEngine Servlets/Endpoints is totally off-topic!

Renaud


On Monday, February 1, 2016 at 8:21:51 PM UTC+1, Renaud Tarnec wrote:
Hi Ed,

I am not 100% sure to understand what you want to do, but if you want your application users to enter their e-mail address in a form, then click a "send" button and have your AppEngine backend fecthing data from Cloud datastore and send an email with this data to the user's e-mail address you can do it by running a background task using queues, as follow:

1/Write a servlet that will send the mail (i.e. write the code to perform the task.). Example:

public class SendConfirmationEmailServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String email = request.getParameter("email");   //User's email
              
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(“yourmail@gmail.com", “Your Name“));   

Ed Betteridge

unread,
Feb 1, 2016, 3:19:24 PM2/1/16
to Google API JavaScript Client
Hi Renaud, I appreciate the detailed response, but unfortunately, yes, it wasn't quite what I was looking for.

However, you have given me a few ideas of things I should clarify about my original post.

A simplified example of what I'm trying to achieve:
  • User navigates to the webpage.
  • Inputs text.
  • Inputs their email address.
  • Presses send.
  • An email appears in their inbox from my email address, with the specified text.
I realize that to achieve this, I may need to enter login/private keys into the JavaScript. But I am okay with this, as the account is only temporary, and the project is for educational purposes.
(One layer of defense might be the locations that are allowed to send requests, but I know this is only a superficial protection).

I must also stress that the solution CANNOT include any server-side code at my end.

I have had success with EmailJS, but I'd like to allow myself more freedom by controlling the process myself (e.g. remove 200 email limit, branding etc.).

- Thanks, Renaud, and any other readers :) . Ed

Reply all
Reply to author
Forward
0 new messages