我想在Java中使用MAPISendMailW。该函数需要我在Java代码中定义的MapiMessageW结构。文档中的C ++代码如下所示(https://msdn.microsoft.com/en-us/library/hh707273(v=vs.85).aspx):
typedef struct {
ULONG ulReserved;
PWSTR lpszSubject;
PWSTR lpszNoteText;
PWSTR lpszMessageType;
PWSTR lpszDateReceived;
PWSTR lpszConversationID;
FLAGS flFlags;
lpMapiRecipDescW lpOriginator;
ULONG nRecipCount;
lpMapiRecipDescW lpRecips;
ULONG nFileCount;
lpMapiFileDescW lpFiles;
} MapiMessageW, *lpMapiMessageW;
PWSTR被定义为指向16位Unicode字符串的指针(https://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx#PWSTR)。
我想传递的字符串,例如lpszSubject包含Unicode字符 - 例如String myString =“带有č的句子”。根据我的理解,Java使用UTF-16 for Strings。但是当我尝试像传递myString一样传递myString时,它只会在邮件中显示垃圾。 当我尝试从String中首先获取字节然后发送 new String(subjectBytes,“UTF-16”))时,会发生同样的情况。
要传递PWSTR值的java端的正确数据类型是什么?
编辑:根据评论中的要求 - jave代码
这是我定义MapiMessageW的地方:
class MapiMessageW extends Structure {
public NativeLong ulReserved;
public String lpszSubject;
public String lpszNoteText;
public String lpszMessageType;
public String lpszDateReceived;
public String lpszConversationID;
public NativeLong flFlags;
public Pointer lpOriginator;
public NativeLong nRecipCount;
public MapiRecipDescWs lpRecips;
public NativeLong nFileCount;
public MapiFileDescWs lpFiles;
[...]
}
在这里填写值 - getSubject()和getBody()都返回一个字符串:
MapiMessageW message = new MapiMessageW();
message.lpszSubject = new String(email.getSubject());
message.lpszNoteText = new String(email.getBody());
message.nRecipCount = new NativeLong(recipDescs.size());
message.lpRecips = new MapiRecipDescWs(recipDescs);
message.nFileCount = new NativeLong(fileDescs.size());
message.lpFiles = new MapiFileDescWs(fileDescs);
在此处调用MapiSendMailW,并将消息作为参数:
rc = Mapi32W.INSTANCE.MAPISendMailW(sessionPtr.getValue(), null, message,
new NativeLong(Mapi32W.MAPI_DIALOG | Mapi32W.MAPI_LOGON_UI), null).longValue();