SendGrid BCC Mail Settings

BCC stands for Blank Carbon Copy. Whenever a mail is sent, it has certain parameters.

  • to

the end-user recipient who is receiving the email.

  • from

the organization's address who is sending an email.

  • CC

CC stands for carbon copy, email address of those end-users who are not the direct recipients of the email but must be aware of the conversation.

  • BCC

BCC is not visible anywhere in the email, the email address mentioned in BCC will receive all the emails and the recipient will never know about it.

SendGrid provides an HTTP PATCH method to enable BCC service and set BCC email addresses.

When BCC is enabled, SendGrid will send a copy of every email to the configured email address without showing the email address in the mail.

SendGrid allows only 1 email address as BCC. if multiple email address needs to be included in BCC service, then creating a distribution group or forwarding mail rules are preferred.

package org.wesome.sendgrid;

import com.sendgrid.SendGrid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SendGridApplication {
    @Value("${send.grid.api.key}")
    private String sendGridApiKey;

    public static void main(String[] args) {
        SpringApplication.run(SendGridApplication.class, args);
    }

    @Bean
    public SendGrid sendGrid() {
        SendGrid sendGrid = new SendGrid(sendGridApiKey);
        return sendGrid;
    }
}
package org.wesome.sendgrid.controller;

import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class SendGridController {
    @Autowired
    private SendGrid sendGrid;

    @GetMapping
    public ResponseEntity sendGridAPI() throws IOException {
        Request request = new Request();
        Response response;
        try {
            request.setMethod(Method.PATCH);
            request.setEndpoint("mail_settings/bcc");
            request.setBody("{\"enabled\":true,\"email\":\"[email protected]\"}");
            response = sendGrid.api(request);
            System.out.println("Headers := \n" + response.getHeaders());
            System.out.println("Body := \n" + response.getBody());
            System.out.println("StatusCode := \n" + response.getStatusCode());
        } catch (IOException ex) {
            throw ex;
        }
        return new ResponseEntity(HttpStatus.valueOf(response.getStatusCode()).getReasonPhrase(), HttpStatus.valueOf(response.getStatusCode()));
    }
}
plugins {
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'org.springframework.boot' version '2.5.5'
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    implementation 'com.sendgrid:sendgrid-java:4.4.5'
    implementation('org.springframework.boot:spring-boot-starter-web')
}

test {
    useJUnitPlatform()
}
send.grid.api.key=Your SendGrid Api Key
curl --location --request GET 'localhost:8080/'

To turn off BCC settings

package org.wesome.sendgrid;

import com.sendgrid.SendGrid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SendGridApplication {
    @Value("${send.grid.api.key}")
    private String sendGridApiKey;

    public static void main(String[] args) {
        SpringApplication.run(SendGridApplication.class, args);
    }

    @Bean
    public SendGrid sendGrid() {
        SendGrid sendGrid = new SendGrid(sendGridApiKey);
        return sendGrid;
    }
}
package org.wesome.sendgrid.controller;

import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.Response;
import com.sendgrid.SendGrid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class SendGridController {
    @Autowired
    private SendGrid sendGrid;

    @GetMapping
    public ResponseEntity sendGridAPI() throws IOException {
        Request request = new Request();
        Response response;
        try {
            request.setMethod(Method.PATCH);
            request.setEndpoint("mail_settings/bcc");
            request.setBody("{\"enabled\":false}");
            response = sendGrid.api(request);
            System.out.println("Headers := \n" + response.getHeaders());
            System.out.println("Body := \n" + response.getBody());
            System.out.println("StatusCode := \n" + response.getStatusCode());
        } catch (IOException ex) {
            throw ex;
        }
        return new ResponseEntity(HttpStatus.valueOf(response.getStatusCode()).getReasonPhrase(), HttpStatus.valueOf(response.getStatusCode()));
    }
}
plugins {
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'org.springframework.boot' version '2.5.5'
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    implementation 'com.sendgrid:sendgrid-java:4.4.5'
    implementation('org.springframework.boot:spring-boot-starter-web')
}

test {
    useJUnitPlatform()
}
send.grid.api.key=Your SendGrid Api Key
curl --location --request GET 'localhost:8080/'

follow us on