Click here to Skip to main content
6,292,426 members and growing! (9,975 online)
Email Password   helpLost your password?
Languages » Java » General     Intermediate License: The Code Project Open License (CPOL)

Quick Mail using Java Mail API

By Ashraf Mohamed

Sending Mails using Java API
Java, Java, Win2K, WinXP, Win2003, Visual Studio, Dev
Posted:14 Jun 2005
Views:104,952
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 3.12 Rating: 2.89 out of 5
3 votes, 25.0%
1

2
4 votes, 33.3%
3
1 vote, 8.3%
4
4 votes, 33.3%
5

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


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

Occupation: Web Developer
Location: United States United States

Other popular Java articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
QuestionDisplaying the read coneten using javamail in HTML format PinmemberArivudai NAmbi1:42 25 Feb '09  
AnswerRe: Displaying the read coneten using javamail in HTML format PinmemberAshraf Mohamed6:45 25 Feb '09  
GeneralQuery for using Java Mail PinmemberDivyaKamli0:09 8 Feb '09  
GeneralRe: Query for using Java Mail PinmemberAshraf Mohamed3:03 8 Feb '09  
QuestionLogger Pinmembersawanram22:57 26 Jun '08  
AnswerRe: Logger PinmemberAshraf Mohamed19:09 28 Jun '08  
GeneralError in sending Address Try ... null Pinmemberchris pang22:13 20 Dec '07  
QuestionInitializaiton Block = Null & other errors [modified] Pinmemberhanozt_0076:18 22 Aug '07  
GeneralConnectException PinmemberSabros5:28 14 Aug '07  
GeneralRe: ConnectException PinmemberAshraf Mohamed17:30 15 Aug '07  
Generalsend mail to other servers PinmemberRudraxi21:55 7 Aug '07  
GeneralRe: send mail to other servers PinmemberAshraf Mohamed17:29 15 Aug '07  
GeneralInitialization Block = Null PinmemberGitanjali Dua2:45 14 Jun '07  
GeneralRe: Initialization Block = Null PinmemberAshraf Mohamed21:48 1 Jul '07  
GeneralInitialization Block : Null Pinmemberbinoyvp20:14 13 Jun '07  
GeneralInitialization Block.......null PinmemberThermax10:00 13 Mar '07  
GeneralRe: Initialization Block.......null PinmemberMohamed Ashraf22:41 13 Mar '07  
Generalinvalid address(urgent) Pinmember20:40 3 Mar '07  
GeneralRe: invalid address(urgent) PinmemberMohamed Ashraf4:07 4 Mar '07  
GeneralRe: invalid address(urgent) Pinmember191832Sanjay23:36 29 Jul '08  
GeneralRe: invalid address(urgent) PinmemberAshraf Mohamed19:18 30 Jul '08  
QuestionProblem with Text Pinmembertoriton1:11 11 Jan '07  
AnswerRe: Problem with Text PinmemberMohamed Ashraf1:58 11 Jan '07  
GeneralRe: Problem with Text Pinmembertoriton5:47 11 Jan '07  
GeneralIOException PinmemberLEELAPRASADMALLIPUDI2:37 12 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Jun 2005
Editor: Deeksha Shenoy
Copyright 2005 by Ashraf Mohamed
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project