Click here to Skip to main content
Licence CPOL
First Posted 14 Jun 2005
Views 158,645
Downloads 3,487
Bookmarked 33 times

Quick Mail using Java Mail API

By | 14 Jun 2005 | Article
Sending Mails using Java API

Overview

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 Java_Mail_demo.zip 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
    mimemessage.setSubject(subject);

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

    // set message BODY
    MimeBodyPart mimebodypart = new MimeBodyPart();
    mimebodypart.setText(text);

    // attach message BODY
    MimeMultipart mimemultipart = new MimeMultipart();
    mimemultipart.addBodyPart(mimebodypart);

    // attach FILE
    mimebodypart = new MimeBodyPart();
    try
    {
        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
    mimemultipart.addBodyPart(mimebodypart);
    mimemessage.setContent(mimemultipart);

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

Just try it. It is simple to use.

Happy programming!

History

  • 14th June, 2005: Initial post

License

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

Member

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 www.mohamedashraf.tk


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionclass path and other error Pinmembersukhadiya raviraj20:18 20 Aug '11  
QuestionInitialization Block.........null Pinmembervinitan50:34 10 May '11  
QuestionHow to decode Base64 and Quoted-Printable content type Pinmembershikha_virmani22:15 8 May '10  
AnswerRe: How to decode Base64 and Quoted-Printable content type PinmemberAshraf Mohamed1:28 9 May '10  
QuestionDisplaying the read coneten using javamail in HTML format PinmemberArivudai NAmbi0:42 25 Feb '09  
AnswerRe: Displaying the read coneten using javamail in HTML format PinmemberAshraf Mohamed5:45 25 Feb '09  
GeneralQuery for using Java Mail PinmemberDivyaKamli23:09 7 Feb '09  
GeneralRe: Query for using Java Mail PinmemberAshraf Mohamed2:03 8 Feb '09  
QuestionLogger Pinmembersawanram21:57 26 Jun '08  
When we create logger object.It is locked.If requrinment is to read lines of logger file and create new automaticaly how can we do
AnswerRe: Logger PinmemberAshraf Mohamed18:09 28 Jun '08  
GeneralError in sending Address Try ... null Pinmemberchris pang21:13 20 Dec '07  
QuestionInitializaiton Block = Null & other errors [modified] Pinmemberhanozt_0075:18 22 Aug '07  
GeneralConnectException PinmemberSabros4:28 14 Aug '07  
GeneralRe: ConnectException PinmemberAshraf Mohamed16:30 15 Aug '07  
Generalsend mail to other servers PinmemberRudraxi20:55 7 Aug '07  
GeneralRe: send mail to other servers PinmemberAshraf Mohamed16:29 15 Aug '07  
GeneralInitialization Block = Null PinmemberGitanjali Dua1:45 14 Jun '07  
GeneralRe: Initialization Block = Null PinmemberAshraf Mohamed20:48 1 Jul '07  
GeneralInitialization Block : Null Pinmemberbinoyvp19:14 13 Jun '07  
GeneralInitialization Block.......null PinmemberThermax9:00 13 Mar '07  
GeneralRe: Initialization Block.......null PinmemberMohamed Ashraf21:41 13 Mar '07  
Generalinvalid address(urgent) PinmemberMember #324569519:40 3 Mar '07  
GeneralRe: invalid address(urgent) PinmemberMohamed Ashraf3:07 4 Mar '07  
GeneralRe: invalid address(urgent) Pinmember191832Sanjay22:36 29 Jul '08  
GeneralRe: invalid address(urgent) PinmemberAshraf Mohamed18:18 30 Jul '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120604.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by Ashraf Mohamed
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid