Saving msg file attachment using EWS API - vb.net

Hope someone can help. I am implementing a feature where you can choose an email attachment and save it within a database. The feature works fine with PDF etc but when it comes to MSG files it creates a ItemAttachment not a Fileattachment and does not give me the ability to get the content or content type.
I have found this post about saving messages as .eml but ideally as the email had an .msg attached this is what should be saved in the system. I have done some extensive searching but have come to a dead end.
I am using asp.net so answers in VB.net would be appreciated.
Thanks

EDIT: Sorry, I did not see you were using VB, for now I won't delete my post since it can still give you an idea of how to handle this, I know that when I struggled with this, my thought of process was just incorrect and the actual syntax was not a challenge.
I am fairly new to this website so excuse me if I am not formatting my answer correctly.
Assuming you already established your exchange connection, created a new ItemView, and retrieved all emails from your inbox, we will start off by creating a secondary list of emails, but we are only going to list the emails that contain an Item Attachment.
List<EmailMessage> emailsWithItemAttachment =
emails.Where(e => e.HasAttachments && e.Attachments[0] is ItemAttachment).ToList();
Now, we can loop only the emails with Item attachment(s)
foreach (EmailMessage emailMessage in emailsWithItemAttachment)
{
//Loads all emails with Item attachments as an item attachment
foreach (Attachment attachment in emailMessage.Attachments)
{
attachment.Load();
ItemAttachment itemAttachment = attachment as ItemAttachment;
if (itemAttachment == null) continue;
ItemAttachment itemattachment = attachment as ItemAttachment;
itemattachment.Load(new PropertySet(ItemSchema.Attachments));
//Loads the scanned Attachment as an Item Attachment
foreach (Attachment scannedAttachment in itemattachment.Item.Attachments)
{
scannedAttachment.Load();
//Loads all Item Attachments as File Attachments
FileAttachment fileAttachment = scannedAttachment as FileAttachment;
if (fileAttachment != null)
{
//All Done! Your attachment will be "fileAttachment", from here you can do whatever you want
}
}
}
}
I really hope this helps you, and again if anything is wrong about my answer please do not hesitate to edit and/or contact me!

When querying an ItemAttachment, add the MimeContentPropertyDefinition (sorry, don't remember the exact names and classes). Then your ItemAttachment will have that MimeContent property set - that's a text (MIME) that you can save to a file in UTF-8 encoding with .EML extension. MIME is a standard, so that .EML file can be opened by any mail client app.

Related

How can I configure EAGetMail DLL options to make date search work?

Hi everyone and thanks in advance for taking the time reading my question.
I have this scenario: I'm using EAGetMail DLL to retrieve emails from a POP3 e-mail server. I have used an example given on the website and so far so good; I can get all the emails from my inbox.
My problem arises when I try to use the data filters that the DLL provides, in order to not get all the emails in the inbox, just a few, using a certain data range.
The code that I'm using for this purpose is the following:
Dim oClient As New MailClient("TryIt")
oClient.Connect(oServer)
Dim options As GetMailInfosOptionType
options = options Or GetMailInfosOptionType.DateRange
options = options Or GetMailInfosOptionType.OrderByDateTime
oClient.GetMailInfosParam.Reset()
oClient.GetMailInfosParam.GetMailInfosOptions = options
oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now.AddDays(-4)
oClient.GetMailInfosParam.DateRange.BEFORE = System.DateTime.Now.AddDays(1)
Theoretically this should retrieve only the emails within last four days from today, but it is not working. Everytime I execute the code I get every e-mail from my inbox.
Has someone faced something similar to this?
The first sentence on this page, regarding the MailClient.GetMailInfosParam property, says that POP3 is not supported:
Search email on IMAP4 Server and MS Exchange Server. It doesn't
support POP3 protocol.

Discrepancy between attachment contentType and attachment names on different clients for item attachments

When using the officejs api (v1.8), we're observing differences between item attachment metadata when using different clients when looking at the same email.
Outlook for Mac (v16.31 19111002)
- AttachmentDetails.name properties include the file suffix. E.g. (fileName).(eml, ics, vcf)
- AttachmentDetails.contentType returns correctly (message/rfc822, text/calendar, text/directory)
- AttachmentContent.format incorrectly returns .eml for all item attachment types
OWA (Chrome)
- AttachmentDetails.name properties does not include the file suffix
- AttachmentDetails.contentType incorrectly returns undefined
- AttachmentContent.format returns .(eml,iCalendar) as expected for all item attachment types
- This ONLY happens when viewing emails with item attachments sent from Outlook Desktop on Windows. Example build (1911 12228.20332)
What is the expected behavior when reading attachment metadata on different clients?
In addition as per this prior stackOverflow post, are there any plans to support getAttachmentContentAsync with contact item attachments?

Unable to read the Content of Email Automation Anywhere

I am reading the incoming mails from a outlook mailbox using automation anywhere Email Automation - Get All Messages menu. My simple code is given below. But everytime I am getting html objects and tags printing in the message box, I want only the email message.
Start Loop "Each message on server:outlook.office.365.com,User Name:xyz#xyz.com,SSL Server Type:POP3,Message Format:Plain Text"
Message Box:"$Email Message$"
Can anyone help????
In your original Email Automation - Get All Messages command, there should be a "Message Format" option below port number.
If not - you could offload to a javascript script that might look like this and call it via Run Script:
function noTags(vString) {
return vString.replace(/<(.|\n)*?>/g, '');
}
Passing vString into it and getting vString back out, now without the tags.

Attachment.Name when retrieving email attachments is null

I'm using openpop to retrieve and process emails, In processing the mails, I'm checking for attachments and saving them to a specific folder. This works fine for csv files but for some reason for pdfs "att.Name" is returning null and won't save.
AttachmentCollection attachments = mailItem.Attachments;
foreach (Attachment att in attachments)
{
using (var fileStream = new FileStream(conf.AttachmentSaveTo + att.Name, FileMode.Create, FileAccess.Write))
{
att.ContentStream.CopyTo(fileStream);
}
}
Any help, much appreciated.
Without having access to the raw message, it's possible that the MIME headers for your pdf attachments do not specify a file name.
If you look at the raw message source, check to see if your pdf attachments have a Content-Disposition header and make sure that they have a filename parameter.
If not, does the Content-Type header have a name parameter?
If at least 1 of those 2 parameters is present, perhaps you have discovered a bug in OpenPOP. If that's the case, I would suggest using my MailKit library instead.

YiiMail sending attachment

In my Project i am using YiiMail extension to send mail to the users. in which i am attaching a file. but the problem is its not possible to send the mail using the attachment. my mail code is given below.
$this->email->setBody('<p>'.$email.'-'.$name.'-'.$details.'</p>', 'text/html');
$this->email->from = "test#test.com";
$this->email->setSubject('Direct Request');
$this->email->attach(CUploadedFile::getInstanceByName('fileupload'));
$this->email->setTo(array($emailId => 'test#test.com'));
with this code the mail is not sending and error message is showing.
Argument 1 passed to Swift_Mime_SimpleMessage::attach() must implement interface Swift_Mime_MimeEntity, instance of CUploadedFile given
what is reason this error is showing and any solution for this.
thanks in advance
You need to convert your file attachment to a SwiftMailer Swift_Mime_MimeEntity type. CUploadedFile::getInstanceByName('fileupload') returns a CUploadedFile class, which SwiftMailer does not know how to handle. More on Swift attachments here.
I have not tested this, but you will need to do something like this:
$uploadedFile = CUploadedFile::getInstanceByName('fileupload'); // get the CUploadedFile
$uploadedFileName = $uploadedFile->tempName; // will be something like 'myfile.jpg'
$swiftAttachment = Swift_Attachment::fromPath($uploadedFileName); // create a Swift Attachment
$this->email->attach($swiftAttachment); // now attach the correct type
Good luck!