|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.lavantech.net.mail.SMTPMailer
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 |
public static int SMTP_PORT
public static int SOCKET_TIMEOUT
Constructor Detail |
public SMTPMailer()
Method Detail |
public static java.lang.String[] getMailServers(java.lang.String domain)
public static void sendMail(Message msg) throws InvalidAddressException, AttachmentException, MailException
msg
- Message that needs to be emailed.
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.public static void sendMail(Message msg, java.lang.String mailServer, java.lang.String login, java.lang.String passwd) throws InvalidAddressException, AttachmentException, SMTPAuthException, MailException
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.
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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |