• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sending Email attachment without saving

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have to send email with a pdf file as an attachment.
I want to send the attachment without saving the pdf file onto the disk.

I have written code for sending an email attachment could some one help to send the attachment without saving it as a file.

SEE MY CODE BELOW:-
*************************************************************************
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Insert the type's description here.
* Creation date: (11/26/2001 6:22:14 PM)
* @author: Administrator
*/
public class SimpleSenderServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
this.doService(request, response);
}

public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
this.doService(request, response);
}

public void doService(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {

String recipients[] =
{ "[email protected]"};

String recipientsCC[] = {"[email protected]"};
String from = "[email protected]";
String host = "172.19.31.189";
String filename =
"C:\\Documents and Settings\\vraju.SYNTELDCX\\Desktop\\aqua.jpeg";
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";

Properties props = System.getProperties();
props.put("mail.smtp.host", host);

Session session = Session.getInstance(props, null);

try {

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] addressTo =
new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
InternetAddress[] addressCC =
new InternetAddress[recipientsCC.length];
for (int i = 0; i < recipientsCC.length; i++) {
addressCC[i] = new InternetAddress(recipientsCC[i]);
}
msg.setRecipients(Message.RecipientType.CC, addressCC);
msg.setSubject(subject);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);

MimeBodyPart mbp2 = new MimeBodyPart();

FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

msg.setContent(mp);
msg.setSentDate(new Date());

Transport.send(msg);

} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}

}
}


*************************************************************************

Thanks in advance

With Regards,
Vivek Raju.
 
Sheriff
Posts: 28436
104
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using JavaMail 1.3 or earlier, there is a ByteArrayDataSource class in the "demo" directory of your JavaMail download. You could use that. In JavaMail 1.4 that class is built in (the javax.mail.util package).
 
I knew that guy would be trouble! Thanks tiny ad!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic