Coder Social home page Coder Social logo

sendgrid-google-java's Introduction

Sendgrid-google-java

This library allows you to quickly and easily send emails from Google App Engine through SendGrid using Java.

License

Licensed under the MIT License.

Downloading

Installing the SendGrid package is as simple as adding it to your project's include path. If you're using git, you can just clone down the repo like this:

git clone [email protected]:sendgrid/sendgrid-google-java.git

Note: If you don't have git or would rather install by unpacking a Zip or Tarball, you can always grab the latest version of the package from the downloads page.

SendGrid API

SendGrid provides SendGrid library for sending email.

Mail Pre-Usage

Before we begin using the library, its important to understand a few things about the library architecture...

  • The SendGrid object is the means of setting mail data. In general, data can be set in three ways for most elements:

    1. set - reset the data, and initialize it to the given element. This will destroy previous data
    2. set (List) - for array based elements, we provide a way of passing the entire array in at once. This will also destroy previous data.
    3. add - append data to the list of elements.
  • Sending an email is as simple as :

    1. Creating a SendGrid object, and setting its data
    2. Sending the mail.

Mail Usage

To begin using this library, you must first include it

import packageName.Sendgrid;

Then, initialize the SendGrid object with your SendGrid credentials

Sendgrid mail = new Sendgrid("<sendgrid_username>","<sendgrid_password>");

Headers are enabled by default. If you do not want to use headers, the use_headers variable must be set to false

mail.use_headers = false;

Create a new SendGrid object and add your message details

mail.setTo("[email protected]")
    .setFrom("[email protected]")
    .setSubject("Subject goes here")
    .setText("Hello World!")
    .setHtml("<strong>Hello World!</strong>");

Send the email

mail.send();

Using Categories

You can mark messages with optional categories to give better visibility to email statistics (opens, clicks, etc.). You can add up to 10 categories per email message. You can read more about Categories here: http://docs.sendgrid.com/documentation/delivery-metrics/categories/

To add categories to your message, use the mail.addCategory() method and pass a category as parameter or mail.setCategories() and pass a list of category names. SendGrid will begin tracking statistics with these category names if the category name is new, or aggregate statistics for existing category names.

mail.addTo("[email protected]")
    ...
    .addCategory("Category 1")
    .addCategory("Category 2");
mail.addTo("[email protected]")
    ...
    .setCategories(new String[]{"Category 1","Category 2","Category 3"});

Using Substitutions

SendGrid also allows you to send multi-recipient messages with unique information per recipient. This is commonly used for sending unique URLs or codes to a list of recipients in a single batch. You can read more about Substitutions here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/substitution-tags/

mail.addTo("[email protected]")
    .addTo("[email protected]")
    .addTo("[email protected]")
    ...
    .setHtml("Hey %name%, we've seen that you've been gone for a while")
    .addSubstitution("%name%", new String[]{"John", "Harry", "Bob"});

Using Sections

Used in conjunction with Substitutions, Sections can be used to further customize messages for the end users, and acts like a second tier of substitution data. You can use mail.addSection() to add a single section, or mail.setSections() method to add several sections. You can read more about using Sections here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/section-tags/

mail.addTo("[email protected]")
    .addTo("[email protected]")
    .addTo("[email protected]")
    ...
    .setHtml("Hey %name%, you work at %place%")
    .addSubstitution("%name%", new String[]{"John", "Harry", "Bob"})
    .addSubstitution("%place%", new String[]{"%office%", "%office%", "%home%"})
    .addSection("%office%", "an office")
    .addSection("%home%", "your house");

Using Unique Arguments

Unique Arguments are used for tracking purposes on the message, and can be seen in the Email Activity screen on your account dashboard or through the Event API. Use the mail.addUniqueArgument() method, which takes two parameters, a key and a value. To pass multiple keys/values, use mail.setUniqueArguments() and pass a dictionary of key/value pairs. More information can be found here: http://docs.sendgrid.com/documentation/api/smtp-api/developers-guide/unique-arguments/

mail.addTo("[email protected]")
    ...
    .addUniqueArgument("Customer", "Someone")
    .addUniqueArgument("location", "Somewhere");

Using Filter Settings

Filter Settings are used to enable and disable apps, and to pass parameters to those apps. You can read more here: http://docs.sendgrid.com/documentation/api/smtp-api/filter-settings/ Here's an example of passing content to the 'footer' app:

mail.addTo("[email protected]")
    ...
    .addFilterSetting("footer", "enable", "1")
    .addFilterSetting("footer", "text/plain", "Here is a plain text footer")
    .addFilterSetting("footer", "text/html", "<p style='color:red;'>Here is an HTML footer</p>");

Using Bcc

Bcc is used to send a blind carbon copy to an address. Standard setBcc will hide who the email is addressed to. This is by design. Additionally, it is a good idea to use multiple addTos instead addBcc(this is currently not supported) and each user will receive a personalized email showing only their email.

mail.setBcc("[email protected]")

Notes:

  • addBcc() was removed because is currently not supported.

sendgrid-google-java's People

Contributors

awwa avatar danielamihalache avatar laur-craciun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sendgrid-google-java's Issues

BCC isn't working

Calling addBcc doesn't seem to do anything. I couldn't find a reason for this though, initially it looked like the substring is wrong here:
request += "?" +this._arrayToUrlPart(this.getBccs(), "bcc").substring(1);

But changing this didn't fix the problem.

Missing HTML email entry triggers a null pointer exception

I tried creating an email without any HTML since I have the Email template switched on (not sure how this works so I might have gotten it wrong).
Got a null pointer exception in the paramIterator loop in the send method from inside the encode method pretty sure it was this invocation: requestParams.append(URLEncoder.encode(value, "UTF-8")); meaning that value was null.

I think the right fix will be just changing this in _prepMessageData():
params.put("html", this.getHtml());

To:
if(getHtml() != null) {
params.put("html", this.getHtml());
}

I made that change and it seemed to fix this issue.

When adding BCC destination the TO field is empty

I'm not exactly sure if I'm doing this right but the idea is to provide automation for a "personal" email. So I'm bcc'ing myself on outgoing emails to know that I sent a welcome email to person X.
However, the email that I'm receiving has no indication to whom it was sent.

Is there a way to add a "proper" bcc in sendgrid?

Is is possible to send attachments using sendgrid-google_java

I am trying to send mails with transactional templates with attachments . Will it be supported by sendgrid-google_java. I have another issue too. I could not send email with UTF-8 characterset through google app engine. This issue has been resolved in send grid web version 3 api. Could you please fix this?

addTo(String email, String name) doesn't seem to work

Initially I tried to fix this by adding a space on the first line here:
String toAddress = (name.length() > 0) ? name + "<" + email + ">" : email;

So there is a space between the name and the less than character.
However, the server still returns errors for invalid email address. I'm assuming that the toname argument needs to be used rather than this approach.

HTTP url is used for sending

Does that make a sensible default over using https?
I know email is inherently insecure but is that intentional?

OLD Sendgrid API version!

I am guessing the version of the API this code uses is pretty obsolete. SendGrid has currently updated its API version to version 3, with more features and functionalities. Is there a roadmap to this or will it remain at it's current state?

IOException is caught, constructor is package protected

Despite the fact that the send method declares itself as throwing an IOException, the exception is caught and set to a public variable. Both rather bad practices.
The constructor is package protected instead of public which doesn't really make much sense either.

String comparison should always be with equals

@@ -646,7 +646,7 @@ public class Sendgrid {
while (paramIterator.hasNext()) {
final String key = paramIterator.next();
final String value = data.get(key);

  •                   if (key == "to" && this.getTos().size() > 0) {
    
  •                   if (key.equals("to") && this.getTos().size() > 0) {
                            if (this._useHeaders() == true) {
                                    requestParams.append("to=" + value + "&"
                            } else {
    
    @@ -654,7 +654,7 @@ public class Sendgrid {
    "to") + "&");
    }
    } else {
  •                           if (key == "toname" && this.getToNames().size() 
    
  •                           if (key.equals("toname") && this.getToNames().si
                                    requestParams.append(this._arrayToUrlPar
                                                    this.getToNames(), "tona
                                                    + "&");
    

Outdated API

As said in here:
#22

the imports
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.google.appengine.labs.repackaged.org.json.JSONArray;

and their use fail. They are no more part of the Java Appengine SDK.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.