JavaMail First Steps - Hello World Tutorial
(Free Web Tutorials)
by Michael Thomas
JavaMail Home Page
On the JavaMail Home page, you can download
the whole JavaMail site (all content, tutorials & examples) !!!
Objectives
- Use JavaMail to send and receive emails via Hello World type examples.
JavaMail FirstSteps
- Before you continue, you need to complete the following:
- JavaMail must be installed. If it's not installed, click in the
JavaMail Home Page at the top and load the JavaMail Install Tutorial.
- download zip file - Extract the file from the download zip.
If you do not have the zip file, go to the "JavaMail Home Page" (click the
link at the top) and click on the "download" link.
- Use JavaMail to receive emails via SMTP from Pop3 (a Hello World
example)
- Sorry, I have not had time to create the tutorial
(under construction).
More importantly, I do have working examples in the download zip file.
JavaMailReceiveEmailPop3_HelloWorld_
etc... - look at all of these files and the associated .java files.
Make sure you read the _run.bat file to know how to call this batch file and
pass the proper parameters.
- Use JavaMail to send emails via SMTP
- Sorry, I have not had time to create the tutorial
(under construction).
More importantly, I do have working examples in the download zip file.
JavaMailSendEmailSmtp_HelloWorld_ etc...
- look at all of these files and the associated .java files.
Make sure you read the _run.bat file to know how to call this batch file and
pass the proper parameters.
- This example can send plain/text or html/text emails. Download the full
source code and look at the file "JavaMailSendEmailSmtp_HelloWorld.java".
Here is the key snippets:
//Step 1: Create an "Alternative" Multipart message
Multipart mp = new MimeMultipart("alternative");
//Step 2: Create the Text part of the message.
if ( strEmailBodyText != null ) {
MimeBodyPart text = new MimeBodyPart();
text.setText( strEmailBodyText );
mp.addBodyPart(text);
}
//Step 3: Create the HTML part of the message.
if ( strEmailBodyHtml != null ) {
MimeBodyPart html = new MimeBodyPart();
html.setContent(strEmailBodyHtml, "text/html");
mp.addBodyPart(html);
}
//Step 4: Set the content for the message.
msgEmail.setContent(mp);
- SMTP Server Name - some internet providers will not allow you to
connect to another SMTP server so you must use their SMTP server. AT&T is
one of those so use: mailhost.worldnet.att.net
- Java Exception examples:
- Example #1: Bad SMTP host name:
javax.mail.MessagingException: Unknown SMTP host: bad.hostname.com;
nested exception is:
java.net.UnknownHostException: michael@michael-thomas.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1543)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:45
3)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
- Example #2: Exception that occurs during the Send of the email that I think
is caused by some internet providers do not let SMTP relaying. In this case,
just use the SMTP server provided by your internet provider.
Attempting to send the email ...
javax.mail.MessagingException: [EOF]
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1
512)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at com.michaelthomas.myjavamail.JavaMailSendEmailSmtp_HelloWorld.send(Ja
vaMailSendEmailSmtp_HelloWorld.java:140)
at com.michaelthomas.myjavamail.JavaMailSendEmailSmtp_HelloWorld.main(Ja
vaMailSendEmailSmtp_HelloWorld.java:44)
- Example #3: I think this is the SMTP server not allowing access.
Attempting to send the email ...
com.sun.mail.smtp.SMTPSendFailedException: 550 Access denied - Invalid HELO
name (See RFC2821 4.1.1.1)
- Other Programming notes:
- http://java.sun.com/developer/EJTechTips/2004/tt0426.html#1 - Sun's info
HTML emails.
- http://www.vipan.com/htdocs/javamail.html - Great resource for Text,
HTML & Attachments.
- Examples of Content-Type: text/plain, text/html, multipart/mixed,
multipart/related
JavaMail - provides no direct support for multipart/related messaged (pg 91
of JavaMail1.4.pdf)