使用EWS Java API 1.1的exchange.webservices.data.ServiceRequestException

时间:2012-01-30 15:46:30

标签: java exchange-server exchangewebservices ewsjavaapi

我一直在尝试使用Exchange Web服务API创建和发送电子邮件。 send()命令始终抛出下面显示的异常。有没有人有这个工作?

Exception in thread "main" microsoft.exchange.webservices.data.ServiceRequestException: The request failed. ParseError at [row,col]:[1,1]
Message: Premature end of file.
        at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(Unknown Source)
        at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(Unknown Source)
        at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(Unknown Source)
        at microsoft.exchange.webservices.data.ExchangeService.createItem(Unknown Source)
        at microsoft.exchange.webservices.data.Item.internalCreate(Unknown Source)
        at microsoft.exchange.webservices.data.EmailMessage.internalSend(Unknown Source)
        at microsoft.exchange.webservices.data.EmailMessage.send(Unknown Source)
        at edu.jhmi.cidr.utils.mail.EwsSendMessage.createAndSendMessage(EwsSendMessage.java:40)
        at edu.jhmi.cidr.utils.mail.EwsSendMessage.main(EwsSendMessage.java:47)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
        at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
        at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)
        at microsoft.exchange.webservices.data.EwsXmlReader.read(Unknown Source)
        at microsoft.exchange.webservices.data.EwsXmlReader.read(Unknown Source)
        at microsoft.exchange.webservices.data.EwsXmlReader.internalReadElement(Unknown Source)
        at microsoft.exchange.webservices.data.EwsXmlReader.readStartElement(Unknown Source)
        at microsoft.exchange.webservices.data.ServiceRequestBase.readResponse(Unknown Source)
        at microsoft.exchange.webservices.data.SimpleServiceRequestBase.readResponse(Unknown Source)
        ... 9 more
import java.net.URI;
import microsoft.exchange.webservices.data.BodyType;
import microsoft.exchange.webservices.data.EmailAddress;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.WebCredentials;

public class EwsSendMessage {

    private final String eMailAddress;
    private final String pwd;
    private final String serverName;

    public EwsSendMessage(String eMailAddress, String pwd, String serverName) {
        this.eMailAddress = eMailAddress;
        this.pwd = pwd;
        this.serverName = serverName;
    }

    public void createAndSendMessage() throws Exception {
        ExchangeService service = new ExchangeService();
        ExchangeCredentials credentials = new WebCredentials(
                eMailAddress, pwd);
        service.setCredentials(credentials);
        // AutoDiscover service is turned off; must set URL manually
        service.setUrl(new URI(serverName));
        EmailMessage msg = new EmailMessage(service);
        EmailAddress senderAddress = new EmailAddress(eMailAddress);
        msg.setSender(senderAddress);
        EmailAddress recipientAddress = senderAddress;
        msg.getToRecipients().add(recipientAddress);
        msg.setSubject("Hello world!");
        MessageBody body = new MessageBody("This is the message body");
        body.setBodyType(BodyType.Text);
        msg.setBody(body);
        msg.send();
    }

    public static void main(String[] args) throws Exception {
        String eMailAddress = args[0];
        String pwd = args[1];
        String serverName = args[2];
        new EwsSendMessage(eMailAddress, pwd, serverName).createAndSendMessage();
    }
}

0 个答案:

没有答案