# Class: Mail

## helma.Mail(smtp)

This class provides functionality to sending Email messages. A mail client object is created by using the helma.Mail() constructor. The mail object then can be manipulated and sent using the methods listed below.
You will either need to set your mail server via the smtp property in the app.properties or server.properties file or pass the hostname of the mail server you want to use as a parameter to the constructor.
Note: Make sure that the SMTP server itself is well-configured, so that it accepts e-mails coming from your server and does not deny relaying. Best and fastest configuration is of course if you run your own SMTP server (e.g. postfix) which might be a bit tricky to set up, however.

## Constructor

#### new Mail(smtp)

Mail client enabling you to send e-mail via SMTP using Packages.javax.mail.

##### Parameters:

Name | Type | Description
--- | --- | ---
`smtp` | String | as String, the hostname of the mail server

Source:
  - helma/Mail.js, line 57

### Members

#### status

The status of this Mail object. This equals `0` unless an error occurred. See Mail.js source code for a list of possible error codes.

Source:
  - helma/Mail.js, line 206

#### (inner) props

Main constructor body

Source:
  - helma/Mail.js, line 645

### Methods

#### addBCC(addstr, name)

Adds a recipient to the list of addresses to get a "blind carbon copy" of an e-mail message.
The first argument specifies the receipient's e-mail address. The optional second argument specifies the name of the recipient.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, receipients email address
`name` | String | as String, optional receipients name

Source:
  - helma/Mail.js, line 397

#### addCC(addstr, name)

Adds a recipient to the list of addresses to get a "carbon copy" of an e-mail message.
The first argument specifies the receipient's e-mail address. The optional second argument specifies the name of the recipient.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, receipients email address
`name` | String | as String, optional receipients name

Source:
  - helma/Mail.js, line 377

#### addHeader(name, value)

Set a header in the e-mail message. If the given header is already set the previous value is replaced with the new one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` |  | a header name
`value` |  | the header value

Source:
  - helma/Mail.js, line 259

#### addPart(obj, filename, contentType)

Adds an attachment to an e-mail message.
The attachment needs to be either a helma.util.MimePart Object retrieved through the global getURL function, or a helma.File object, or a String.
Use the getURL() function to retrieve a MIME object or wrap a helma.File object around a file of the local file system.

##### Parameters:

Name | Type | Description
--- | --- | ---
`obj` | fileOrMimeObjectOrString | File, Mime object or String to attach to the email
`filename` | String | optional name of the attachment
`contentType` | String | optional content type (only if first argument is a string)

Source:
  - helma/Mail.js, line 494
See:
  - global.getURL
  - helma.util.MimePart
  - helma.File

#### addText(text)

Appends a string to the body text of an e-mail message.

##### Parameters:

Name | Type | Description
--- | --- | ---
`text` | String | as String, to be appended to the message body

Source:
  - helma/Mail.js, line 444
See:
  - #setText

#### addTo(addstr, name)

Adds a recipient to the address list of an e-mail message.
The first argument specifies the receipient's e-mail address. The optional second argument specifies the name of the recipient.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, receipients email address
`name` | String | as String, optional receipients name

Source:
  - helma/Mail.js, line 356
See:
  - setTo

#### getHeader(name) → {Array.<String>}

Get all the headers for this header name. Returns null if no headers for this header name are available.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` |  | a header name

Source:
  - helma/Mail.js, line 275

##### Returns:

a string array of header values, or null

Type
  Array.<String>

#### getMessage()

Returns the wrapped message

Source:
  - helma/Mail.js, line 189

##### Returns:

The wrapped message

#### getMultipartType()

Returns the MIME multiparte message subtype. The default value is "mixed" for messages of type multipart/mixed.

Source:
  - helma/Mail.js, line 474
See:
  - #setMultipartType
  - #addPart

##### Returns:

the MIME subtype

#### getSource()

Returns the source of this mail as RFC 822 formatted string.

Source:
  - helma/Mail.js, line 578

##### Returns:

The source of this mail as RFC 822 formatted string

#### removeHeader(name)

Remove all headers with this name.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` |  | the header name

Source:
  - helma/Mail.js, line 295

#### send()

Sends an e-mail message.
This function sends the message using the SMTP server as specified when the Mail object was constructed using helma.Mail.
If no smtp hostname was specified when the Mail object was constructed, the smtp property in either the app.properties or server.properties file needs to be set in order for this to work.
As a fallback, the message will then be written to file using the #writeToFile method. Additionally, the location of the message files can be determined by setting smtp.dir in app.properties to the desired file path.

Source:
  - helma/Mail.js, line 610

#### setAuthentication(uname, pwd)

Sets username and password to use for SMTP authentication.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uname` | String | The username to use
`pwd` | String | The password to use

Source:
  - helma/Mail.js, line 174

#### setDebug(debug)

Switches debug mode on or off.

##### Parameters:

Name | Type | Description
--- | --- | ---
`debug` | Boolean | If true debug mode is enabled

Source:
  - helma/Mail.js, line 197

#### setFrom(addstr, name)

Sets the sender of an e-mail message.
The first argument specifies the receipient's e-mail address. The optional second argument specifies the name of the recipient.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, sender email address
`name` | String | as String, optional sender name

Source:
  - helma/Mail.js, line 218

#### setHeader(name, value)

Set a header in the e-mail message. If the given header is already set the previous value is replaced with the new one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` |  | a header name
`value` |  | the header value

Source:
  - helma/Mail.js, line 243

#### setMultipartType(messageType)

Sets the MIME multiparte message subtype. The default value is "mixed" for messages of type multipart/mixed. A common value is "alternative" for the multipart/alternative MIME type.

##### Parameters:

Name | Type | Description
--- | --- | ---
`messageType` | String | the MIME subtype such as "mixed" or "alternative".

Source:
  - helma/Mail.js, line 461
See:
  - #getMultipartType
  - #addPart

#### setReplyTo(addstr)

Sets the Reply-To address of an e-mail message.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, the reply-to email address

Source:
  - helma/Mail.js, line 310

#### setSubject(subject)

Sets the subject of an e-mail message.

##### Parameters:

Name | Type | Description
--- | --- | ---
`subject` | String | as String, the email subject

Source:
  - helma/Mail.js, line 412

#### setText(text)

Sets the body text of an e-mail message.

##### Parameters:

Name | Type | Description
--- | --- | ---
`text` | String | as String, to be appended to the message body

Source:
  - helma/Mail.js, line 431
See:
  - #addText

#### setTo(addstr, name)

Sets the recipient of an e-mail message. The first argument specifies the receipient's e-mail address. The optional second argument specifies the name of the recipient.

##### Parameters:

Name | Type | Description
--- | --- | ---
`addstr` | String | as String, receipients email address
`name` | String | as String, optional receipients name

Source:
  - helma/Mail.js, line 335
See:
  - #addTo

#### writeToFile(dir)

Saves this mail RFC 822 formatted into a file. The name of the file is prefixed with "helma.Mail" followed by the current time in milliseconds and a random number.

##### Parameters:

Name | Type | Description
--- | --- | ---
`dir` | helma.File | An optional directory where to save this mail to. If omitted the mail will be saved in the system's temp directory.

Source:
  - helma/Mail.js, line 549