Results 1 to 3 of 3
  1. #1
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Sending a data structure as an attachment via JavaMail

    Hi, I've managed to send an email with a file as an attachment through JavaMail. However, I'd like to send a data structure (array, vector, etc.) as the attachment instead. One way of course is to export the data structure as a file and then attach that file, however that seems kind of redundant. Is there a way to directly parse the data structure as an attachment? My current partial code for sending an email with an attachment is
    Java Code:
    public static void sendMail() throws Exception {
    		String mailhost = "mailserver.asdasdasd.com";
    		String from = "fred_liu@asdasdas.com";
    		String sendTo[] = { "fred_liu@dfasfsdfs.com" };
    		String sendCC = "";
    		String subject = "";
    		String message = "";
    		String attachment = "C:\\fred.txt";
    
    		Properties prop = System.getProperties();
    		Session session = Session.getInstance(prop, null);
    		Message msg = new MimeMessage(session);
    		prop.put("mail.smtp.host", mailhost);
    		msg.setFrom(new InternetAddress(from));
    		for (String to : sendTo) {
    			msg.setRecipients(Message.RecipientType.TO,
    					InternetAddress.parse(to, false));
    			if (sendCC != null && sendCC.length() > 0)
    				msg.setRecipients(Message.RecipientType.CC,
    						InternetAddress.parse(sendCC, false));
    			msg.setSubject(subject);
    			msg.setSentDate(Calendar.getInstance().getTime());
    
    			MimeBodyPart messageBodyPart = new MimeBodyPart();
    
    			messageBodyPart.setContent(message, "text/html");
    			Multipart multipart = new MimeMultipart();
    
    			multipart.addBodyPart(messageBodyPart);
    			
    			// Attachment
    			messageBodyPart = new MimeBodyPart();
    			DataSource source = new FileDataSource(attachment);
    			messageBodyPart.setDataHandler(new DataHandler(source));
    			messageBodyPart.setFileName(attachment);
    			multipart.addBodyPart(messageBodyPart);
    
    			msg.setContent(multipart);
    			Transport.send(msg);
    		}
    	}

  2. #2
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: Sending a data structure as an attachment via JavaMail

    bump bumpbump bump

  3. #3
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Sending a data structure as an attachment via JavaMail

    Try serializing the object...

Similar Threads

  1. JavaMail - Problem while sending file attachment
    By saniruddhabiswas in forum Networking
    Replies: 0
    Last Post: 06-13-2010, 12:16 PM
  2. Deleting an attachment after sending mail
    By modestmj in forum Java Servlet
    Replies: 2
    Last Post: 06-02-2010, 12:52 PM
  3. Replies: 1
    Last Post: 03-05-2010, 03:05 AM
  4. Sending email with JavaMail
    By eponcedeleon in forum Advanced Java
    Replies: 2
    Last Post: 03-03-2010, 08:32 PM
  5. Sending MTOM Attachment through SOAP message
    By suchismitasuchi in forum Advanced Java
    Replies: 0
    Last Post: 01-19-2009, 02:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •