Dart
Dart
Get IMAP Attachment Sizes
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.
Background: Like the attachment filename, the size comes from
ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes of an
// attachment. The 1st argument is the Email and the 2nd is the zero-based attachment index.
final imap = CkImap();
imap.ssl = true;
imap.port = 993;
try {
imap.connect('imap.example.com');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imap.login('user@example.com', 'myPassword');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
try {
imap.selectMailbox('Inbox');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
// metadata describing the attachments is present, so the attachment info methods still work.
final headersOnly = true;
final useUid = false;
final seqNum = 1;
final email = CkEmail();
try {
imap.fetchEmail(headersOnly, seqNum, useUid, email);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final numAttach = imap.getMailNumAttach(email);
for (var i = 0; i < numAttach; i++) {
final String fname;
try {
fname = imap.getMailAttachFilename(email, i);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final sz = imap.getMailAttachSize(email, i);
print('$fname: $sz bytes');
}
try {
imap.disconnect();
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
}