Cannot send an MP3 file as email attachment...

J

juliekcf

I want to send an email with an MP3 file as attachment. If I do not
set content type, the file received will not be in the correct format.
But when I set content type to audio/mp3, I will receive
unsupporteddatatype exception. How can I send an mp3 file as an
attachment so that when I download the mp3 file from the email, the mp3
file will work and play as usual?

Someone said I may modify the activation.jar by editing the mailcap
file. However, I don't know what to add to the file. The file only
contains:

#
# This is a very simple 'mailcap' file
#
image/gif;; x-java-view=com.sun.activation.viewers.ImageViewer
image/jpeg;; x-java-view=com.sun.activation.viewers.ImageViewer
text/*;; x-java-view=com.sun.activation.viewers.TextViewer
text/*;; x-java-edit=com.sun.activation.viewers.TextEditor



My program is below:
=======================
package my;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
//import java.lang.Object.*;
//import javax.activation.CommandMap.*;
//import javax.activation.MailcapCommandMap.*;



public class rSendMail{
private String to;
private String from;
private String smtpServer="smtp.macau.ctm.net";
private String subject;
private String body;
private String contenttype;
private String attachfile;


public void setTo(String sTo)
{
to=sTo;
}

public void setFrom(String sFrom)
{
from=sFrom;
}

public void setSmtpServer(String sSmtpServer)
{
smtpServer=sSmtpServer;
}

public void setSubject(String sSubject)
{
subject=sSubject;
}

public void setBody(String sBody)
{
body=sBody;
}

public void setContentType(String sContenttype)
{
contenttype=sContenttype;
}

public void setAttachment(String sAttachment)
{
attachfile=sAttachment;
}


public void send(String path)
{
try
{

//MailcapCommandMap mc =
(MailcapCommandMap)CommandMap.getDefaultCommandMap();
//mc.addMailcap("text/html;;
x-java-content-handler=com.sun.mail.handlers.text_html");
//mc.addMailcap("text/xml;;
x-java-content-handler=com.sun.mail.handlers.text_xml");
//mc.addMailcap("text/plain;;
x-java-content-handler=com.sun.mail.handlers.text_plain");
//mc.addMailcap("multipart/*;;
x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
//mc.addMailcap("message/rfc822;;
x-java-content-handler=com.sun.mail.handlers.message_rfc822");
//CommandMap.setDefaultCommandMap(mc);

Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one
--
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);

// -- Create a new message --
Message msg = new MimeMessage(session);

// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));

msg.setSubject(subject);
msg.setText(body);
// StringBuffer sb = new StringBuffer();
// sb.append("<a href='"+path+"'>HERE</a>");
// msg.setText(body+sb.toString());

// msg.setContent(attachfile,contenttype);
// msg.setFileName(attachfile);


// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());

// -- Send the message --
Transport.send(msg);

// System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}




}
 
M

M.J. Dance

I want to send an email with an MP3 file as attachment. If I do not
set content type, the file received will not be in the correct format.
But when I set content type to audio/mp3, I will receive
unsupporteddatatype exception. How can I send an mp3 file as an
attachment so that when I download the mp3 file from the email, the mp3
file will work and play as usual?

Someone said I may modify the activation.jar by editing the mailcap
file. However, I don't know what to add to the file. The file only
contains:

[snip]

You cannot send an attachment with plain-text message. You have to wrap it into
a MIME (multipart) format. See: http://jakarta.apache.org/commons/email/
 
M

M.J. Dance

M.J. Dance ¼g¹D¡G



Thanks for your reference link. But do I still need to add anything to
the mailcap file?

MailCap* is something straight out of 1.6. Which was just released, BTW. I
haven't used it, yet, so I wouldnt't want to give advice about it. If you use
Apache's email/commons, you don't need it anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top