• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sending file as an attachment

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
{
}
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Other Java APIs.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic