gpt4 book ai didi

android - 在 gmail-api 中获取发件人邮件

转载 作者:行者123 更新时间:2023-11-29 15:09:38 24 4
gpt4 key购买 nike

我正在尝试通过 gmail-api 开发人员指南弄明白。gmail-api 中的消息部分不包含有关发件人的任何详细信息,感谢您的帮助。

最佳答案

Ok I finally did it.Get message payload and then get headers from payload loop through the headers with name "from"

format of header is of the form header=[name:"somename" value:"somevalue"]

here is my code hope it helps someone

private List<MessageReader> getDataFromApi() throws IOException {
String user = "me";
List<MessageReader> labels = new ArrayList<MessageReader>();
ListMessagesResponse listResponse = mActivity.mService.users().messages().list(user).setQ(query).execute();
//int i=0;
for (Message label : listResponse.getMessages()) {
Message m = mActivity.mService.users().messages().get(user, label.getId()).execute();
String a ="";
try{
List<MessagePart> parts =m.getPayload().getParts();
List<MessagePartHeader> headers = m.getPayload().getHeaders();
MessageReader mreader = readParts(parts);
mreader.setDate(m.getInternalDate());
for(MessagePartHeader header:headers){
String name = header.getName();
if(name.equals("From")||name.equals("from")){
mreader.setSender(header.getValue());
break;
}
}
labels.add(mreader);
}catch(Exception e){
a+="The following error occurred:\n" +
e.getMessage();
}

}
return labels;//extractData(labels);
}


private MessageReader readParts(List<MessagePart> parts){
MessageReader mreader = new MessageReader();
int cnt =0;
for(MessagePart part:parts){

try{

String mime = part.getMimeType();
if(mime.contentEquals("text/plain")){
String s = new String(Base64.decodeBase64(part.getBody().getData().getBytes()));
mreader.setText(s);
}else if(mime.contentEquals("text/html")){
String s = new String(Base64.decodeBase64(part.getBody().getData().getBytes()));
mreader.setHtml(s);

}else if(mime.contentEquals("multipart/alternative")){
List<MessagePart> subparts =part.getParts();
MessageReader subreader = readParts(subparts);
mreader.setText(subreader.getText());
mreader.setHtml(subreader.getHtml());
}else if(mime.contentEquals("application/octet-stream")){
cnt++;
mreader.setNo_of_files(cnt);
}

}catch(Exception e){
// get file here

}

}
return mreader;
}





public class MessageReader {

private String text;
private String html;
int no_of_files;
private String sender;
private long date;
// file data to be made


public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public long getDate() {
return date;
}

public void setDate(long date) {
this.date = date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
public int getNo_of_files() {
return no_of_files;
}
public void setNo_of_files(int no_of_files) {
this.no_of_files = no_of_files;
}



}

关于android - 在 gmail-api 中获取发件人邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com