Click here to Skip to main content
Licence CPOL
First Posted 14 Jun 2005
Views 154,310
Downloads 3,238
Bookmarked 32 times

Quick Mail using Java Mail API

By Ashraf Mohamed | 14 Jun 2005
Sending Mails using Java API
3 votes, 21.4%
1

2
4 votes, 28.6%
3
1 vote, 7.1%
4
6 votes, 42.9%
5
3.10/5 - 14 votes
μ 3.10, σa 2.81 [?]

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 raviraj21:18 20 Aug '11  
QuestionInitialization Block.........null Pinmembervinitan51:34 10 May '11  
QuestionHow to decode Base64 and Quoted-Printable content type Pinmembershikha_virmani23:15 8 May '10  
AnswerRe: How to decode Base64 and Quoted-Printable content type PinmemberAshraf Mohamed2:28 9 May '10  
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) PinmemberMember #324569520:40 3 Mar '07  
GeneralRe: invalid address(urgent) PinmemberMohamed Ashraf4:07 4 Mar '07  
GeneralRe: invalid address(urgent) Pinmember191832Sanjay23:36 29 Jul '08  
Hi,
 
I have implemented the same logic into my test program. Its working fine for the correct email addresses, but if I put email address as abc@xyz.com, then also its sending. though no reciept msg is coming even then no exception is there.
GeneralRe: invalid address(urgent) PinmemberAshraf Mohamed19: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
Web04 | 2.5.120210.1 | Last Updated 14 Jun 2005
Article Copyright 2005 by Ashraf Mohamed
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid