Before you begin
The examples shown below are based on two key assumptions:
You have already created a basic automated test using our getting started guides .
You have a chosen assertion library that you will use to test the values shown below.
How many attachments
The attachments property of a message contains an array of attachments. The length of this array corresponds to the number of files attached to the email.
Node.js Python Java .NET Ruby PHP Go
console .log (message.attachments .length ) print (len (message.attachments)) System.out.println(message.attachments().size()); Console.WriteLine(message.Attachments.Count); puts(message.attachments.length) print (count ($message ->attachments)); fmt.Println(len (message.Attachments)) Testing the file name and content type
Each attachment contains metadata on the file name and content type.
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
console .log (firstAttachment.fileName )
console .log (firstAttachment.contentType ) first_attachment = message.attachments[0 ]
print (first_attachment.file_name)
print (first_attachment.content_type) Attachment firstAttachment = message.attachments().get(0 );
System.out.println(firstAttachment.fileName());
System.out.println(firstAttachment.contentType()); var firstAttachment = message.Attachments[0 ];
Console.WriteLine(firstAttachment.FileName);
Console.WriteLine(firstAttachment.ContentType); first_attachment = message.attachments[0 ]
puts(first_attachment.file_name)
puts(first_attachment.content_type) $firstAttachment = $message ->attachments[0 ];
print ($firstAttachment ->fileName);
print ($firstAttachment ->contentType); var firstAttachment = message.Attachments[0 ]
fmt.Println(firstAttachment.FileName)
fmt.Println(firstAttachment.ContentType) Testing attachment file size
The length property returns the size of the attached file (in bytes).
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
console .log (firstAttachment.length ) first_attachment = message.attachments[0 ]
print (first_attachment.length) Attachment firstAttachment = message.attachments().get(0 );
System.out.println(firstAttachment.length()); var firstAttachment = message.Attachments[0 ];
Console.WriteLine(firstAttachment.Length); first_attachment = message.attachments[0 ]
puts(first_attachment.length) $firstAttachment = $message ->attachments[0 ];
print ($firstAttachment ->length); var firstAttachment = message.Attachments[0 ]
fmt.Println(firstAttachment.Length) Embedded Images
If an email contains embedded images then the contentId attribute will be populated and will have a corresponding entry in the images array .
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
console .log (firstAttachment.fileName )
console .log (firstAttachment.contentType ) first_attachment = message.attachments[0 ]
print (first_attachment.file_name)
print (first_attachment.content_type) Attachment firstAttachment = message.attachments().get(0 );
System.out.println(firstAttachment.fileName());
System.out.println(firstAttachment.contentType()); var firstAttachment = message.Attachments[0 ];
Console.WriteLine(firstAttachment.FileName);
Console.WriteLine(firstAttachment.ContentType); first_attachment = message.attachments[0 ]
puts(first_attachment.file_name)
puts(first_attachment.content_type) $firstAttachment = $message ->attachments[0 ];
print ($firstAttachment ->fileName);
print ($firstAttachment ->contentType); var firstAttachment = message.Attachments[0 ]
fmt.Println(firstAttachment.FileName)
fmt.Println(firstAttachment.ContentType) Testing content ID
The contentId property identifies embedded attachments within an email.
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
console .log (firstAttachment.contentId ) first_attachment = message.attachments[0 ]
print (first_attachment.content_id) Attachment firstAttachment = message.attachments().get(0 );
System.out.println(firstAttachment.contentId()); var firstAttachment = message.Attachments[0 ];
Console.WriteLine(firstAttachment.ContentId); first_attachment = message.attachments[0 ]
puts(first_attachment.content_id) $firstAttachment = $message ->attachments[0 ];
print ($first_attachment ->contentId); var firstAttachment = message.Attachments[0 ]
fmt.Println(firstAttachment.ContentId) Fetching an attachment as base64
You can fetch the content of an attachment, encoded as base64, using the “get attachment” method:
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
const file = await mailosaur.files .getAttachment (firstAttachment.id )
const base64 = file.toString ('base64' )
console .log (base64)first_attachment = message.attachments[0 ]
file = mailosaur.files.get_attachment(first_attachment.id ).content
base64 = base64.b64encode(file)
print (base64)Attachment firstAttachment = message.attachments().get(0 );
byte [] file = mailosaur.files().getAttachment(firstAttachment.id());
String base64 = new String (Base64.getEncoder().encode(file));
System.out.println(base64);var firstAttachment = message.Attachments[0 ];
var file = mailosaur.Files.GetAttachment(firstAttachment.Id));
var base64 = Convert.ToBase64String(file );
Console.WriteLine(base64);first_attachment = message.attachments[0 ]
file = mailosaur.files.get_attachment(first_attachment.id)
base64 = Base64 .encode64(file)
puts base64$firstAttachment = $message ->attachments[0 ];
$file = $mailosaur ->files->getAttachment ($firstAttachment ->id);
$base64 = base64_encode ($file );
print ($base64 );var firstAttachment = message.Attachments[0 ]
var file, _ = m.Files.GetAttachment(firstAttachment.Id)
base64 := b64.StdEncoding.EncodeToString(file)
fmt.Println(base64)Saving an attachment to disk
You can also write an attached file to disk:
Node.js Python Java .NET Ruby PHP Go
const firstAttachment = message.attachments [0 ]
const file = await mailosaur.files .getAttachment (firstAttachment.id )
fs.writeFileSync (firstAttachment.fileName , file)first_attachment = message.attachments[0 ]
file = mailosaur.files.get_attachment(first_attachment.id ).content
f = open (first_attachment.file_name, 'wb' )
f.write(file)
f.close()Attachment firstAttachment = message.attachments().get(0 );
byte [] file = mailosaur.files().getAttachment(firstAttachment.id());
Files.write(Paths.get(firstAttachment.fileName()), file);var firstAttachment = message.Attachments[0 ];
var file = mailosaur.Files.GetAttachment(firstAttachment.Id));
File.WriteAllBytes(firstAttachment.FileName, file );first_attachment = message.attachments[0 ]
file = mailosaur.files.get_attachment(first_attachment.id)
File .open(first_attachment.file_name, 'wb' ) { |fp | fp.write(file) }$firstAttachment = $message ->attachments[0 ];
$file = $mailosaur ->files->getAttachment ($firstAttachment ->id);
$f = fopen ($firstAttachment ->fileName, "w" ) or die ("Unable to open file!" );
fwrite ($f , $file );
fclose ($f );var firstAttachment = message.Attachments[0 ]
var file, _ = m.Files.GetAttachment(firstAttachment.Id)
var err = os.WriteFile(firstAttachment.FileName, file, 0644 )
if err != nil {
}See also