How to save oullook email as text format

7 次查看(过去 30 天)
As below, I can save oullook email in msg format, but I don't know how to save it in text formati.
Please advise me.
objol = actxserver('outlook.Application');
objNameSpace = objol.GetNamespace('MAPI');
objInbox = objNameSpace.GetDefaultFolder('olFolderInbox');
objMails = objInbox.Items;
t_filePath = fullfile(pwd, 'test');
objMails.Item(1).SaveAs([t_filePath, '.msg'])

采纳的回答

Mario Malic
Mario Malic 2023-9-21
编辑:Mario Malic 2023-9-21
Hi,
objol = actxserver('outlook.Application');
objNameSpace = objol.GetNamespace('MAPI');
objInbox = objNameSpace.GetDefaultFolder('olFolderInbox');
objMails = objInbox.Items;
objMail = objMails.GetFirst; % Use other methods to traverse through items
mailBody = objMail.Body;
t_filePath = fullfile(pwd, 'test.txt');
fid = fopen(t_filePath, "w");
if fid ~= -1
fprintf(fid, "%s", mailBody);
fclose(fid);
end
  3 个评论
Mario Malic
Mario Malic 2023-9-21
What you should look for is the Component Object Model (COM) (or API?) for Outlook and read what are the properties and methods available for particular class.
For example, class for mail is MailItem, here is a link https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem the properties that you are looking for are there. So you should construct string array or char array like in my answer above. For example:
if fid ~= -1
fprintf(fid, "%s\n", objMail.SenderEmailAddress)
fprintf(fid, "%s\n", objMail.Subject)
fprintf(fid, "%s\n", objMail.ReceivedTime)
fprintf(fid, "%s\n", objMail.Body)
fclose(fid);
end
I haven't tested it, it might fail, but this is the general idea. You can optimize it by constructing a complete data first and then just use fprintf once to write the data to file as this way might be slow if you have to process a lot of data.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by