Setting attachment content-type manually?

Hello,
I am trying to write code that will allow a user to specify the content-type of an attachment when s/he adds it to an email. The following code, which appears to let Java figure out the content-type from the file extension, works just fine:
Multipart multipart = new MimeMultipart();
// Part one is the text
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setText("Some text");
multipart.addBodyPart(bodyPart);
// Part two is the attachment
bodyPart = new MimeBodyPart();
DataSource sourceFile = new FileDataSource(FileName);
bodyPart.setDataHandler(new DataHandler(sourceFile));
multipart.addBodyPart(bodyPart);
message.setContent(multipart);
I attached a .exe file, passed the DataSource into the DataHandler constructor, and it sent the email as expected, with a Content-Type of application/octet-stream. However, if I do the following instead:
bodyPart.setContent(fileDataSource, "application/octet-stream");
-or-
bodyPart.setDataHandler(new DataHandler(fileDataSource, "application/octet-stream");
I get the dreaded javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/octet-stream. Why do I get this exception when using setContent, but not when I use the DataHandler? It seems it has the same Content-Type either way.
-Aaron

Well, I figured out the answer myself by digging into the source code.
When you create a DataHandler by using the (Object, String) constructor, it expects an Object that is not a DataSource, or so it would seem. So I abducted the BinaryArrayDataSource from the demo classes and instantiated one of it, specifying my desired content type and passing in the FileInputStream from the file that I was creating my FileDataSource from, and it worked like a champ.
So, the moral is, use DataSources whenever possible!
-Aaron

Similar Messages

  • How Do I Set XML Content-type in Flex 3

    Hi,
    My Flex 3 project makes an ExternalInterface call to some Javascript. The Javascript is then turned into XML.
    But, my xml isn't parsing in IE. It works in all other browsers. I think that the problem is that I haven't set the XML's content-type. As I understand it IE requires that the Content-type be set. Is this correct?
    My code looks like:
    myReturn =  '<myXMLReturn>' + myReturn + '</myXMLReturn>';
    myReturn = '<?xml version="1.0" encoding="UTF-8"?>' + myReturn;
    xmlReturn = new XML(myReturn);
    How would I set: header('Content-type: application/xml'); for an ExternalInterface call?
    Thank you.
    -Laxmidi

    Okay. I figured it out. I don't need to set the Content-type.
    I had a case sensitivity problem in parsing the xml. Thank you.
    -Laxmidi

  • How do I set the content type ?

    Hi
    I have to upload a file from my Java Application to a remote servlet.
    I have used the HTTPConnection class to connect to the remote servlet and made it call the POST method explicitly.
    How do I set the content type of the file I am uploading from my Java apllication?
    I tried to change the content type in the doPost method. by giving
    req.setContentType("multipart/form-data"); in the doPost.
    It still doesnt work..
    Is there any way to set the content type in the Java application?
    Thanks in advance

    What class is your req variable? If your using a HttpServletRequest, then there is no method req.setContentType("");
    In the HttpServletResponse this is for telling the web browser or receving medium what kind of mime type to expect.
    And what package is the HttpConnection class from?
    I think more precise info is required before this question can be answered.

  • Cfhttp and setting the content-type for a request

    Hi,
    I am trying to set the content-type for a cfhttp request like this (on coldfusion 9):
    <cfhttp redirect="no" method="get" timeout="120" url="http://10.0.0.1/test2.cfm">
        <cfhttpparam type="HEADER" name="Content-Type" value="application/json; charset=utf-8">
    </cfhttp>
    // coding on test2.cfm:
    <cfset x = GetHttpRequestData()>
    <cfdump var=#x#>
    // Output on test2.cfm
    content
    [empty string]
    headers
    struct
    Accept-Encoding
    deflate, gzip, x-gzip, compress, x-compress
    Connection
    TE
    Host
    10.0.0.1:80
    Proxy-Connection
    close
    TE
    trailers, deflate, gzip, compress
    User-Agent
    ColdFusion
    X-REWRITE-URL
    http://10.0.0.1:80/test2.cfm
    method
    GET
    protocol
    HTTP/1.1
    As you can see no content-type is send through. I also tried the sniffer.exe:
    GET http://10.0.0.1:80/test2.cfm HTTP/1.1
    Host: 127.0.0.1
    Proxy-Connection: close
    Connection: TE
    TE: trailers, deflate, gzip, compress
    User-Agent: ColdFusion
    Accept-Encoding: deflate, gzip, x-gzip, compress, x-compress
    Here you can also see that no content-type was passed through. The sniffer should report back
    GET http://10.0.0.1:80/test2.cfm HTTP/1.1
    Host: 127.0.0.1
    Proxy-Connection: close
    Connection: TE
    TE: trailers, deflate, gzip, compress
    User-Agent: ColdFusion
    Accept-Encoding: deflate, gzip, x-gzip, compress, x-compress
    Content-Type: application/json; charset=utf-8
    But is does not, what do i need todo to set the content-type in a cfhttp request.
    Kind regards,
    Nebu

    12Robots - are you sure about your statement that GET method requests cannot have Content-Type headers?  I don't think that that is correct.  I've always thought that it was valid (maybe unusual, but ...) and this W3C link weems to agree, especially in the context of its comment about what Content-Type means in a HEAD method request: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    The big problem with trying to get around the CFHTTP limitation by using the CGI scope is that the dash is illegal in a CF varname.
    There is a custom tag called CFHTTP5 that getsaround a lot of the CFHTTP limitations, including cookie preservation across multiple requests.  I haven't used it for a long time (CF5), but it is still actively developed.  Costs $50 - http://www.cftagstore.com/tags/cfxhttp5.cfm
    -reed

  • Setting the content type inside the ABAP mapping.

    Hello All,
    Can any one help me by telling me how to set the content-type inside ABAP mapping. I have searched a large no of blogs and help.sap.com for the same.
    My requirement is I want to change the MIME type of the Main Document in side SXMB_MONI so that can change the content type of Main document which is application\xml by default.
    I would appreciate an early response regarding the same.
    Abinash

    Hello Raj,
    The output of my ABAP mapping is a flat file. So when I try to open the file with content type application/xml in the message monitor it shows the file has some error which is natural. The way to get around this problem is change the content type to application\txt from application\xml.
    I am not finding an option to set the content type inside the ABAP mapping for the same.
    Abinash

  • Document Set default content type

    When defining a document set, you can define what content types can be used in a document set.  If, after uploading a document, the user cancels the properties page, the default content type for the library is assigned to the document (even though the
    document set does not permit the content type).  This is obviously a bug.  There have been several posts on this and it appears 2013 has the bug as well as 2010.
    We are seeking a work around to this bug. 
    Is it possible in in the client object model by placing a CEWP in the editform.aspx to force a content type on the item when the form is loaded?
    The other option is to modifiy the url that opens EditForm.aspx to force the content type.  Unfortunately the link that opens the EditForm.aspx page cannot be modified in the document library.

    Hi Clem_FRB,
    I did a test in my environment and I reproduce this issue.
    As a workaround, I suggest you change the content type or return an error in ItemAdded Event Receiver for document library.
    Here is a similar thread for you reference:
    http://sharepoint.stackexchange.com/questions/21986/document-sets-dont-enforce-allowed-content-types-when-multiple-doc-set-ct-are-a
    More information for your reference:
    http://www.niteenbadgujar.com/2013/05/change-default-content-type.html
    https://social.technet.microsoft.com/Forums/en-US/281fa281-7071-4beb-b275-e3179ce2cc08/how-to-set-default-content-type-in-document-library-using-client-object-model?forum=sharepointdevelopmentprevious
    Best Regards
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Zhengyu Guo
    TechNet Community Support

  • Using Forms that don't set the content type charset

    The Servlet API 2.3 has a new method, request.setCharacterEncoding(), to
              deal with the fact that almost no browsers set the content type. When I call
              this method passing "Shift_JIS" using weblogic 6.1, it works fine when the
              content type is not set by the browser. That is, after calling
              setCharacterEncoding when I call request.getParameter("testparam") I get the
              correct translated value. However, if the content type is
              "application/x-www-form-urlencoded; charset=Shift_JIS" and I call
              request.setCharacterEncoding("Shift_JIS"), getParameter("testparam") returns
              null.
              Can anyone shed some light on this?
              It looks like I'll have to not set the character encoding if it is already
              set. That, of course, opens the possibility that the character set used for
              translation is different than was used in the form.
              Russ Cole
              [email protected]
              

    It might not be the new 3G network. Though I've heard the major metropolitan cities can have slower responses due to congestion.
    Have you upgraded to the new operating system? Have you tried resetting your phone? You can do that by holding the "Home" and "Power" buttons down together, through the shut down screen and until the apple appears on the screen.
    You might also try this:
    http://www.roughlydrafted.com/2010/07/07/how-to-speed-up-your-iphone-3g-running- ios4/
    It's called a cold reboot.
    Sharon

  • Sun BPEL engine sets wrong Content-Type on calling an external web service

    I have a WSDL for an external web service (which as it happens is running in Websphere locally) and have implemented it as a
    partnerlink on a BPEL diagram in an SOA composite project. The binding style is Document and if you use SOAPUI to test it it
    works fine. Tracing on the web service's server shows the SOAP request coming in. It has a content-type of text/xml, as expected. If I do a JUnit test from NetBeans based on this WSDL alone it also works - same result. BUT if I do
    a full test and call my BPEL process the invocation of this service from BPEL fails.
    On the Test result you see
    BPCOR-6135:A fault was not handled in the process scope; Fault Name is http://www.sun.com/wsbpel/2.0/process/executable/SUNExtension/ErrorHandling}systemFault;
    (This happens because I did not handle the fault in the BPEL.)
    On the trace I see it now has Content-type of application/xop+xml. Why would the Sun BPEL engine change this? I just
    accepted the usual defaults when I created the PartnerLink and the Invoke. Anyway, the format of the request doesn't match what the WS server expects, so it fails.
    As far as I'm aware I'm just using SOAP1.1. This is NetBeans 6.5 with Glassfish 2.1. I tried with NetBeans 6.7 and it got worse, not better. I would certainly appreciate any advice?
    Thanks

    Perhaps when the BPEL file based process calls the WSDL file to execute,their message communication is not correct.
    the head part in the WSDL file should be manually set to "text/xml", or the IDE or Server will use the default value;
    for example as follow:
    SOAPUI tool would use the default value "text/xml" to request.(depends the tool support).
    NetBeans tool would use the default value "text/xml" to request.
    when you do a full test and you do not set the head "context-type" value ,the WSDL file use the server's default head
    value"application/xop-xml" so that return the contents under the control of the server's default value.
    I hope these will be useful for you.

  • BPEL engine sets wrong Content-Type on calling an external web service

    I have a WSDL for an external web service (which as it happens is running in Websphere locally) and have implemented it as a
    partnerlink on a BPEL diagram in an SOA composite project.
    The binding style is Document and if you use SOAPUI to test it it
    works fine. Tracing on the web service's server shows the SOAP request
    coming in. It has a content-type of text/xml, as expected. If I do a
    JUnit test from NetBeans based on this WSDL alone it also works - same result. BUT if I do
    a full test and call my BPEL process the invocation of this service
    from BPEL fails.
    On the Test result you see
    BPCOR-6135:A fault was not handled in the process scope; Fault Name is http://www.sun.com/wsbpel/2.0/process/executable/SUNExtension/ErrorHandling}systemFault;
    (This happens because I did not handle the fault in the BPEL.)
    On the trace I see it now has Content-type of
    application/xop+xml. Why would the Sun BPEL engine change this? I just
    accepted the usual defaults when I created the PartnerLink and the
    Invoke. Anyway, the format of the request doesn't match what the WS server expects, so it fails.
    As far as I'm aware I'm just using SOAP1.1. This is NetBeans 6.5 with
    Glassfish 2.1. I tried with NetBeans 6.7 and it got worse, not better.
    I would certainly appreciate any advice?
    Thanks

    Perhaps when the BPEL file based process calls the WSDL file to execute,their message communication is not correct.
    the head part in the WSDL file should be manually set to "text/xml", or the IDE or Server will use the default value;
    for example as follow:
    SOAPUI tool would use the default value "text/xml" to request.(depends the tool support).
    NetBeans tool would use the default value "text/xml" to request.
    when you do a full test and you do not set the head "context-type" value ,the WSDL file use the server's default head
    value"application/xop-xml" so that return the contents under the control of the server's default value.
    I hope these will be useful for you.

  • Save Dialog in Office Application does not set the Content Type to the one selected on Document Creation.

    We have a Library that supports 4 Content Types  ("Content Type 1","Content Type 2","Content Type 3","Content Type 4")
    The user clicks new and selects "Content Type 3"
    Word is started
    The user edits the document
    Clicks Save
    "Choose Content Type" dialog is shown set to the Content Type "Content Type 1" because the Document content type is ordered as First (which also means Default)
    What is expected is the "Choose Content Type" dialog to show "Content Type 3" as selected
    Any ideas?

    Hi,
    According to your description, there is a library with four content types, you create a document with one of the content types, click save button in Office application,
    then a “Choose Content Type” dialog will show up for selecting a content type.
    I tried to reproduce as below:
    1. Create a library with four content types;
    2. In the ribbon of this library, “FILES”->”New Document”->”Content Type 1”, then the Office application with the predefined template will be opened for editing;
    3. Click the “Save” button, choose the save path as the current library, click “OK” to finish the process;
    4. Refresh the library in browser, the newly created document appears there.
    In my test, there is no “Choose Content Type” dialog showing up when save the document in Office application.
    I would suggest you provide more details about how to reproduce this issue(screenshot would be better) for further research.
    Thanks
    Patrick Liang
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support,
    contact [email protected]
    Patrick Liang
    TechNet Community Support

  • Document Sets and Content Types

    Hi All,
    I am having trouble in getting the "document sets" content types applied as expected when I try to create a site based off custom template.
    Scenario:
    We have 8 document sets stored in a document library. This library is part of a site which we have saved as a template.
    E.g. of document sets is "Others", "Meetings" etc
    Each of these document sets have one or more content types associated with it.
    We are trying to create a site based off the saved site template using powershell.
    E.g. $web = New-SPWeb http://myserver/sites/mysite/subsite
    $web.ApplyWebTemplate("{GUID}#Name of Template")
    When we open the document sets in this newly created site, the content types seem to be coming all together rather than being associated with the specific document set.
    E.g. Clicking "New document" in the "others" document set would show up content types from "meeting" and all the remaining document sets present in that library.
    Interestingly, when I go to "Document Set" settings, I only see the content type of "Others" Document Set.
    Any idea why the "New document" dropdown for the document set is not behaving as expected?

    Hi Abhi,
    Based on your description, I did a test. In my testing, everything worked well.
    The steps are:
    Go to Site Actions->Site Settings->Site content types.
    Create a new content type, and select parent content type from Document Set Content Types, and Parent Content Type: Document Set
    Click ok
    Under Settings, click Document Set settings
    On the Allowed Content Type section, select some content types and add them
    Click OK
    Then repeat step2-6, and create another custom document set
    Open your library, and allow management of content types in advanced settings
    Add the two document sets into the library
    Check each document set, and each one had different content types
    Save the site as a template and include the content
    Create a new site based on the site template using PowerShell(the command was same with yours)
    After creating, check the document sets of the library in the new site, everything worked well.
    Please check your process as the above steps.
    I hope this helps.
    Thanks,
    Wendy
    Wendy Li
    TechNet Community Support

  • Document Set custom content type - welcome page missing

    Sharepoint 2013. 404 error on welcome page for a following content type based on document
    set:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Field ID="{B9B7D98D-A2D3-4191-9B30-C516C8EDD9F4}" DisplayName="Fieldpr41" Name="Fieldpr41" Type="Note" RichText="false" NumLines="3" Group="pr41" Overwrite="TRUE" />
    <!-- Parent ContentType: Document Set (0x0120D520) -->
    <ContentType ID="0x0120D5200062FEFD4873814585B3EFD57010F1F8AE"
    Name="ContentType41"
    Group="Custom Content Types"
    Description="My Content Type"
    Inherits="FALSE"
    ProgId="SharePoint.DocumentSet"
    Version="0">
    <FieldRefs>
    <FieldRef ID="{B9B7D98D-A2D3-4191-9B30-C516C8EDD9F4}" DisplayName="Fieldpr41" Name="Fieldpr41" />
    </FieldRefs>
    <XmlDocuments>
    <XmlDocument
    NamespaceURI=
    "http://schemas.microsoft.com/office/documentsets/welcomepagefields">
    <wpFields:WelcomePageFields
    xmlns:wpFields=
    "http://schemas.microsoft.com/office/documentsets/welcomepagefields"
    LastModified="1/1/2010 08:00:00 AM">
    <WelcomePageField id="83729202-DCA7-4BF8-A75B-56DDDE53189C" />
    </wpFields:WelcomePageFields>
    </XmlDocument>
    </XmlDocuments>
    </ContentType>
    </Elements>
    Here's
    what i did after changing Inherits parameter to FALSE: 1) added ProgId parameter 2) added wpFields section 
    Any ideas?

    Hi,
    According to your post, my understanding is that you got an error while creating custom document set content type.
    The issue is that you set the Inherits=”FALSE” in the element. If you set the Inherits=”TRUE”, the content type would work.
    Based on the article form MSDN:
    If Inherits is TRUE, the child content type inherits all fields that are in the parent, including fields that users have added.
    If Inherits is FALSE or absent and the parent content type is a built-in type, the child content type inherits only the fields that were in the parent content type when SharePoint Foundation was installed.
    The child content type does not have any fields that users have added to the parent content type.
    If Inherits is FALSE or absent and the parent content type was provisioned by a sandboxed solution, the child does not inherit any fields from the parent.
    It means if you set the Inherits to true, we would lose all our customization there.
    If we set Inherits="FALSE", which means you then have to remember to explicitly add into the definition all the stuff you should be inheriting, like the default Doc Set event receivers. 
    More reference:
    http://morefunthanapokeintheeye.blogspot.com/2012/10/how-to-successfully-provision-and.html
    http://ybbest.wordpress.com/2012/07/04/how-to-deploy-document-set-using-caml-in-sharepoint2010-solution-package/
    Thanks & Regards,
    Jason
    Jason Guo
    TechNet Community Support

  • Setting DCP content type in Premiere

    Hey all,
    I'm trying to export a trailer in DCP form from Premiere using the Wraptor DCP format -- no problems with the end result except the cinema are saying it's coming up as a feature on their system, rather than a trailer or advertisement. Is there a way to specify the content type within the Premiere export settings? Cheers.

    Frank,
    Thanks you for you help.
    What i would like to do is to use a JSF library components to build YahooGO pages (Yahoo mobile widgets).
    For this my server needs to return an xml document with the application/x-ywidget+xml content type.
    Regarding you question i don't think that the target device understand the HTML.. on my mobile, it's J2ME application...
    Perhaps you can provide me some advices.
    Regards,
    Fred

  • How to set content type for the attachment?

    Hello Everybody,
    I was trying to do the following in my send mail program:-
    1. I am making a file using FileWriter:
    FileWriter fw = new FileWriter(f);
    fw.write(str);
    2. I now want to attach this file with my mail. but before that I want to set the Content type of this as "application/smil". and trying the following code:
    mimebodyparts[0].setContent(fds[0],"application/smil");
    As this is a new MIME type perhaps I need to change the mailcap file. I found the same in the activation jar package but thought instead of chagning the default file i tried putting following code in my program:
                  File capfile = new File("simple.mailcap");
                  if (!capfile.isFile())
                        System.out.println("Cannot locate the \"simple.mailcap\" file.");
                        System.exit(1);
                  CommandMap.setDefaultCommandMap( new MailcapCommandMap(new FileInputStream(capfile)));
    and in simple.mailcap file put the following value
    application/smil;;     x-java-content-handler=com.sun.mail.handlers.application_smil  but I am still getting the following error:
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type application/smil
    Can somebody Please help to sort this out.
    regards,
    Arun

    //add related mutip-part to combine parts
    MimeMultipart multipart = new MimeMultipart("related");
    //attach a pdf
    messageBodyPart = new MimeBodyPart();
    fds = new FileDataSource("h:/something.pdf");
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setFileName(fds.getName());
    multipart.addBodyPart(messageBodyPart);
    //add multipart to the message
    message.setContent(multipart);
    //send message
    Transport.send(message);

  • How to set Content-Type of attachment?

    Does anyone know how I can set the Content-Type of an attachment of an outgoing mail?
    I use Apple Mail, and with one correspondent I have this annoying problem that he never receives any excel or word attachments that I send (and possibly other ones, too).
    Our support now says that I should try to set the Content-Type
    of the excel attachments to "Content-Type: application/vnd.ms-excel".
    Apple Mail seems to set it to
    "Content-Type: application/octet-stream"
    and they think that attachments with this content-type might get blocked by the mail server of my correspondent.
    Any suggestions, ideas, etc., will be appreciated.
    Best regards,
    Gabriel.

    Thanks a lot for your test.
    Unfortunately, I get
    Content-Type: application/octet-stream;
    when I try to send an excel attachment to myself.
    Below is the beginning of the email (in "source" format).
    Any ideas why you get the correct mime-type, but I don't?
    Regards,
    Gabriel.
    ---------- beginning of email
    Return-Path: <[email protected]>
    Received: from [80.128.100.143] (account xxxxxxx HELO [192.168.2.100])
    by tu-clausthal.de (CommuniGate Pro SMTP 5.2.4)
    with ESMTPSA id 35503032 for [email protected]; Mon, 30 Jun 2008 21:59:05 +0200
    Message-Id: <[email protected]>
    From: Gabriel Zachmann <[email protected]>
    To: Gabriel Zachmann <[email protected]>
    Content-Type: multipart/signed; boundary=Apple-Mail-13-325543015; micalg=sha1; protocol="application/pkcs7-signature"
    Mime-Version: 1.0 (Apple Message framework v924)
    Subject: test
    Date: Mon, 30 Jun 2008 21:59:04 +0200
    X-Mailer: Apple Mail (2.924)
    --Apple-Mail-13-325543015
    Content-Type: multipart/mixed;
    boundary=Apple-Mail-12-325542957
    --Apple-Mail-12-325542957
    Content-Disposition: attachment;
    filename="Kostenabfrage AVILUS Gesamtinklusive_TPxyz 080606.xls"
    Content-Type: application/octet-stream;
    x-mac-creator=5843454C;
    x-unix-mode=0644;
    x-mac-type=584C5338;
    name="Gesamt.xls"
    Content-Transfer-Encoding: base64

Maybe you are looking for

  • Audio level driven by marker

    Hey all, 3 pronged this..... I would like to set markers on my audio layer that will drive its volume. So for example I have a clip that comes in with a voice over and I would like the background audio bed to drop down quiet whilst the voice is on an

  • Set current record after dataset filter or sort

    How do you set the current record as the first record after a filter or sort? I have multiple dataset filters and sorts on the page and the current record always remains the first record after using them. Is there a way to force the current(viewed pr

  • My slideshows don't fill my tv screen any longer

    I have PSE 9 and PRE9 and have made several slideshows in the past. I create and edit the slideshows in elements and burn them thru premiere. They all have played in my DVD player with no problems and have all filled the entire television screen. How

  • Mediamind not show in Flash CS5

    Hi guys, The .mxp is installed (showing up in extension manager) but not showing up in Flash. Tried restarting Flash, un-installing + re-installing .mxp, reboot. only see the old version of eyeblaster workshop...I've read the related forum topics and

  • Em in browser

    Hi, after installing DB 10g on Win 2003 (on a virtual machine), when browsing to http://server:1158/em It is indicated Done in left hand of task bar, but nothing in the page, just a white page. What can be the problem ? Should I install IIS, or somet