I keep seeing people say that using JavaMail is hard.....


>From my humble experience, its very easy. Notice that with the code below,
most of it is setting up message details.....



The actual JavaMail related code is about four lines....



import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;



public class SimpleMsgSend {
    public static void main(String[] argv) {
        new SimpleMsgSend(argv);
    }
    public SimpleMsgSend(String[] argv) {
        String to = "[EMAIL PROTECTED]";
        String subject = "Testing java mail";
        String from = "[EMAIL PROTECTED]";
        String cc = "[EMAIL PROTECTED]";
        String bcc = "[EMAIL PROTECTED]";
        String mailer = "SimpleMsgSend - Test";
        try {
                // Get a Session object
                Session session =
Session.getDefaultInstance(System.getProperties(), null);
                // construct the message
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(from));
                msg.setRecipients(Message.RecipientType.TO,
                                        InternetAddress.parse(to, false));
                msg.setRecipients(Message.RecipientType.CC,
                                        InternetAddress.parse(cc, false));
                msg.setRecipients(Message.RecipientType.BCC,
                                        InternetAddress.parse(bcc,
false));
                msg.setSubject(subject);
                msg.setText("this is a simple test\nWith\na few\nlines in
it\ndIon");
                msg.setHeader("X-Mailer", mailer);
                msg.setSentDate(new Date());

                // send the thing off
                Transport.send(msg);
                System.out.println("\nMail was sent successfully.");
        } catch (Exception e) {
                e.printStackTrace();
        }
}
}





Please respond to [EMAIL PROTECTED]







To:

[EMAIL PROTECTED]

cc:

(bcc: dIon Gillard/Multitask Consulting/AU)





Subject:

Re: Servlet using sun.net.smtp.SmtpClient?









> This is going to sound a little harsh, but IMHO it is
> irresponsible for a respected author to encourage Java
> developers to learn and use bad habits.


Well, I wasn't a respected author at the time.  :-)


I really hoped to use JavaMail, but the code using JavaMail was such
that even sending a simple message brought in all this complexity, and
I felt if I used that code I should at least quickly describe what
every line was doing.  That would have gotten long.  I also pictured
the tech support nightmare of people having problems installing two
.jar files just to send an email.  It wasn't worth it.


I looked for free third party packages next.  At the time I couldn't
find many, and no good ones.  There has been some development in this
area since more than a year ago when I wrote that section.  Today
maybe there's a good alternative.  Every one I saw back then was just
as unsupported as Sun's SmtpClient code, was less widely available,
and was more likely to be removed from its web site than SmtpClient
was to be removed from the JDK.


I decided that if I gave the book a big warning and pointed out this
was only an example of what could be done, I could sleep at night.  :-)


-jh-


_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com


___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
of the message "signoff SERVLET-INTEREST".


Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to