5,702,921 members and growing! (22,019 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, NT4, Win2K, WinXP, Win2003, Windows, Visual Studio, Dev

Posted: 14 Jun 2005
Updated: 14 Jun 2005
Views: 86,619
Bookmarked: 21 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
12 votes for this Article.
Popularity: 3.12 Rating: 2.89 out of 5
3 votes, 25.0%
1
0 votes, 0.0%
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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 49 (Total in Forum: 49) (Refresh)FirstPrevNext
QuestionLoggermembersawanram22:57 26 Jun '08  
AnswerRe: LoggermemberAshraf Mohamed19:09 28 Jun '08  
GeneralError in sending Address Try ... nullmemberchris pang22:13 20 Dec '07  
QuestionInitializaiton Block = Null & other errors [modified]memberhanozt_0076:18 22 Aug '07  
GeneralConnectExceptionmemberSabros5:28 14 Aug '07  
GeneralRe: ConnectExceptionmemberAshraf Mohamed17:30 15 Aug '07  
Generalsend mail to other serversmemberRudraxi21:55 7 Aug '07  
GeneralRe: send mail to other serversmemberAshraf Mohamed17:29 15 Aug '07  
GeneralInitialization Block = NullmemberGitanjali Dua2:45 14 Jun '07  
GeneralRe: Initialization Block = NullmemberAshraf Mohamed21:48 1 Jul '07  
GeneralInitialization Block : Nullmemberbinoyvp20:14 13 Jun '07  
GeneralInitialization Block.......nullmemberThermax10:00 13 Mar '07  
GeneralRe: Initialization Block.......nullmemberMohamed Ashraf22:41 13 Mar '07  
Generalinvalid address(urgent)member20:40 3 Mar '07  
GeneralRe: invalid address(urgent)memberMohamed Ashraf4:07 4 Mar '07  
GeneralRe: invalid address(urgent)member191832Sanjay23:36 29 Jul '08  
GeneralRe: invalid address(urgent)memberAshraf Mohamed19:18 30 Jul '08  
QuestionProblem with Textmembertoriton1:11 11 Jan '07  
AnswerRe: Problem with TextmemberMohamed Ashraf1:58 11 Jan '07  
GeneralRe: Problem with Textmembertoriton5:47 11 Jan '07  
GeneralIOExceptionmemberLEELAPRASADMALLIPUDI2:37 12 Dec '06  
GeneralRe: IOExceptionmemberMohamed Ashraf4:11 16 Dec '06  
Generaljavax.mail.MessagingException: IOException while sending message;memberLEELAPRASADMALLIPUDI2:21 12 Dec '06  
GeneralSimple clarificationmembersrilathaaaaaaaaaaa1:06 12 Oct '06  
GeneralRe: Simple clarificationmemberMohamed Ashraf3:56 12 Oct '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-2008
Web10 | Advertise on the Code Project