i need the code for how to send a mail with attachment using java

Posted on 2010-01-03
Last Modified: 2012-05-08
how send the attachment file to mail using java.
Question by:sunda2010
    LVL 40

    Accepted Solution

    LVL 12

    Assisted Solution

    Hi there;

    I write the following code and it's working

    Things you must do:
    import javamail-1.4.3 jar
    import for jaf-1_0_2-upd2 jar file for javax.activation

    Input your password to the code.

    Voila, attachment with mail is with you.

    Best regards.

    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    public class AttachExample 
    	public static void main(String[] args) 
    		String to = "";  
    		String from = "";
    		String host = "";
    		String filename = "C:\\arp.txt";
    		String msgText1 = "Sending a file.\n";
    		String subject = "Sending a file";
    		// create some properties and get the default Session
    		Properties props = System.getProperties();
    		props.put("mail.transport.protocol", "smtp");
    		props.put("", host);
    		props.put("mail.smtp.port", "587");
    		props.put("mail.smtp.auth", "true");
    		props.put("", "pop3");
    		Session session = 
    			Session.getInstance(props, new javax.mail.Authenticator() { 
    				protected PasswordAuthentication getPasswordAuthentication(){
    					return new PasswordAuthentication("jazzIIIlove", "yourpass");
    			// create a message
    			MimeMessage msg = new MimeMessage(session);
    			msg.setFrom(new InternetAddress(from));
    			InternetAddress[] address = {new InternetAddress(to)};
    			msg.setRecipients(Message.RecipientType.TO, address);
    			// create and fill the first message part
    			MimeBodyPart mbp1 = new MimeBodyPart();
    			// create the second message part
    			MimeBodyPart mbp2 = new MimeBodyPart();
    			// attach the file to the message
    			FileDataSource fds = new FileDataSource(filename);
    			mbp2.setDataHandler(new DataHandler(fds));
    			// create the Multipart and add its parts to it
    			Multipart mp = new MimeMultipart();
    			// add the Multipart to the message
    			// set the Date: header
    			msg.setSentDate(new Date());
    			// send the message
    		catch (MessagingException mex) 
    			Exception ex = null;
    			if ((ex = mex.getNextException()) != null) 

    Open in new window


    Author Comment

    I'll run this program i got an answer.
    This code is very useful for me.
    Thanks a lot for sending the code.
    Thanks for ur reply.


    Author Comment

    Respected Sir/Madam,

    pls provide the code for browse button to select a file and send the file to gmail using attachement for java.

    LVL 40

    Assisted Solution

    check out this swing component example to know how to browse through the file

    Now you have the example of browsing through the files and sending them as an attachment. Please make an attempt to write a source code and get back in case of issues.
    LVL 12

    Expert Comment

    >>Thanks a lot for sending the code
    Normally, the first comment is enough to write the code, but I get curious and solve it.

    >>Respected Sir/Madam,
    No need for that:)

    >>pls provide the code for browse button to select a file and send the file to gmail using attachement for >>java.
    oh come on, why didn't tell it in the first place?:)
    Please write some code and let's see your code too for browse stuff, gladly I will edit it.
    thx gurvinder372.

    Best regards.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now