File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending file as an attachment

 
madhavi prasad
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Im new to JAVA Mail . My requirement is i need to send a file as an attachment can any one give me any references to links, r sample code.

Thnx in advace
 
Sree Kumari
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have alook at this


try
{
String host="192.168.1.83";
String to=request.getParameter("to");
String from=request.getParameter("from");
String sub=request.getParameter("sub");
String matter=request.getParameter("matter");
String filename=request.getParameter("F1");

boolean sessionDebug=false;

Properties props=System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
Session mailSession=Session.getDefaultInstance(props,null);
mailSession.setDebug(sessionDebug);
Message msg=new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(sub);
msg.setSentDate(new Date());
msg.setText(matter);


BodyPart bp=new MimeBodyPart();
bp.setText(matter);
Multipart multipart=new MimeMultipart();
multipart.addBodyPart(bp);

BodyPart msgpart=new MimeBodyPart();
DataSource source=new FileDataSource(filename);
msgpart.setDataHandler(new DataHandler(source));
msgpart.setFileName(filename);
multipart.addBodyPart(msgpart);
msg.setContent(multipart);

Transport.send(msg);

out.println("Mail was successfully sent to"+to);
out.println("from"+from);
out.println("using host"+".");

}
catch(Exception e)
{
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Other Java APIs.
 
payal agrawal
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the URL below:

How to send an email with a file attachment
http://www.java-tips.org/content/view/616/29/
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic