Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / Java / Java SE
Article

Quick Mail using Java Mail API

Rate me:
Please Sign up or sign in to vote.
3.14/5 (15 votes)
14 Jun 2005CPOL 245.8K   4.8K   35   59
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.

C#
    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)


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

Comments and Discussions

 
QuestionReading mail using Java mail API Pin
boss prabu1-Jun-13 9:16
boss prabu1-Jun-13 9:16 
QuestionEmail Reader Software Pin
Sourabh Usta2-Jul-12 5:14
Sourabh Usta2-Jul-12 5:14 
Questionclass path and other error Pin
sukhadiya raviraj20-Aug-11 20:18
sukhadiya raviraj20-Aug-11 20:18 
QuestionInitialization Block.........null Pin
vinitan510-May-11 0:34
vinitan510-May-11 0:34 
QuestionHow to decode Base64 and Quoted-Printable content type Pin
shikha_virmani8-May-10 22:15
shikha_virmani8-May-10 22:15 
AnswerRe: How to decode Base64 and Quoted-Printable content type Pin
Ashraf Mohamed9-May-10 1:28
Ashraf Mohamed9-May-10 1:28 
QuestionDisplaying the read coneten using javamail in HTML format Pin
Arivudai NAmbi25-Feb-09 0:42
Arivudai NAmbi25-Feb-09 0:42 
AnswerRe: Displaying the read coneten using javamail in HTML format Pin
Ashraf Mohamed25-Feb-09 5:45
Ashraf Mohamed25-Feb-09 5:45 
GeneralQuery for using Java Mail Pin
DivyaKamli7-Feb-09 23:09
DivyaKamli7-Feb-09 23:09 
GeneralRe: Query for using Java Mail Pin
Ashraf Mohamed8-Feb-09 2:03
Ashraf Mohamed8-Feb-09 2:03 
QuestionLogger Pin
sawanram26-Jun-08 21:57
sawanram26-Jun-08 21:57 
AnswerRe: Logger Pin
Ashraf Mohamed28-Jun-08 18:09
Ashraf Mohamed28-Jun-08 18:09 
GeneralError in sending Address Try ... null Pin
chris pang20-Dec-07 21:13
chris pang20-Dec-07 21:13 
QuestionInitializaiton Block = Null & other errors [modified] Pin
hanozt_00722-Aug-07 5:18
hanozt_00722-Aug-07 5:18 
GeneralConnectException Pin
Sabros14-Aug-07 4:28
Sabros14-Aug-07 4:28 
GeneralRe: ConnectException Pin
Ashraf Mohamed15-Aug-07 16:30
Ashraf Mohamed15-Aug-07 16:30 
Generalsend mail to other servers Pin
Rudraxi7-Aug-07 20:55
Rudraxi7-Aug-07 20:55 
GeneralRe: send mail to other servers Pin
Ashraf Mohamed15-Aug-07 16:29
Ashraf Mohamed15-Aug-07 16:29 
GeneralInitialization Block = Null Pin
Gitanjali Dua14-Jun-07 1:45
Gitanjali Dua14-Jun-07 1:45 
GeneralRe: Initialization Block = Null Pin
Ashraf Mohamed1-Jul-07 20:48
Ashraf Mohamed1-Jul-07 20:48 
GeneralInitialization Block : Null Pin
binoyvp13-Jun-07 19:14
binoyvp13-Jun-07 19:14 
GeneralInitialization Block.......null Pin
Thermax13-Mar-07 9:00
Thermax13-Mar-07 9:00 
GeneralRe: Initialization Block.......null Pin
Ashraf Mohamed13-Mar-07 21:41
Ashraf Mohamed13-Mar-07 21:41 
Generalinvalid address(urgent) Pin
Member 32456953-Mar-07 19:40
Member 32456953-Mar-07 19:40 
GeneralRe: invalid address(urgent) Pin
Ashraf Mohamed4-Mar-07 3:07
Ashraf Mohamed4-Mar-07 3:07 

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.