Click here to Skip to main content
13,300,331 members (52,218 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 14 Jun 2005

Quick Mail using Java Mail API

, 14 Jun 2005
Rate this:
Please Sign up or sign in to vote.
Sending Mails using Java API


Quick Mail is a simple way to send mail using Java API in Windows and SUN/UNIX.

Things Required, Where to Get and How to Install

  1. activation.jar
  2. dnsjava.jar.
  3. mail.jar
  4. sendmail.jar

Above all, you can get it from the Sun site. But, you can get all the jar files from which attached with this article.

Brief Description

This article will help you to send mails using Java API. I have given the basic Java program. You can integrate this with any UI for easy access.

Source Code

I have given sample code to use it.

    MimeMessage mimemessage = new MimeMessage(session);
    // set FROM
    mimemessage.setFrom(new InternetAddress(mailfrom));
    // set DATE
    mimemessage.setSentDate(new java.util.Date());
    // set SUBJECT

    // set TO address
        mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, mailto);
    catch(Exception exception1)
        System.out.println("\tError in setting recipients ......\t" + 

    // set message BODY
    MimeBodyPart mimebodypart = new MimeBodyPart();

    // attach message BODY
    MimeMultipart mimemultipart = new MimeMultipart();

    // attach FILE
    mimebodypart = new MimeBodyPart();
        FileDataSource filedatasource = new FileDataSource(filename);
        mimebodypart.setDataHandler(new DataHandler(filedatasource));
    catch(Exception exception3)
        System.out.println("\tError in sending file not been able to attach ......\t" 
                        + exception3.getMessage());
    mimebodypart.setFileName(filename); // set FILENAME

    //set CC MAIL and SEND the mail
        // set CC MAIL
        mimemessage.setRecipients(javax.mail.Message.RecipientType.CC, ccmailid);
                        // send MAIL
        System.out.println("\tSent Successfully..........");
        strResult = "\tSent Successfully..........";
        catch(Exception exception4)
        System.out.println("\tError in sending Address Try........." + 
        System.out.println("\tMail operation Failed..........\t");
        strResult = "\tMail operation Failed..........\t";

Just try it. It is simple to use.

Happy programming!


  • 14th June, 2005: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ashraf Mohamed
Web Developer
United States United States
I am a system analyst and have been with Microsoft & Sun Technologies for more than 7 years. I have always been fascinated by java and .NET. I take lot of technical articles and writing them.

I am a Sun Certified Java Programmer for Java 2 Platform 1.4 , Web component developer Java 2 Platform - Enterprise Edition 1.4 and Microsoft certified developer using C#.NET in Web Development ASP.NET.

Visit my web site

You may also be interested in...


Comments and Discussions

QuestionReading mail using Java mail API Pin
boss prabu1-Jun-13 10:16
memberboss prabu1-Jun-13 10:16 
QuestionEmail Reader Software Pin
Sourabh Usta2-Jul-12 6:14
memberSourabh Usta2-Jul-12 6:14 
Questionclass path and other error Pin
sukhadiya raviraj20-Aug-11 21:18
membersukhadiya raviraj20-Aug-11 21:18 
QuestionInitialization Block.........null Pin
vinitan510-May-11 1:34
membervinitan510-May-11 1:34 
QuestionHow to decode Base64 and Quoted-Printable content type Pin
shikha_virmani8-May-10 23:15
membershikha_virmani8-May-10 23:15 
AnswerRe: How to decode Base64 and Quoted-Printable content type Pin
Ashraf Mohamed9-May-10 2:28
memberAshraf Mohamed9-May-10 2:28 
QuestionDisplaying the read coneten using javamail in HTML format Pin
Arivudai NAmbi25-Feb-09 1:42
memberArivudai NAmbi25-Feb-09 1:42 
AnswerRe: Displaying the read coneten using javamail in HTML format Pin
Ashraf Mohamed25-Feb-09 6:45
memberAshraf Mohamed25-Feb-09 6:45 
GeneralQuery for using Java Mail Pin
DivyaKamli8-Feb-09 0:09
memberDivyaKamli8-Feb-09 0:09 
GeneralRe: Query for using Java Mail Pin
Ashraf Mohamed8-Feb-09 3:03
memberAshraf Mohamed8-Feb-09 3:03 
QuestionLogger Pin
sawanram26-Jun-08 22:57
membersawanram26-Jun-08 22:57 
AnswerRe: Logger Pin
Ashraf Mohamed28-Jun-08 19:09
memberAshraf Mohamed28-Jun-08 19:09 
GeneralError in sending Address Try ... null Pin
chris pang20-Dec-07 22:13
memberchris pang20-Dec-07 22:13 
QuestionInitializaiton Block = Null & other errors [modified] Pin
hanozt_00722-Aug-07 6:18
memberhanozt_00722-Aug-07 6:18 
GeneralConnectException Pin
Sabros14-Aug-07 5:28
memberSabros14-Aug-07 5:28 
GeneralRe: ConnectException Pin
Ashraf Mohamed15-Aug-07 17:30
memberAshraf Mohamed15-Aug-07 17:30 
Generalsend mail to other servers Pin
Rudraxi7-Aug-07 21:55
memberRudraxi7-Aug-07 21:55 
GeneralRe: send mail to other servers Pin
Ashraf Mohamed15-Aug-07 17:29
memberAshraf Mohamed15-Aug-07 17:29 
GeneralInitialization Block = Null Pin
Gitanjali Dua14-Jun-07 2:45
memberGitanjali Dua14-Jun-07 2:45 
GeneralRe: Initialization Block = Null Pin
Ashraf Mohamed1-Jul-07 21:48
memberAshraf Mohamed1-Jul-07 21:48 
GeneralInitialization Block : Null Pin
binoyvp13-Jun-07 20:14
memberbinoyvp13-Jun-07 20:14 
GeneralInitialization Block.......null Pin
Thermax13-Mar-07 10:00
memberThermax13-Mar-07 10:00 
GeneralRe: Initialization Block.......null Pin
Mohamed Ashraf13-Mar-07 22:41
memberMohamed Ashraf13-Mar-07 22:41 
Generalinvalid address(urgent) Pin
Member #32456953-Mar-07 20:40
memberMember #32456953-Mar-07 20:40 
GeneralRe: invalid address(urgent) Pin
Mohamed Ashraf4-Mar-07 4:07
memberMohamed Ashraf4-Mar-07 4:07 
GeneralRe: invalid address(urgent) Pin
191832Sanjay29-Jul-08 23:36
member191832Sanjay29-Jul-08 23:36 
GeneralRe: invalid address(urgent) Pin
Ashraf Mohamed30-Jul-08 19:18
memberAshraf Mohamed30-Jul-08 19:18 
QuestionProblem with Text Pin
toriton11-Jan-07 1:11
membertoriton11-Jan-07 1:11 
AnswerRe: Problem with Text Pin
Mohamed Ashraf11-Jan-07 1:58
memberMohamed Ashraf11-Jan-07 1:58 
GeneralRe: Problem with Text Pin
toriton11-Jan-07 5:47
membertoriton11-Jan-07 5:47 
GeneralIOException Pin
GeneralRe: IOException Pin
Mohamed Ashraf16-Dec-06 4:11
memberMohamed Ashraf16-Dec-06 4:11 
Generaljavax.mail.MessagingException: IOException while sending message; Pin
GeneralSimple clarification Pin
srilathaaaaaaaaaaa12-Oct-06 1:06
membersrilathaaaaaaaaaaa12-Oct-06 1:06 
GeneralRe: Simple clarification Pin
Mohamed Ashraf12-Oct-06 3:56
memberMohamed Ashraf12-Oct-06 3:56 
Questionwhat do u mean by UI Pin
srilathaaaaaaaaaaa10-Oct-06 22:46
membersrilathaaaaaaaaaaa10-Oct-06 22:46 
AnswerRe: what do u mean by UI Pin
Mohamed Ashraf11-Oct-06 6:13
memberMohamed Ashraf11-Oct-06 6:13 
GeneralIntialization Block--null Pin
Rajasekarankanagaraj9-Oct-06 20:50
memberRajasekarankanagaraj9-Oct-06 20:50 
GeneralRe: Intialization Block--null Pin
Mohamed Ashraf10-Oct-06 20:58
memberMohamed Ashraf10-Oct-06 20:58 
QuestionInitiallization Block-----null Pin
Rajasekarankanagaraj9-Oct-06 2:17
memberRajasekarankanagaraj9-Oct-06 2:17 
AnswerRe: Initiallization Block-----null Pin
Mohamed Ashraf9-Oct-06 4:40
memberMohamed Ashraf9-Oct-06 4:40 
QuestionInitiallization Block-----null Pin
Bisweswar21-Sep-06 3:54
memberBisweswar21-Sep-06 3:54 
AnswerRe: Initiallization Block-----null Pin
Mohamed Ashraf21-Sep-06 6:03
memberMohamed Ashraf21-Sep-06 6:03 
Questionimport org.xbill.DNS.*; Pin
Bisweswar20-Sep-06 4:14
memberBisweswar20-Sep-06 4:14 
Generalunknown host Pin
layak14-Sep-06 0:36
memberlayak14-Sep-06 0:36 
GeneralRe: unknown host Pin
Mohamed Ashraf14-Sep-06 3:50
memberMohamed Ashraf14-Sep-06 3:50 
GeneralRe: unknown host Pin
layak15-Sep-06 22:19
memberlayak15-Sep-06 22:19 
Generalunknown host Pin
layak17-Sep-06 19:52
memberlayak17-Sep-06 19:52 
GeneralDoubt regarding the use of sendmail.jar Pin
Dipanjan Chatterjee6-Jun-06 21:04
memberDipanjan Chatterjee6-Jun-06 21:04 
GeneralRe: Doubt regarding the use of sendmail.jar Pin
Mohamed Ashraf25-Jul-06 2:24
memberMohamed Ashraf25-Jul-06 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by Ashraf Mohamed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid