簡體   English   中英

如何使用 Java 發送帶有 MM7 的彩信?

[英]How to send MMS with MM7 using Java?

請讓我知道有沒有辦法在 Java 中使用 MM7 協議發送彩信?

如果有免費的 API 可以生成適當的 SOAP 消息,請也告訴我。 我處於死胡同,我真的需要一種方法來做到這一點。 也歡迎您提出明智的建議。

預先感謝。

MMS Soap 應如下所示: 3GPP MM7 SOAP Spesicifation

這是我的做法:

您將需要 activation.jar、saaj-api.jar、activation.jar 和 saaj-impl.jar(以及任何其他相關的 jar 文件)。

這是您可以開始的方法。

package com.pixelandtag.mms.soap;

import java.io.IOException;
import java.net.URL;
import java.sql.Timestamp;
import javax.activation.DataHandler;
import javax.xml.soap.AttachmentPart;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;



public class MMSGen {

private static final String TxId = "";
private static final String MM7_VERSION = "";
private static final String VASPID = "";
private static final String VASID = "";
private static final String SHORTCODE = "";
private static final String SERVICE_CODE = "";
private static final String LINKED_ID = "";
private static final String MMSCMPMMS0000 = "";
private static final String EARLIEST_DELIVERY_TIME = "2006-11-09T13:01:04+03:00";
private static final String EXPIRY_DATE = "2007-11-10T13:01:04+03:00";
private static final String DELIVERY_REPORT = "false";
private static final String READ_REPLY = "false";
private static final String SUBJECT = "";
private static final String DISTRIBUTION_INDICATION = "false";


/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    
    
    try {
        
        //SOAP Message created
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage(); 
        
        //Now accessing the elements of the soap message
        SOAPPart soapPart = message.getSOAPPart(); 
        
        //Get the envelope
        SOAPEnvelope envelope = soapPart.getEnvelope(); 
        
        
        //You can now use the getHeader and getBody methods 
        //of envelope to retrieve its empty SOAPHeader and SOAPBody objects.
        SOAPHeader header = envelope.getHeader();
        SOAPBody body = envelope.getBody();
        
        //Other ways of getting the body and header
        //header = message.getSOAPHeader();
        //body = message.getSOAPBody();
        
        
        //Deleting a node.
        //header.detachNode(); 
        SOAPFactory soapFactory = SOAPFactory.newInstance();
         
        //================CONSTRUCTING HEADER
        Name headerName = soapFactory.createName("TransactionID",
                  "mm7", "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2");
         
        SOAPHeaderElement headerElement = header.addHeaderElement(headerName); 
        headerElement.setTextContent("TID."+TxId);
        headerElement.setMustUnderstand(true);
        //===============HEADER DONE======================
        
        
        
        
        //===============CONSTRUCTING BODY==============
        Name bodyName = soapFactory.createName("SubmitReq");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        Name attributeName = envelope.createName("xmlsn");//change name to proper to xmls, though it does not print out
        bodyElement.addAttribute(attributeName, "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2");
        
        
        bodyName = soapFactory.createName("MM7Version");
        SOAPElement MM7Version =  bodyElement.addChildElement(bodyName);
        MM7Version.setTextContent(MM7_VERSION);
        
        bodyName = soapFactory.createName("SenderIdentification");
        SOAPElement SenderIdentification =  bodyElement.addChildElement(bodyName);
        
        bodyName = soapFactory.createName("SenderAddress");
        SOAPElement senderAddress =  SenderIdentification.addChildElement(bodyName);
        
        bodyName = soapFactory.createName("ShortCode");
        SOAPElement shortCode =  senderAddress.addChildElement(bodyName);
        shortCode.setTextContent(SHORTCODE);
        
        
        bodyName = soapFactory.createName("Recipients");
        SOAPElement recipients =  bodyElement.addChildElement(bodyName);
       
        
        bodyName = soapFactory.createName("To");
        SOAPElement to =  recipients.addChildElement(bodyName);
        
        
        bodyName = soapFactory.createName("Number");
        SOAPElement number =  to.addChildElement(bodyName);
        number.setTextContent(MMSCMPMMS0000);
        
        
        bodyName = soapFactory.createName("VASPID");
        SOAPElement vaspID =  SenderIdentification.addChildElement(bodyName);
        vaspID.setTextContent(VASPID);
        
        bodyName = soapFactory.createName("VASID");
        SOAPElement vasID =  SenderIdentification.addChildElement(bodyName);
        vasID.setTextContent(VASID);
        
        
        bodyName = soapFactory.createName("ServiceCode");
        SOAPElement serviceCode =  bodyElement.addChildElement(bodyName);
        serviceCode.setTextContent(SERVICE_CODE);
        
        
        bodyName = soapFactory.createName("LinkedID");
        SOAPElement linkedID =  bodyElement.addChildElement(bodyName);
        linkedID.setTextContent(LINKED_ID);
        
        
        bodyName = soapFactory.createName("MessageClass");
        SOAPElement messageClass =  bodyElement.addChildElement(bodyName);
        messageClass.setTextContent(MessageClass.Personal.toString());
        
        
        bodyName = soapFactory.createName("EarliestDeliveryTime");
        SOAPElement earliestDeliveryTime =  bodyElement.addChildElement(bodyName);
        earliestDeliveryTime.setTextContent(EARLIEST_DELIVERY_TIME);
        
        
        bodyName = soapFactory.createName("ExpiryDate");
        SOAPElement expiryDate =  bodyElement.addChildElement(bodyName);
        expiryDate.setTextContent(EXPIRY_DATE);
        
        
        bodyName = soapFactory.createName("DeliveryReport");
        SOAPElement deliveryReport =  bodyElement.addChildElement(bodyName);
        deliveryReport.setTextContent(DELIVERY_REPORT);
        
        bodyName = soapFactory.createName("ReadReply");
        SOAPElement readReply =  bodyElement.addChildElement(bodyName);
        readReply.setTextContent(READ_REPLY);
        
        
        bodyName = soapFactory.createName("Priority");
        SOAPElement priority =  bodyElement.addChildElement(bodyName);
        priority.setTextContent(Priority.Normal.toString());
        
        
        bodyName = soapFactory.createName("Subject");
        SOAPElement subject =  bodyElement.addChildElement(bodyName);
        subject.setTextContent(SUBJECT);
        
        
        bodyName = soapFactory.createName("ChargedParty");
        SOAPElement chargedParty =  bodyElement.addChildElement(bodyName);
        chargedParty.setTextContent(ChargedParty.Recipient.toString());
        
        
        bodyName = soapFactory.createName("DistributionIndicator");
        SOAPElement distributionIndicator =  bodyElement.addChildElement(bodyName);
        distributionIndicator.setTextContent(DISTRIBUTION_INDICATION);
        
        
        bodyName = soapFactory.createName("Content");
        SOAPElement content =  bodyElement.addChildElement(bodyName);
        
        attributeName = envelope.createName("href");//change name to proper to xmls, though it does not print out
        SOAPElement attr = content.addAttribute(attributeName, "cid:A0");
        
        attributeName = envelope.createName("allowAdaptations");//change name to proper to xmls, though it does not print out
        attr = content.addAttribute(attributeName, "false");
        //=====================BODY DONE================
        
        
        
        //=================create attachment===========
        
       // AttachmentPart attachment = message.createAttachmentPart(); 
        
        
        //Simple attach
       /* String stringContent = "Update address for Sunny Skies " + 
        "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";

      attachment.setContent(stringContent, "application/smil");
      attachment.setContentId("update_address");

      message.addAttachmentPart(attachment); */
        
        
        URL url = new URL("http://www.petermak.nl/webalbum_peter_mak_aannemersbedrijf011002.jpg");
        DataHandler dataHandler = new DataHandler(url);
        AttachmentPart attachment1 = 
          message.createAttachmentPart(dataHandler);
       // attachment.setContent(stringContent, "application/smil");
        attachment1.setContentId("attached_image");

        message.addAttachmentPart(attachment1); 
        
        
        URL url2 = new URL("file:///C:\\Users\\Timo\\Desktop\\MMS Pis\\pic_test.jpg");
        dataHandler = new DataHandler(url2);
        AttachmentPart attachment2 = message.createAttachmentPart(
                    dataHandler);
        attachment2.setContentId("attached_image2");

        message.addAttachmentPart(attachment2);
        
        
        
        
        
        
        
       
        
       java.net.URL endpoint = new URL("http://theendpoint.");
      
      
      
        
       
         
       SOAPConnectionFactory soapConnectionFactory = 
              SOAPConnectionFactory.newInstance(); 
        
        SOAPConnection connection = 
              soapConnectionFactory.createConnection(); 
        SOAPMessage response = connection.call(message, endpoint); 
         
         
         System.out.println("\nRESPONSE:\n");
         response.writeTo(System.out);
         System.out.println();
         
         connection.close();
        
        
    
    } catch (SOAPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}


}

多年來,我不得不使用 MM7 協議連接到各種 MMSC。 SAAJ 方法是一種有效的方法,但有時您必須手動制作 MM7 消息,因為 MMSC 往往對精確的 MIME 格式、SOAP,甚至二進制與 base64 編碼都很挑剔。

在我的公司,我們開發了一個庫,它僅依賴於 JDOM 在低級別管理 MM7 SOAP 和附件。 無論實際的 MM7 協議版本/架構如何,它都能正常工作。 最近我們決定開源它,你可以在這里獲取它https://github.com/vnesek/instantcom-mm7

例子:

    SubmitReq sr = new SubmitReq();
    sr.setVaspId("xxx_vaspid");
    sr.setVasId("xxx_vasid");
    sr.setSubject("Nice weather");
    sr.setMessageClass(MessageClass.INFORMATIONAL);
    sr.setServiceCode("7007");
    sr.addRecipient(new Address("+385910000001", RecipientType.TO));

    // Add text content
    TextContent text = new TextContent("We got a real nice weather today.");
    text.setContentId("text");
    sr.setContent(text);

    // Initialize MM7 client to MMSC
    MMSC mmsc = new BasicMMSC(url);

    // Send a message
    SubmitRsp submitRsp = mmsc.submit(sr);
    System.out.println(submitRsp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM