Click here to Skip to main content
15,885,641 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.9K   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

 
QuestionProblem with Text Pin
toriton11-Jan-07 0:11
toriton11-Jan-07 0:11 
AnswerRe: Problem with Text Pin
Ashraf Mohamed11-Jan-07 0:58
Ashraf Mohamed11-Jan-07 0:58 
GeneralRe: Problem with Text Pin
toriton11-Jan-07 4:47
toriton11-Jan-07 4:47 
GeneralIOException Pin
LEELAPRASADMALLIPUDI12-Dec-06 1:37
LEELAPRASADMALLIPUDI12-Dec-06 1:37 
GeneralRe: IOException Pin
Ashraf Mohamed16-Dec-06 3:11
Ashraf Mohamed16-Dec-06 3:11 
Generaljavax.mail.MessagingException: IOException while sending message; Pin
LEELAPRASADMALLIPUDI12-Dec-06 1:21
LEELAPRASADMALLIPUDI12-Dec-06 1:21 
GeneralSimple clarification Pin
srilathaaaaaaaaaaa12-Oct-06 0:06
srilathaaaaaaaaaaa12-Oct-06 0:06 
GeneralRe: Simple clarification Pin
Ashraf Mohamed12-Oct-06 2:56
Ashraf Mohamed12-Oct-06 2:56 
Hi,
You misunderstood. The above stated article I showed for your understanding. I have written two articles for one in C# and another one in java. First of all I could not understand your questions. If you want to use it jsp then put my code in init method.

Rgs,
Ashraf Mohamed

visit www.mohamedashraf.tk

Questionwhat do u mean by UI Pin
srilathaaaaaaaaaaa10-Oct-06 21:46
srilathaaaaaaaaaaa10-Oct-06 21:46 
AnswerRe: what do u mean by UI Pin
Ashraf Mohamed11-Oct-06 5:13
Ashraf Mohamed11-Oct-06 5:13 
GeneralIntialization Block--null Pin
Rajasekarankanagaraj9-Oct-06 19:50
Rajasekarankanagaraj9-Oct-06 19:50 
GeneralRe: Intialization Block--null Pin
Ashraf Mohamed10-Oct-06 19:58
Ashraf Mohamed10-Oct-06 19:58 
QuestionInitiallization Block-----null Pin
Rajasekarankanagaraj9-Oct-06 1:17
Rajasekarankanagaraj9-Oct-06 1:17 
AnswerRe: Initiallization Block-----null Pin
Ashraf Mohamed9-Oct-06 3:40
Ashraf Mohamed9-Oct-06 3:40 
QuestionInitiallization Block-----null Pin
Bisweswar21-Sep-06 2:54
Bisweswar21-Sep-06 2:54 
AnswerRe: Initiallization Block-----null Pin
Ashraf Mohamed21-Sep-06 5:03
Ashraf Mohamed21-Sep-06 5:03 
Questionimport org.xbill.DNS.*; Pin
Bisweswar20-Sep-06 3:14
Bisweswar20-Sep-06 3:14 
Generalunknown host Pin
layak13-Sep-06 23:36
layak13-Sep-06 23:36 
GeneralRe: unknown host Pin
Ashraf Mohamed14-Sep-06 2:50
Ashraf Mohamed14-Sep-06 2:50 
GeneralRe: unknown host Pin
layak15-Sep-06 21:19
layak15-Sep-06 21:19 
Generalunknown host Pin
layak17-Sep-06 18:52
layak17-Sep-06 18:52 
GeneralDoubt regarding the use of sendmail.jar Pin
Dipanjan Chatterjee6-Jun-06 20:04
Dipanjan Chatterjee6-Jun-06 20:04 
GeneralRe: Doubt regarding the use of sendmail.jar Pin
Ashraf Mohamed25-Jul-06 1:24
Ashraf Mohamed25-Jul-06 1:24 
Questionsmtp server Pin
J512198212-Apr-06 21:14
J512198212-Apr-06 21:14 
AnswerRe: smtp server Pin
Ashraf Mohamed13-Apr-06 4:16
Ashraf Mohamed13-Apr-06 4:16 

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.