Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2010-01-03
6
Medium Priority
?
201 Views
Last Modified: 2012-05-08
how send the attachment file to mail using java.
0
Comment
Question by:sunda2010
  • 2
  • 2
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 332 total points
ID: 26169162
0
 
LVL 12

Assisted Solution

by:jazzIIIlove
jazzIIIlove earned 168 total points
ID: 26169793
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 = "jazzIIIlove@gmail.com";  

		String from = "jazzIIIlove@gmail.com";

		String host = "smtp.gmail.com";

		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("mail.smtp.host", host);

		props.put("mail.smtp.port", "587");
		props.put("mail.smtp.auth", "true");

		props.put("mail.store.protocol", "pop3");
		props.put("mail.smtp.starttls.enable","true");
		Session session = 
			Session.getInstance(props, new javax.mail.Authenticator() { 
				protected PasswordAuthentication getPasswordAuthentication(){
					return new PasswordAuthentication("jazzIIIlove", "yourpass");
				}
			}
			);

		try 
		{
			// create a message
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = {new InternetAddress(to)};
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);

			// create and fill the first message part
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setText(msgText1);

			// 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));
			mbp2.setFileName(fds.getName());

			// create the Multipart and add its parts to it
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
			mp.addBodyPart(mbp2);

			// add the Multipart to the message
			msg.setContent(mp);

			// set the Date: header
			msg.setSentDate(new Date());

			// send the message
			Transport.send(msg);
			System.out.println("ok!");
		} 
		catch (MessagingException mex) 
		{
			mex.printStackTrace();
			Exception ex = null;
			if ((ex = mex.getNextException()) != null) 
			{
				ex.printStackTrace();
			}
		}
	}

}

Open in new window

0
 

Author Comment

by:sunda2010
ID: 26188197
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:sunda2010
ID: 26188206
Respected Sir/Madam,

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

0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 332 total points
ID: 26191292
check out this swing component example to know how to browse through the file
http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

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.
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 26194198
>>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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 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