SMTPMsg Class

This class provides a basic structure for e-mail messages and various methods for verifying e-mail addresses and generating mail headers. It is a supplemental class to the SMTPConn class which is required by this class.

An example of how to use this class is shown at the bottom of this page.

Works with:

JDK 1.5 >

See Also:

SMTPConn Class

Download:

You can download the SMTPConn and SMTPMsg classes from the sample page.



Class Overview

Constructor Summary

SMTPMsg()
Initializes the class without any message data.

SMTPMsg(String to, String from, String fromName, String subject, String data)
Initializes the class and sets the e-mail data to, from, fromName, subject and data.

Method Summary

String generateHeader()
Returns a standard mail header to be used with the DATA SMTP method. This method is called by the SMTPConn.sendMail() method.

String getMailBCC()
Returns the e-mail address in the BCC field.

String getMailCC()
Returns the e-mail address in the CC field.

String getMailData()
Returns the content of the e-mail.

String getMailFrom()
Returns the e-mail address in the From field (the sending e-mail).

String getMailFromName()
Returns the name of the sender.

String getMailReplyTo()
Returns the e-mail address in the Reply-To field.

String getMailSubject()
Returns the subject of the e-mail.

String getMailTo()
Returns the e-mail address in the To field.

String getUserAgent()
Returns the User-Agent string that will be identify the mail program.

void reset()
Set's all message variables (which are strings) to "", effectively resetting the class to it's original state if the SMTPMsg() constructor is used.

int send(SMTPConn server, Boolean verbose)
Sends the e-mail using the specified SMTPConn connection.

void setMailBCC(String newVal)
Set's the BCC field to the value passed in newValfield.

void setMailCC(String newVal)
Set's the CC field to the value passed in newValfield.

void setMailData(String newVal)
Set's the e-mail content to the value passed in newValfield.

void setMailFrom(String newVal)
Set's the From field to the value passed in newValfield.

void setMailFromName(String newVal)
Set's the sender's name to the value passed in newValfield.

void setMailReplyTo(String newVal)
Set's the reply address to the value passed in newValfield.

void setMailSubject(String newVal)
Set's the e-mail subject to the value passed in newValfield.

void setMailTo(String newVal)
Set's the To field to the value passed in newValfield.

void setUserAgent(String newVal)
Set's the User-Agent to the value passed in newValfield.

boolean verifyMsg()
Returns true or false depending on whether enough information is available to successfully send the message.



Constructor Detail

SMTPMsg

public SMTPMsg()

Initializes the class with all fields blank.

SMTPMsg

public SMTPMsg(String to, String from, String fromName, String subject, String data)

Initializes the class and accepts values for the to, from, fromName (sender's name), subject and content for the e-mail.

Parameters:

  • to - The e-mail address to send the mail to.
  • from - The senders e-mail address.
  • fromName - The senders name (i.e. "Joe Bloggs").
  • subject - The subject of the e-mail message.
  • data - The body text of the e-mail. This should be plain-text but can contain special characters such as "\r\n".

Throws:



Method Detail

generateHeader

public String generateHeader()

Returns an e-mail header created from the existing content, e-mail addresses and subject. This method is called by SMTPConn.sendMail(). See RFC 2821 for more information on e-mail headers.

getMailBCC

public String getMailBCC()

Returns the e-mail address in the BCC field.

getMailCC

public String getMailCC()

Returns the e-mail address in the CC field.

getMailData

public String getMailData()

Returns the value stored in the data field, this should be the e-mail's body content.

getMailFrom

public String getMailFrom()

Returns the e-mail address in the From field.

getMailMailFromName

public String getMailFromName()

Returns the value of the fromName field, this should be the sender's name.

getMailReplyTo

public String getMailReplyTo()

Returns the e-mail address in the Reply-To field.

getMailSubject

public String getMailSubject()

Returns the subject text of the e-mail.

getMailTo

public String getMailTo()

Returns the e-mail address in the To field.

getUserAgent

public String getUserAgent()

Returns the User-Agent string. The User-Agent is used to identify the sending mail client.

reset

public void reset()

Sets all variables (which are strings) to "", thus resetting the class to an initial state as if created using the SMTPMsg() constructor.

send

public int send(SMTPConn server, Boolean verbose)

Calls the SMTPConn.sendMail() method and passes "this" as a parameter. See the SMTPConn class for more information on the sendMail() method.

Parameters:

  • server - The SMTPConn server connection to use to send the e-mail. This should be of the type SMTPConn.
  • verbose - Specifies whether the method should print status output to the console while sending the message. When set to true, the server responses will be printed to the console and any errors highlighted.

Returns:

'0' if the message is sent successfully or '-1' if a connection error occurs. If the SMTP server reports an error then the last error code sent by the server is returned. See RFC 2821 for a full list.

setMailBCC

public void setMailBCC(String newVal)

Sets the BCC e-mail address field to the value of newVal.

setMailCC

public void setMailCC(String newVal)

Sets the CC e-mail address field to the value of newVal.

setMailData

public void setMailData(String newVal)

Sets the content of the e-mail to the contents of newVal.

setMailFrom

public void setMailFrom(String newVal)

Sets the From e-mail address field to the value of newVal.

setMailFromName

public void setMailFromName(String newVal)

Sets the sender's name to the value of newVal.

setMailReplyTo

public void setMailReplyTo(String newVal)

Sets the ReplyTo e-mail address field to the value of newVal.

setMailSubject

public void setMailSubject(String newVal)

Sets the subject of the e-mail to the value of newVal.

setMailTo

public void setMailTo(String newVal)

Sets the To e-mail address field to the value of newVal.

setUserAgent

public void setUserAgent(String newVal)

Sets the User-Agent string to the value of newVal. This is used to identify what client was used to send the e-mail, if no value is provided then "Java SMTPConn Class" is used as a default.



Example & Download

You can download the SMTPConn and SMTPMsg classes along with a sample application on the SMTPConn & SMTPMsg Classes page.