Send an attach mail from Google Cloud Storage using App Enging Java Client

2,326 views
Skip to first unread message

Snahasish Ghosh

unread,
Oct 19, 2015, 6:25:10 AM10/19/15
to Google App Engine, snah...@adasoftware.com

Hi Google Cloud Support

I want your suggestion for sending mail with attachments from Google Cloud Storage.

Currently I upload a file(.mp3) in Google Cloud Storage within the specific bucket using App Engine Java Client.

I want to send this file (.mp3) as an attachment with the mail and mail will be send from another Java Web Application which is connected to Google Cloud Storage using App Engine Java Client.

As mentioned by Nick in an earlier post, I go through SendGrid documentation but can't find a suitable documentation about how to attach an file with mail and send.

Can you please help me what should be the best way ?

-Snahasish

Alejandro Gonzalez

unread,
Oct 19, 2015, 6:40:33 AM10/19/15
to Google App Engine, snah...@adasoftware.com
Hello Snahasish,

You are having troubles with which step? download a file stored in GCS or attaching the file through the SendGrid API? Are you using any library to interact with SendGrid (https://github.com/sendgrid/sendgrid-java)?

If you're using the SendGrid library have you tried attaching the InputStream as shown here? https://github.com/sendgrid/sendgrid-java#attachments 


I'm doing exactly the same but with another mail provider instead of SendGrid. I'm using jersey with FormDataMultiPart and StreamDataBodyPart classes to send the request with the attachment to the API (with no specific client library, just Jersey)


FormDataMultiPart formData = new FormDataMultiPart();
formData.field("from", emailFrom);
formData.field("subject", subject);
formData.field("html", content);
formData.field("to", email);

StreamDataBodyPart bodyPart = new StreamDataBodyPart("inline", imageInputStream,filename,new MediaType("image","png"));
formData.bodyPart(bodyPart);

Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api", API_KEY));
WebResource webResource = client.resource(API_ENDPOINT + "/messages");
webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, formData);

Let me know if this is what you are looking for.

Cheers.

Snahasish Ghosh

unread,
Oct 19, 2015, 7:45:24 AM10/19/15
to Google App Engine, snah...@adasoftware.com

Hi Alejandro

Thanks for your reply.

Thanks for your valuable time that you have spent here. I hope you will spent some more time to solve my problem.

Before going to write the code I am going to describe you what it is toady and what I want to achieve.

I have an java web application which run on a different server (e.g Tomcat).
All the media files (.mp3) which are uploaded to Google cloud Storage,are present in that server and easily assessable from server side code from my web application.
When user click on a button in my application, my web app first search for that media file in specific path and if found then web app try to connect Google Cloud Storage with java client and upload this media file to the specific bucket.

I want to send this media file to the users from Google Cloud Storage using SendGrid.

This is what I needed.

I go through the link you have suggested.

If I am not wrong, this attachments are just read the file from specific path (in my case server side) as Input stream and send it through the mail. no need to download this file from Google Cloud Storage. It is not dependent on SendGrid or jurssy. Just upload to GCS and send it from the local path.

Can you please clarify.

-Snahasish

Alejandro Gonzalez

unread,
Oct 19, 2015, 9:03:34 AM10/19/15
to Google App Engine, snah...@adasoftware.com
Hello, to summarize:

  • You have a Tomcat server with files in the local file system.
  • On a user action, you want to:
    • Upload a specific file to a specific bucket in GCS
    • Send that file as an attachment to a user by email using SendGrid
If this is your scenario I don't know where is the problem. You have said that the files are easily accessible from you server code. Using the oficial SendGrid client library should be straightforward to send a file as an attachment (https://github.com/sendgrid/sendgrid-java#attachments).

What I don't understand is why you want to upload that file to GCS... maybe you want to generate a link for that file instead of attaching the entire file to an email? You don't need to upload the file to GCS for being able to attach it to an email...

Snahasish Ghosh

unread,
Oct 19, 2015, 9:47:27 AM10/19/15
to Google App Engine, snah...@adasoftware.com
Hi Alejandro

Thanks for your reply.

This is the exact path that you describe.

Actually GCS is our client requirement and we have to do that. Sending mail for GCS is another mandatory requirement and we are trying to do that.

I was in little bit confusion about what does it mean when it say that Sending Mail from GCS.

From your comment it seems to me that I can send mail from my web app without connecting to GCS with attaching file from local system.

Can you please tell me what is the maximum limit of attach file size in a mail in sendgrid.

-Snahasish

Alejandro Gonzalez

unread,
Oct 19, 2015, 10:08:02 AM10/19/15
to Google App Engine, snah...@adasoftware.com
Hello,

To clarify: You can not send an email from Google Cloud Storage. GCS is an object storage service. You can store files, download, create links for files stored in there etc...

What make sense to me is to not send the mp3 as an attachment, but instead send the email with a link to the mp3 file stored in your Google Cloud Storage. 

Anyway and answering your last question: https://sendgrid.com/docs/Classroom/Build/attachments.html you can find info about attachments and best practices with SendGrid in their official documentation...

PS -> these are the AppEngine forums and it seem that your questions has nothing to do with Google App Engine :D for further support it might be worth to post your question on Stack Overflow or even in Send Grid support site...

Cheers!

Snahasish Ghosh

unread,
Oct 19, 2015, 10:14:59 AM10/19/15
to Google App Engine, snah...@adasoftware.com

Thanks Alejandro

Thanks for your valuable comment and advise.

-Snahasish
Reply all
Reply to author
Forward
0 new messages