Cannot send an MP3 file as email attachment...

Discussion in 'Java' started by juliekcf@gmail.com, Dec 11, 2006.

  1. Guest

    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();
    }
    }




    }
     
    , Dec 11, 2006
    #1
    1. Advertising

  2. M.J. Dance Guest

    wrote:
    > 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.J. Dance, Dec 11, 2006
    #2
    1. Advertising

  3. Guest

    M.J. Dance ¼g¹D¡G


    > 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/


    Thanks for your reference link. But do I still need to add anything to
    the mailcap file?
     
    , Dec 11, 2006
    #3
  4. M.J. Dance Guest

    wrote:
    > M.J. Dance ¼g¹D¡G
    >
    >
    >> 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/

    >
    > 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.
     
    M.J. Dance, Dec 11, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    1
    Views:
    424
  2. yihan
    Replies:
    3
    Views:
    508
    Thomas Weidenfeller
    Nov 10, 2004
  3. zhao wang

    How to send the email attachment

    zhao wang, Jan 3, 2005, in forum: Java
    Replies:
    2
    Views:
    438
    Ajith KR via JavaKB.com
    Jan 4, 2005
  4. Andrew Chan
    Replies:
    0
    Views:
    431
    Andrew Chan
    Dec 1, 2004
  5. Andrew Chan
    Replies:
    0
    Views:
    639
    Andrew Chan
    Dec 1, 2004
Loading...

Share This Page