Attachment.Name when retrieving email attachments is null - pdf

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.

Related

Sending files using pure HTTP request using telegram bot

hello everyone I was tring to send files using my bot like http://api.telegram.org/botTOKEN/sendDocument?document=http://my_path&chat_id but it ain't support .txt .docx..... and other formats..... any help please
According to https://core.telegram.org/bots/api#sending-files
Sending by URL In sendDocument, sending by URL will currently only
work for gif, pdf and zip files.
You may try to use this approach
Post the file using multipart/form-data in the usual way that files
are uploaded via the browser. 10 MB max size for photos, 50 MB for
other files.
This answer might give you some ideas on how to do that.
UPDATE
It is also good idea to use someone's library to understand how it works there.
For example, I use longman/telegram-bot from this repo. There is encodeFile method in Request class.
Method is follows:
public static function encodeFile($file)
{
$fp = fopen($file, 'rb');
if ($fp === false) {
throw new TelegramException('Cannot open "' . $file . '" for reading');
}
return $fp;
}
Which means simple fopen method with 'rb' parameter is enough to convert file.

How to POST a XML file in jmeter body instead of a physical file

I'm using JMeter 3.2.
My requirement is to read an XML file from the disk, replace some tags with dynamic values to ensure each thread sends a unique xml file upload (NOT SOAP Request). The following code in JSR223 sampler works perfectly fine when I try to upload the newfile through POST using a http sampler with ${newfilename} file text/xml.
import org.apache.commons.io.FileUtils;
try {
String content = FileUtils.readFileToString(new File("E:/test.xml"));
content = content.replaceAll("SUB_ID", "${__UUID}");
content = content.replaceAll("ABN_ID", "${empabn}");
content = content.replaceAll("EMPNAME", "${empname}");
vars.put("content", content);
FileUtils.writeStringToFile(new File("E:/testnew${empname}.xml"), content);
}
catch (Throwable ex) {
log.info("What happened?", ex);
throw ex;
}
Instead of writing again to the disk and uploading again, how can I send the contents of string 'content' as part of request body? I have looked at many posts that talk about the input output streams but they are confusing. When I try to send just ${content} in body, the application throws following error:
HTTP Status 500 - Could not write JSON: Name is null (through reference chain: com.xxx.xxx.datafile.rest.DataFileResponse["validationStatus"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Name is null (through reference chain:
Appreciate your help.
Multipart POST requests which are being used for files upload are different from normal POST requests hence there no possibility to simply substitute the file with the generated in-memory string.
You need to replicate the request exactly as it would be send by JMeter or real browser and manually populate each part starting from defining boundary using the HTTP Header Manager and ending up with the creation of Content-Disposition and specify your file contents there.
A little hint: you don't need to generate/substitute values for each call, it is enough to replace them once and JMeter will substitute them on its own given you use __eval() and __FileToString() functions combination.
You can check out Testing REST API File Uploads in JMeter for an example of creation a relatively complex file upload request, in your case it will be easier but still tricky.

Saving msg file attachment using EWS API

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.

loading response data into web view Titanium

I got response data from the web services, which is base64binary data.
I want to load this base64binary data into web view for titanium alloy [version 3.1.0.2].
The data base64binary is of pdf file.
Ti.API.info('Status is ::',xhrDocument.status);
var ResponseData = xhrDocument.getResponseXML().getElementsByTagName('GetDocResult').item(0).text;
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'pdfbinarray.pdf');
if(xhrDocument.status == 200){
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'filename2.pdf'); file.write(xhrDocument.getResponseXML().getElementsByTagName('GetDocResult').item(0).text);
Titanium.API.info('file write');
Titanium.API.info(file.size);
}
The above code created filename2.pdf in my Documents directory. When I open the file using Adobe Reader, it says Adobe Reader could not open filename2.pdf because it is either not a valid file or has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
Is the web service call returning ONLY the document, or is there additional data included in the response?
We have had success using a simpler method. If the service is simply returning the document, try changing line two to something more like this:
var ResponseData = xhrDocument.responseText;

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!