3 Sending Email - Reference Documentation
Authors: Antony Jones
Version: 1.0
3 Sending Email
In a pinch, you can send email using the SendGridService in one of three ways:- Using any controller's built-in sendMail method, and passing your email details to it.
sendMail { from 'antony@example.com' to 'aiten@example.net' to 'wirah@example.org' bcc 'yourbcc@example.com' subject 'This is the subject line' body 'This is our message body' }
sendGridService.sendMail { … }
SendGridEmail email = new SendGridEmailBuilder()
.from('antony@example.com')
.to('aiten@example.net')
.subject('This is the subject line')
.withText('This is our message body')
.build()
sendGridService.send(email)
Option | Builder Syntax | sendMail { } Syntax | Parameter types | Can be called more than once | Required |
---|---|---|---|---|---|
Recipient | .to('email@example.com') | to 'email@example.com' | String | Yes | Yes |
Recipient display name with email address | .to('Antony Jones', 'email@example.com') .addRecipient('Antony Jones', 'email@example.com') | not implemented | String, String | Yes | No |
Sender | .from('email@example.com') | from 'email@example.com' | String | No | Yes |
Sender display name with email address | .from('Antony Jones', 'email | not implemented@ | String, String | No | No |
Reply-to address | .replyTo('email@example.com') | replyTo 'email@example.com' | String | No | No |
Sent date | .sentDate(new Date()) | sentDate new Date() | Date | No | No |
Subject | .subject('Your subject line') | subject 'Your subject line' | String | No | Yes |
Blind Carbon-copy | .addBcc('email@example.com') | bcc 'email@example.com' | String | Yes | Yes |
Body text | .text('Your message text') | body 'Your message text' | String | No | Yes (one of text or html) |
Body html | .html('<h2>Your message html</h2>') | html '<h2>Your message html</h2>' | String | No | Yes (one of text or html) |
Custom handling instruction | .addCustomHandlingInstruction('headerName', 'headerValue') | addCustomHandlingInstruction 'headerName', 'headerValue' | String, String | Yes | No |
Header | .addHeader('headerName', 'headerValue') | addHeader 'headerName', 'headerValue' | String, String | Yes | No |
Attachments | .addAttachment(new File('/tmp/your.file')) | attach new File('/tmp/your.file') | File | Yes | No |