Working with Messages from Server

รับข้อมูลกล่องจดหมาย

เราสามารถรับข้อมูลของกล่องจดหมาย เช่น จำนวนข้อความและขนาดกล่องจดหมายโดยใช้ getMailBoxSize และ getMailBoxInfo เมธอดของ Pop3Client คลาส.

  • นี้ getMailBoxSize เมธอดคืนขนาดของกล่องจดหมายเป็นไบต์
  • นี้ getMailBoxInfo เมธอดจะคืนค่าอ็อบเจกต์ประเภท Pop3MailBoxInfo.

ยังสามารถรับจำนวนข้อความโดยใช้ MessageCount คุณสมบัติและขนาดโดยใช้ OccupiedSize คุณสมบัติของ Pop3MailBoxInfo คลาส ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับข้อมูลเกี่ยวกับกล่องจดหมาย มันแสดงวิธีการ:

  1. สร้าง Pop3Client.
  2. เชื่อมต่อกับเซิร์ฟเวอร์ POP3
  3. รับขนาดของกล่องจดหมาย
  4. รับข้อมูลกล่องจดหมาย
  5. รับจำนวนข้อความในกล่องจดหมาย
  6. รับขนาดที่ใช้
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java

Pop3Client client = new Pop3Client();
// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);

// Get the size of the mailbox, Get mailbox info, number of messages in the mailbox
long nSize = client.getMailboxSize();
System.out.println("Mailbox size is " + nSize + " bytes.");
Pop3MailboxInfo info = client.getMailboxInfo();
int nMessageCount = info.getMessageCount();
System.out.println("Number of messages in mailbox are " + nMessageCount);
long nOccupiedSize = info.getOccupiedSize();
System.out.println("Occupied size is " + nOccupiedSize);

การนับจำนวนอีเมลในกล่องจดหมาย

โค้ดสแนปต่อไปนี้แสดงวิธีนับจำนวนข้อความอีเมลในกล่องจดหมาย

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
Pop3Client client = new Pop3Client("pop3.server.com", "username", "password");
try {
    client.setSecurityOptions(SecurityOptions.Auto);
    int i = client.getMessageCount();
    System.out.println("Message count: " + i);
} catch (Pop3Exception ex) {
    System.out.println("Error:" + ex.toString());
}

Aspose.Email ให้ผู้พัฒนาสามารถทำงานกับอีเมลได้หลายวิธี ตัวอย่างเช่น พวกเขาสามารถดึงข้อมูลส่วนหัวก่อนตัดสินใจดาวน์โหลดอีเมล หรือสามารถดึงอีเมลจากเซิร์ฟเวอร์และบันทึกโดยไม่ต้องแปลง (เร็วกว่า) หรือหลังจากแปลงแล้ว (ช้ากว่า) บทความนี้แสดงวิธีดึงและแปลงอีเมล

ดึงข้อมูลส่วนหัวอีเมล

ส่วนหัวอีเมลสามารถให้ข้อมูลเกี่ยวกับข้อความอีเมลที่เราสามารถใช้ตัดสินใจว่าจะดึงข้อความเต็มหรือไม่ โดยปกติส่วนหัวจะมีผู้ส่ง, หัวเรื่อง, วันที่รับ, เป็นต้น (ส่วนหัวอีเมลได้อธิบายโดยละเอียดใน ปรับแต่งส่วนหัวอีเมล. หัวข้อนี้เป็นการพูดถึงการส่งอีเมลด้วย SMTP โดยเฉพาะ แต่ข้อมูลส่วนหัวของอีเมลยังคงใช้ได้กับอีเมล POP3) ตัวอย่างต่อไปนี้แสดงวิธีดึงส่วนหัวอีเมลจากเซิร์ฟเวอร์ POP3 ด้วยลำดับหมายเลขข้อความ.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();

// Specify host, username. password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
    HeaderCollection headers = client.getMessageHeaders(1);
    for (int i = 0; i < headers.size(); i++) {
        // Display key and value in the header collection
        System.out.print(headers.getKey(i));
        System.out.print(" : ");
        System.out.println(headers.get(i));
    }
} catch (Exception ex) {
    System.out.println(ex.getMessage());
} finally {
    client.dispose();
}

ดึงข้อความอีเมล

นี้ Pop3Client คอมโพเนนต์คลาสให้ความสามารถในการดึงข้อความอีเมลจากเซิร์ฟเวอร์ POP3 และแปลงเป็น MailMessage อินสแตนซ์โดยใช้ MailMessage components. The MailMessage class contains several properties and methods for manipulating email content. By using fetchMessage เมธอดของ Pop3Client class, you can get a MailMessage instance directlyจากเซิร์ฟเวอร์ POP3 ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงข้อความอีเมลเต็มจากเซิร์ฟเวอร์ POP3

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();

// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);
try {
    int messageCount = client.getMessageCount();
    // Create an instance of the MailMessage class and Retrieve message
    MailMessage message;
    for (int i = 1; i <= messageCount; i++) {
        message = client.fetchMessage(i);
        System.out.println("From:" + message.getFrom());
        System.out.println("Subject:" + message.getSubject());
        System.out.println(message.getHtmlBody());
    }
} catch (Exception ex) {
    System.out.println(ex.getMessage());
} finally {
    client.dispose();
}

Retrieving Message Summary Information using Unique Id

POP3 Client ของ API สามารถดึงข้อมูลสรุปของข้อความจากเซิร์ฟเวอร์โดยใช้รหัส ID ที่เป็นเอกลักษณ์ของข้อความ ซึ่งทำให้เข้าถึงข้อมูลสั้นของข้อความได้อย่างรวดเร็วโดยไม่ต้องดึงข้อความเต็มจากเซิร์ฟเวอร์ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงข้อมูลสรุปของข้อความ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String uniqueId = "unique id of a message from server";
Pop3Client client = new Pop3Client("host.domain.com", 456, "username", "password");
client.setSecurityOptions(SecurityOptions.Auto);
Pop3MessageInfo messageInfo = client.getMessageInfo(uniqueId);

if (messageInfo != null) {
    System.out.println(messageInfo.getSubject());
    System.out.println(messageInfo.getDate());
}

Listing Messages with MultiConnection

Pop3Client ให้การ UseMultiConnection คุณสมบัติที่สามารถใช้สร้างการเชื่อมต่อหลาย ๆ ครั้งสำหรับงานที่มีภาระหนัก. คุณยังสามารถตั้งค่าจำนวนการเชื่อมต่อที่จะใช้ในโหมดหลายการเชื่อมต่อโดยใช้ Pop3Client.ConnectionsQuantity. ตัวอย่างโค้ดต่อไปนี้แสดงการใช้โหมดหลายการเชื่อมต่อสำหรับการลิสต์ข้อความและเปรียบเทียบประสิทธิภาพกับโหมดการเชื่อมต่อเดี่ยว

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client pop3Client = new Pop3Client();
pop3Client.setHost("<HOST>");
pop3Client.setPort(995);
pop3Client.setUsername("<USERNAME>");
pop3Client.setPassword("<PASSWORD>");

pop3Client.setConnectionsQuantity(5);
pop3Client.setUseMultiConnection(MultiConnectionMode.Enable);
long multiConnectionModeStartTime = System.currentTimeMillis();
Pop3MessageInfoCollection messageInfoCol1 = pop3Client.listMessages();
long multiConnectionModeTimeSpan = System.currentTimeMillis() - multiConnectionModeStartTime;

pop3Client.setUseMultiConnection(MultiConnectionMode.Disable);
long singleConnectionModeStartTime = System.currentTimeMillis();
Pop3MessageInfoCollection messageInfoCol2 = pop3Client.listMessages();
long singleConnectionModeTimeSpan = System.currentTimeMillis() - singleConnectionModeStartTime;

System.out.println("multyConnectionModeTimeSpan: " + multiConnectionModeTimeSpan);
System.out.println("singleConnectionModeTimeSpan: " + singleConnectionModeTimeSpan);
double performanceRelation = singleConnectionModeTimeSpan / multiConnectionModeTimeSpan;
System.out.println("Performance Relation: " + performanceRelation);

ดึงข้อความจากเซิร์ฟเวอร์และบันทึกลงดิสก์

บันทึกข้อความลงดิสก์โดยไม่แปลง

หากต้องการดาวน์โหลดข้อความอีเมลจากเซิร์ฟเวอร์ POP3 โดยไม่ต้องแปลง ให้ใช้ Pop3Client คลาส saveMessage ฟังก์ชัน. The saveMessage ฟังก์ชันไม่แปลงข้อความอีเมลจึงเร็วกว่า fetchMessage ฟังก์ชัน. โค้ดสแนปต่อไปนี้แสดงวิธีบันทึกข้อความตามลำดับหมายเลข ในกรณีนี้คือหมายเลข 1. The saveMessage เมธอดบันทึกข้อความในรูปแบบ EML ดั้งเดิมโดยไม่แปลง

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "pop3/";
String dstEmail = dataDir + "InsertHeaders.eml";

// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();

// Specify host, username, password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);

try {
    // Save message to disk by message sequence number
    client.saveMessage(1, dstEmail);
    client.dispose();
} catch (Exception ex) {
    System.out.println(ex.getMessage());
}
System.out.println("Retrieved email messages using POP3 ");

แปลงข้อความก่อนบันทึก

โค้ดสแนปต่อไปนี้ใช้ Pop3Client fetchMessage เมธอดเพื่อดึงข้อความจากเซิร์ฟเวอร์ POP3 ด้วยลำดับหมายเลข แล้วบันทึกข้อความลงดิสก์โดยใช้หัวเรื่องเป็นชื่อไฟล์.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "pop3/";

// Create an instance of the Pop3Client class
Pop3Client client = new Pop3Client();

// Specify host, username and password, Port and SecurityOptions for your client
client.setHost("pop.gmail.com");
client.setUsername("your.username@gmail.com");
client.setPassword("your.password");
client.setPort(995);
client.setSecurityOptions(SecurityOptions.Auto);

try {
    // Fetch the message by its sequence number and Save the message using its subject as the file name
    MailMessage msg = client.fetchMessage(1);
    msg.save(dataDir + "first-message_out.eml", SaveOptions.getDefaultEml());
    client.dispose();
} catch (Exception ex) {
    System.out.println(ex.getMessage());
} finally {
    client.dispose();
}

การดึงข้อความแบบกลุ่ม

Pop3Client ให้การ fetchMessages เมธอดที่รับ iterable ของลำดับหมายเลขหรือ Unique ID และคืนค่าเป็นรายการของ MailMessage. โค้ดสแนปต่อไปนี้แสดงการใช้ fetchMessages เมธอดเพื่อดึงข้อความตามลำดับหมายเลขและ ID ที่ไม่ซ้ำกัน.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of the Pop3Client class
Pop3Client pop3Client = new Pop3Client();
pop3Client.setHost("<HOST>");
pop3Client.setPort(995);
pop3Client.setUsername("<USERNAME>");
pop3Client.setPassword("<PASSWORD>");

Pop3MessageInfoCollection messageInfoCol = pop3Client.listMessages();
System.out.println("ListMessages Count: " + messageInfoCol.size());

List<Integer> sequenceNumberAr = new ArrayList<Integer>();
List<String> uniqueIdAr = new ArrayList<String>();
for (Pop3MessageInfo mi : messageInfoCol) {
    sequenceNumberAr.add(mi.getSequenceNumber());
    uniqueIdAr.add(mi.getUniqueId());
}

for (MailMessage m : pop3Client.fetchMessagesBySequences(sequenceNumberAr)) {
    System.out.println("FetchMessages-sequenceNumberAr : " + m.getSubject());
}

for (MailMessage m : pop3Client.fetchMessagesByUids(uniqueIdAr)) {
    System.out.println("FetchMessages-uniqueIdAr : " + m.getSubject());
}

การกรองข้อความตามผู้ส่ง, ผู้รับ หรือวันที่

นี้ Pop3Client คลาส, อธิบายใน Connecting to a POP3 Server, ให้ listMessages เมธอดที่ดึงข้อความทั้งหมดจากกล่องจดหมาย เพื่อดึงเฉพาะข้อความที่ตรงกับเงื่อนไขบางอย่าง ให้ใช้เมธอดโอเวอร์โหลด listMessages เมธอดที่รับ MailQuery เป็นอาร์กิวเมนต์. MailQuery คลาสให้คุณสมบัติต่าง ๆ สำหรับระบุตำแหน่งเงื่อนไขการค้นหา เช่น วันที่, หัวเรื่อง, ผู้ส่ง, ผู้รับ เป็นต้น MailQueryBuilder คลาสใช้สร้างนิพจน์การค้นหา ก่อนอื่นตั้งค่าเงื่อนไขและข้อจำกัดทั้งหมดแล้วจากนั้น MailQuery ถูกเติมด้วยคิวรีที่พัฒนาโดย MailQueryBuilder. ส่วน MailQuery อ็อบเจกต์คลาสใช้โดย Pop3Client เพื่อดึงข้อมูลที่กรองจากเซิร์ฟเวอร์ บทความนี้แสดงวิธีกรองข้อความอีเมลจากกล่องจดหมาย ตัวอย่างแรกแสดงวิธีกรองข้อความตามวันที่และหัวเรื่อง เราแสดงวิธีกรองตามเกณฑ์อื่น ๆ และวิธีสร้างคิวรีที่ซับซ้อนขึ้น นอกจากนี้ยังแสดงการใช้ตัวกรองวันที่และเวลาเพื่อดึงอีเมลเฉพาะจากกล่องจดหมาย อีกทั้งยังแสดงวิธีใช้การกรองที่คำนึงถึงตัวพิมพ์ใหญ่-เล็ก

การกรองข้อความจากกล่องจดหมาย

เพื่อกรองข้อความจากกล่องจดหมาย:

  1. เชื่อมต่อและล็อกอินเข้าสู่เซิร์ฟเวอร์ POP3.
  2. สร้างอินสแตนซ์ของ MailQuery และตั้งค่าคุณสมบัติตามต้องการ
  3. เรียกใช้ Pop3Client.listMessages(MailQuery query) เมธอดและส่งค่า MailQuery ในพารามิเตอร์เพื่อดึงเฉพาะข้อความที่กรองได้

โค้ดสแนปต่อไปนี้แสดงวิธีเชื่อมต่อกับกล่องจดหมาย POP3 และดึงข้อความที่มาถึงวันนี้และมีคำว่า "newsletter" ในหัวเรื่อง

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Connect and log in to POP3
String host = "host";
int port = 110;
String username = "user@host.com";
String password = "password";
Pop3Client client = new Pop3Client(host, port, username, password);

// Set conditions, Subject contains "Newsletter", Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.getSubject().contains("Newsletter");
builder.getInternalDate().on(new Date());
// Build the query and Get list of messages
MailQuery query = builder.getQuery();
Pop3MessageInfoCollection messages = client.listMessages(query);
System.out.println("Pop3: " + messages.size() + " message(s) found.");

ดึงข้อความที่ตรงตามเกณฑ์เฉพาะ

ตัวอย่างโค้ดด้านบน กรองข้อความตามหัวเรื่องอีเมลและวันที่ เราสามารถใช้คุณสมบัติเพิ่มเติมเพื่อกำหนดเงื่อนไขที่รองรับอื่น ๆ ด้านล่างเป็นตัวอย่างการตั้งค่าเงื่อนไขโดยใช้ MailQuery.

โค้ดสแนปต่อไปนี้แสดงวิธีกรองอีเมลตามเกณฑ์อื่น:

  • ค้นหาอีเมลที่ส่งวันนี้
  • ค้นหาอีเมลที่ได้รับภายในช่วง
  • ค้นหาอีเมลจากผู้ส่งเฉพาะ
  • ค้นหาอีเมลที่ส่งมาจากโดเมนเฉพาะ
  • ค้นหาอีเมลที่ส่งถึงผู้รับเฉพาะ

วันที่วันนี้

โค้ดสแนปต่อไปนี้แสดงวิธีค้นหาอีเมลที่ส่งวันนี้

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.getInternalDate().on(new Date());

ช่วงวันที่

โค้ดสแนปต่อไปนี้แสดงวิธีค้นหาอีเมลที่ได้รับภายในช่วงหนึ่ง

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails that arrived in last 7 days
Calendar calendar = Calendar.getInstance();

builder.getInternalDate().before(calendar.getTime());
calendar.add(Calendar.DATE, -7);
builder.getInternalDate().since(calendar.getTime());

ผู้ส่งเฉพาะ

โค้ดสแนปต่อไปนี้แสดงวิธีค้นหาอีเมลจากผู้ส่งเฉพาะ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails from specific sender
builder.getFrom().contains("saqib.razzaq@127.0.0.1");

โดเมนเฉพาะ

โค้ดสแนปต่อไปนี้แสดงวิธีค้นหาอีเมลที่ส่งมาจากโดเมนเฉพาะ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails from specific domain
builder.getFrom().contains("SpecificHost.com");

ผู้รับเฉพาะ

โค้ดสแนปต่อไปนี้แสดงวิธีค้นหาอีเมลที่ส่งถึงผู้รับเฉพาะ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Get emails sent to specific recipient
builder.getTo().contains("recipient");

การสร้างคิวรีซับซ้อน

หากแตกต่าง MailQueryBuilder คุณสมบัติตั้งค่าในคำสั่งแยกกัน จากนั้นเงื่อนไขทั้งหมดจะตรงกัน ตัวอย่างเช่น หากเราต้องการดึงข้อความระหว่างช่วงวันที่และจากโฮสต์เฉพาะ เราต้องเขียนสามคำสั่ง

การรวมคิวรีด้วย AND

โค้ดสแนปต่อไปนี้แสดงวิธีรวมคิวรีด้วย AND

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Emails from specific host, get all emails that arrived before today and all emails that arrived since 7 days ago
builder.getFrom().contains("SpecificHost.com");

Calendar calendar = Calendar.getInstance();

builder.getInternalDate().before(calendar.getTime());
calendar.add(Calendar.DATE, -7);
builder.getInternalDate().since(calendar.getTime());

การรวมคิวรีด้วย OR

MailQueryBuilder ให้ หรือ เมธอดที่รับสอง MailQuery อินสแตนซ์เป็นพารามิเตอร์ มันจะดึงข้อความที่ตรงกับเงื่อนไขใดเงื่อนไขหนึ่งของสองเงื่อนไขที่ระบุ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีกรองข้อความที่มีคำว่า "test" ในหัวเรื่องหรือ "noreply@host.com" เป็นผู้ส่ง ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรวมคิวรีด้วย OR

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Specify OR condition
builder.or(builder.getSubject().contains("test"), builder.getFrom().contains("noreply@host.com"));

การใช้ตัวกรองที่คำนึงถึงตัวพิมพ์ใหญ่-เล็ก

API ยังให้ความสามารถในการกรองอีเมลจากกล่องจดหมายตามเงื่อนไขที่คำนึงถึงตัวพิมพ์ใหญ่-เล็ก วิธีต่อไปนี้ให้ความสามารถในการค้นหาอีเมลโดยระบุแฟล็กคำนึงถึงตัวพิมพ์ใหญ่-เล็ก

  • Method StringComparisonField.contains(String value, boolean ignoreCase)
  • Method StringComparisonField.equals(String value, boolean ignoreCase)
  • Method StringComparisonField.notContains(String boolean, bool ignoreCase)
  • Method StringComparisonField.notEquals(String boolean, bool ignoreCase)
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// IgnoreCase is True
MailQueryBuilder builder1 = new MailQueryBuilder();
builder1.getFrom().contains("tesT", true);
MailQuery query1 = builder1.getQuery();
Pop3MessageInfoCollection messageInfoCol1 = client.listMessages(query1);