com.lavantech.net.mail
Class SMTPMailer

java.lang.Object
  extended bycom.lavantech.net.mail.SMTPMailer

public class SMTPMailer
extends java.lang.Object

SMTPMailer mails a given message. If no mail server is given, the SMTPMailer resolves mail exchange by doing a DNS MX Record lookup for each "To/CC/BCC" Email Address domain. SMTPMailer supports "PLAIN" Authentication. If a login and password is provided along with a mail server, authentication is performed before delivering the mail.

  
    import java.io.*;
    import com.lavantech.net.mail.*;

    public class TestMail
    {

        public static void main(String[] args)
        {
            try
            {
                EmailAddress from = new EmailAddress("myname@mydomain.com", "My Name");
                EmailAddress to[] = new EmailAddress[2];
                to[0] = new EmailAddress("myfriend@domain.com","My Friend");
                to[1] = new EmailAddress("friend2@domain.com","Friend 2");
                EmailAddress cc[] = new EmailAddress[1];
                cc[0] = new EmailAddress("myname@mydomain.com","My Name");
                Message msg = new Message(from, to, cc, null, "Test Mail", "Hello Friend...");
                msg.addAttachment(new File("C:\\docs\\mydoc.doc"));
                SMTPMailer.sendMail(msg);
            }
            catch(MailException exp)
            {
                EmailAddress[] failedAddresses = exp.getFailedAddresses();
                String[] reasons = exp.getReasons();
                System.err.println("Mail failed for the following email id: \n");
                for(int i=0; i<failedAddresses.length; i++)
                    System.err.println(failedAddresses[i]+" - "+reasons[i]);
            }
            catch(Exception exp)
            {
                System.err.println(exp.getMessage());
                exp.printStackTrace();
            }
        }
    }
  
  


Field Summary
static int SMTP_PORT
          SMTP Port number.
static int SOCKET_TIMEOUT
          Socket Timeout period in milliseconds.
 
Constructor Summary
SMTPMailer()
           
 
Method Summary
static java.lang.String[] getMailServers(java.lang.String domain)
          Returns an array of mail server for a given domain.
static void sendMail(Message msg)
          Mails a given message.
static void sendMail(Message msg, java.lang.String mailServer, java.lang.String login, java.lang.String passwd)
          Mails a given message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SMTP_PORT

public static int SMTP_PORT
SMTP Port number. The default value is 25. Change this only if you know what you are doing.


SOCKET_TIMEOUT

public static int SOCKET_TIMEOUT
Socket Timeout period in milliseconds. The default value is 10000 (10 seconds). This socket timeout is used in socket connect, read and write with the mail server.

Constructor Detail

SMTPMailer

public SMTPMailer()
Method Detail

getMailServers

public static java.lang.String[] getMailServers(java.lang.String domain)
Returns an array of mail server for a given domain. The Mail Servers are obtained by performing a MX Record lookup in DNS.


sendMail

public static void sendMail(Message msg)
                     throws InvalidAddressException,
                            AttachmentException,
                            MailException
Mails a given message. This method groups To/CC/BCC addresses with common domain, for each domain, the method resolves the Mail Server for that domain (DNS MX Record), makes a SMTP connection to the first available mail server and sends the mail for that domain. If a SMTP error occurs for that mail server, the next available mail server for that domain is tried (there can be more than one mail server for a domain). If an error occurs for a EmailAddress/Domain, the remaining EmailAddress/Domain is attempted and a MailException is thrown with the list of failed EmailAddress.

Parameters:
msg - Message that needs to be emailed.
Throws:
InvalidAddressException - if no From address or no To/CC/BCC address in the message.
AttachmentException - if an attachment file in the message is not readable.
MailException - if mail cannot be sent to some email addresses. This execption is thrown even if mail was successfully delivered to other email addresses.

sendMail

public static void sendMail(Message msg,
                            java.lang.String mailServer,
                            java.lang.String login,
                            java.lang.String passwd)
                     throws InvalidAddressException,
                            AttachmentException,
                            SMTPAuthException,
                            MailException
Mails a given message. This method makes a SMTP connection to the given mail server, If a login and password is provided (not null),a PLAIN authentication is done with the server, the message is delivered to the mail server with all the TO/CC/BCC addresses, the mail server then relays the mail to other mail servers. MailException is thrown if a SMTP error occurs with the mail server.

Parameters:
msg - Message that needs to be emailed.
mailServer - The Mail Server host name or IP address.
login - The login id at the Mail Server.
passwd - The Password for the login at the Mail Server.
Throws:
InvalidAddressException - if no From address or no To/CC/BCC address in the message.
AttachmentException - if an attachment file in the message is not readable.
SMTPAuthException - If the mail server doesn't accept the login/password.
MailException - if an error occurs while sending mail.