Arbeiten mit Outlook‑Kontakten

Outlook-Kontakt erstellen

Aspose.Email für Java unterstützt das Erstellen von Outlook-Kontakten (VCards) mit dem MapiContact Klasse. MapiContact enthält viele Methoden, von denen einige unten aufgeführt sind.

Kontaktstruktur in Aspose.Email für Java

Unten ist die für Kontakte in Aspose.Email für Java implementierte Hierarchie dargestellt. Der entsprechende Klassenname ist neben jeder Eigenschaft angegeben. Hyperlinks zur Online-Dokumentation werden für weitere Referenz bereitgestellt.

  1. Kontakt (MapiContact)
    1. Elektronische Adressen (MapiContactElectronicAddressPropertySet)
      1. E‑Mail 1 (MapiContactElectronicAddress)
        1. Adressart
        2. Anzeigename
        3. E‑Mail‑Adresse
        4. Faxnummer
      2. E‑Mail 2
      3. E‑Mail 3
      4. Privates Fax
      5. Primäres Fax
      6. Geschäftstelefaks
    2. Ereignisse (MapiContactEventPropertySet) Siehe unten ein Beispiel, wie Ereignisse gesetzt werden.
      1. Geburtstag
      2. Hochzeitstag
    3. Namensinformationen (MapiContactNamePropertySet)
      1. Anzeigename
      2. Anzeige‑Namenspräfix
      3. Dateiunter
      4. Dateiunter‑ID
      5. Generation
      6. Vorname
      7. Initialen
      8. Zweiter Vorname
      9. Spitzname
      10. Nachname
    4. Persönliche Informationen (MapiContactPersonalInfoPropertySet)
      1. Konto
      2. Geschäfts‑Homepage
      3. Computer‑Netzwerkname
      4. Kunden‑ID
      5. Kostenloser Geschäftsstandort
      6. FTP‑Seite
      7. Geschlecht
      8. Staatliche ID‑Nummer
      9. Hobbys
      10. HTML
      11. Instant‑Messaging‑Adresse
      12. Sprache
      13. Ort
      14. Notizen
      15. Organisations‑ID‑Nummer
      16. Persönliche Homepage
      17. Name des Empfehlenden
      18. Name des Ehepartners
    5. Physische Adresse (MapiContactPhysicalAddressPropertySet)
      1. Privatadresse (MapiContactPhysicalAddress)
        1. Adresse
        2. Stadt
        3. Land
        4. Ländercode
        5. Postleitzahl
        6. Postfach
        7. Bundesland oder Provinz
      2. Weitere Adresse
      3. Arbeitsadresse
    6. Berufsinformationen
      1. Assistent
      2. Firmenname
      3. Abteilungsname
      4. Name des Managers
      5. Bürostandort
      6. Beruf
      7. Titel
    7. Telefonnummern (MapiContactTelephonePropertySet)
      1. Assistenten‑Telefonnummer
      2. Geschäftstelefonnummer 2
      3. Geschäftstelefonnummer
      4. Rückruf‑Telefonnummer
      5. Auto‑Telefonnummer
      6. Haupttelefonnummer des Unternehmens
      7. Telefonnummer (Privat 2)
      8. Telefonnummer (Privat)
      9. ISDN‑Nummer
      10. Mobile Telefonnummer
      11. Andere Telefonnummer
      12. Pager‑Telefonnummer
      13. Primäre Telefonnummer
      14. Funktelefon‑Nummer
      15. Telex‑Nummer
      16. TTY/TDD‑Telefonnummer

Der folgende Code verwendet Aspose.Email, um einen Outlook‑Kontakt zu erstellen und füllt ihn mit Namen, beruflichen Eigenschaften, physischer Adresse und E‑Mail. Er zeigt außerdem das Hinzufügen von MapiContactEventPropertySet zum Kontakt.

|todo:image_alt_text| | :- | |Abbildung: Ein mit Aspose.Email codierter Microsoft Outlook‑Kontakt|

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

MapiContact contact = new MapiContact();

// Set the name properties using MapiContactNamePropertySet
MapiContactNamePropertySet namePropSet = new MapiContactNamePropertySet();
namePropSet.setGivenName("Melissa");
namePropSet.setSurname("MacBeth");
contact.setNameInfo(namePropSet);

// Set the professional properties using MapiContactProfessionalPropertySet
MapiContactProfessionalPropertySet profPropSet = new MapiContactProfessionalPropertySet();
profPropSet.setTitle("Account Representative");
profPropSet.setCompanyName("Contoso Ltd.");
profPropSet.setOfficeLocation("36/2529");
contact.setProfessionalInfo(profPropSet);

// Telephones
MapiContactTelephonePropertySet telephone = new MapiContactTelephonePropertySet();
telephone.setAssistantTelephoneNumber("(831) 758-7214");
telephone.setBusiness2TelephoneNumber("(831) 759-2518");
telephone.setBusinessTelephoneNumber("(831) 758-7285");
telephone.setCallbackTelephoneNumber("(831) 758-7321 (After hours)");
telephone.setCarTelephoneNumber("(831) 758-7201");
telephone.setCompanyMainTelephoneNumber("(831) 758-7368");
telephone.setHome2TelephoneNumber("(831) 758-7256");
telephone.setHomeTelephoneNumber("(831) 758-7257");
telephone.setIsdnNumber("(831) 758-7381");
telephone.setMobileTelephoneNumber("(831) 758-7368");
telephone.setOtherTelephoneNumber("(831) 758-7201");
telephone.setPagerTelephoneNumber("(831) 758-7368");
telephone.setPrimaryTelephoneNumber("(831) 758-7334");
telephone.setRadioTelephoneNumber("(831) 758-7234");
telephone.setTelexNumber("(831) 758-7408");
telephone.setTtyTddPhoneNumber("(800) 806-4474");
contact.setTelephones(telephone);

// Set the physical address using MapiContactPhysicalAddress and MapiContactPhysicalAddressPropertySet
MapiContactPhysicalAddress physAddress = new MapiContactPhysicalAddress();
physAddress.setAddress("144 Hitchcock Rd");
physAddress.setCity("Salinas");
physAddress.setStateOrProvince("CA");
physAddress.setPostalCode("93908");

MapiContactPhysicalAddressPropertySet physAddrPropSet = new MapiContactPhysicalAddressPropertySet();
physAddrPropSet.setWorkAddress(physAddress);
contact.setPhysicalAddresses(physAddrPropSet);

// Set the email information using MapiContactElectronicAddress and its property set
MapiContactElectronicAddress email = new MapiContactElectronicAddress();
email.setAddressType("SMTP");
email.setDisplayName("Melissa MacBeth (melissa@contoso.com)");
email.setEmailAddress("melissa@contoso.com");

MapiContactElectronicAddressPropertySet elecAddrPropSet = new MapiContactElectronicAddressPropertySet();
elecAddrPropSet.setEmail1(email);
contact.setElectronicAddresses(elecAddrPropSet);

contact.save(dataDir + "OutlookContact_out.vcf", ContactSaveFormat.VCard);

Hinzufügen von Kontakt-Ereignisinformationen zu einem MapiContact

Microsoft Outlook ermöglicht es Benutzern, einem Kontakt Ereignisinformationen hinzuzufügen. Das Ereignis enthält Geburtstag und Hochzeitsjubiläum. Aspose.Email stellt das MapiContactEventPropertySet Klasse zum Hinzufügen dieser Informationen zu einem Kontakt. Dies wird im folgenden Beispiel erläutert.

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

java.util.Calendar calendar = java.util.Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.set(1990, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date birthday = calendar.getTime();

calendar.set(2012, java.util.Calendar.NOVEMBER, 1, 0, 0, 0);
Date weddingDay = calendar.getTime();

MapiContactEventPropertySet contactEventSet = new MapiContactEventPropertySet();
contactEventSet.setBirthday(birthday);
contactEventSet.setWeddingAnniversary(weddingDay);

MapiContact contact = new MapiContact();
contact.setEvents(contactEventSet);

contact.save(dataDir + "Contact_out.msg", ContactSaveFormat.Msg);

Erstellen, Speichern und Lesen von Outlook‑Kontakten

Aspose.Email ermöglicht Entwicklern das Erstellen von Microsoft Outlook‑Kontakten sowie E‑Mail‑Nachrichten. Das MapiContact Klasse liefert alle Kontakt‑Eigenschaften, die zum Erstellen eines Outlook‑Kontakts erforderlich sind. Dieser Artikel zeigt, wie man einen Outlook‑Kontakt mit der MapiContact Klasse.

Erstellen und Speichern eines MapiContact

Die folgenden Schritte können verwendet werden, um einen Kontakt zu erstellen und auf Festplatte zu speichern:

  1. Instanziieren Sie ein neues Objekt der MapiContact Klasse.
  2. Geben Sie Informationen zu den verschiedenen Eigenschaften des Kontakts ein.
  3. Fügen Sie dem Kontakt Fotodaten hinzu, falls vorhanden.
  4. Speichern Sie den Kontakt im MSG- oder VCard-Format.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiContact contact = new MapiContact("Sebastian Wright", "SebastianWright@dayrep.com");
contact.setNameInfo(new MapiContactNamePropertySet("Bertha", "A.", "Buell"));
contact.setProfessionalInfo(new MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant"));
contact.getPersonalInfo().setPersonalHomePage("B2BTies.com");
contact.getPhysicalAddresses().getWorkAddress().setAddress("Im Astenfeld 59 8580 EDELSCHROTT");
contact.getElectronicAddresses().setEmail1(
        new MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com"));
contact.setTelephones(new MapiContactTelephonePropertySet("06605045265"));

// Set the photo data
byte[] fileContent = Files.readAllBytes(Paths.get(dataDir + "Desert.jpg"));
contact.setPhoto(new MapiContactPhoto(fileContent, MapiContactPhotoImageFormat.Jpeg));

// Save as MSG
contact.save(dataDir + "Contact_out.msg", ContactSaveFormat.Msg);

Kontakt im VCF‑Version‑3‑Format speichern

Um den Kontakt im VCF‑Version‑3‑Format zu speichern, verwenden Sie die VCardVersion enumerable, um die VCardSaveOptions.Version Eigenschaft. Der folgende Beispielcode demonstriert die Verwendung von VCardVersion enumerable, um den Kontakt im VCF‑Version‑3‑Format zu speichern.

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

MapiContact contact = new MapiContact();
contact.setNameInfo(new MapiContactNamePropertySet("Jane", "A.", "Buell"));
contact.setProfessionalInfo(new MapiContactProfessionalPropertySet("Aspose Pty Ltd", "Social work assistant"));
contact.getPersonalInfo().setPersonalHomePage("Aspose.com");
contact.getElectronicAddresses().setEmail1(new MapiContactElectronicAddress("test@test.com"));
contact.getTelephones().setHomeTelephoneNumber("06605040000");

VCardSaveOptions opt = new VCardSaveOptions();
opt.setVersion(VCardVersion.V30);

contact.save(dataDir + "V30.vcf", opt);

Lesen eines MapiContact

Die MapiContact Klasse kann verwendet werden, um sowohl Microsoft Outlook‑MSG‑Dateien als auch Kontakte im VCard‑Format zu laden. Die folgenden Codebeispiele zeigen, wie Outlook‑Kontakte, die als MSG und VCF gespeichert wurden, in MapiContact.

Laden eines Kontakts aus MSG

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

MapiMessage msg = MapiMessage.load(dataDir + "messageMapi.msg");
MapiContact mapiContact = (MapiContact) msg.toMapiMessageItem();

System.out.println(mapiContact.getNameInfo().getDisplayName());

Laden eines Kontakts aus VCard

Der folgende Code zeigt, wie man ein MapiContact durch Laden von Daten aus einer VCard‑Datei mit VCardLoadOptions:

MapiContact mapiContact = MapiContact.fromVCard("contact.vcf", new VCardLoadOptions());
System.out.println(mapiContact.getNameInfo().getDisplayName());

VCard‑Kontakt mit angegebenem Encoding laden

Unterstützte Methode: MapiContact.fromVCard(String, Encoding)

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

MapiContact contactReadFromFile = MapiContact.fromVCard(dataDir + "microsoft.vcf", StandardCharsets.UTF_8);

MAPI‑Kontakte aus vCard mit benutzerdefinierten Optionen laden

Um mehr Flexibilität beim Konvertieren von VCard‑(.vcf)‑Dateien in MAPI‑Kontakte zu bieten, stellt Aspose.Email für Java das MapiContact.fromVCard(String filePath, VCardLoadOptions options) Überladung. Sie bietet verbesserte Kontrolle darüber, wie VCard-Dateien interpretiert werden – insbesondere beim Arbeiten mit unterschiedlichen VCard-Formaten, Codierungen oder fortgeschrittenen Parsing‑Szenarien.

Der geladene Kontakt kann dann innerhalb von PST-Dateien, MSG-Export oder anderen Outlook-kompatiblen Formaten verwendet werden.

MapiContact mapiContact = MapiContact.fromVCard("contact.vcf", new VCardLoadOptions());
System.out.println(mapiContact.getNameInfo().getDisplayName());

Speichern von VCard-Dateien mit erweiterten Feldern

Die UseExtensions Eigenschaft von VCardSaveOptions ermöglicht Ihnen die Steuerung, ob erweiterte (X-) Felder können beim Speichern von vCard‑Dateien verwendet werden. Wenn eingestellt auf true (Standard), Erweiterungen sind erlaubt und bieten Kompatibilität mit benutzerdefinierten Feldern und zusätzlichen Kontaktinformationen.

Das folgende Code‑Beispiel zeigt, wie man beim Speichern eines Kontakts die erweiterten Felder deaktiviert:

MapiContact contact = MapiContact.fromVCard("contact.vcf");

VCardSaveOptions options = new VCardSaveOptions();
options.setUseExtensions(false);

contact.save("contact_out.vcf", options);

Produkt‑Bezeichner eines Kontakts lesen und setzen

Eine vCard‑Datei zeichnet das Produkt, das sie erstellt hat, im PRODID Feld. Aspose.Email behält diesen Bezeichner bei, wenn ein Kontakt zwischen vCard und MSG konvertiert wird, sodass Sie immer die Quelle eines Kontakts nachverfolgen können. Der Bezeichner ist über die folgenden Mitglieder verfügbar:

Das folgende Code‑Snippet liest den Produkt‑Bezeichner aus einer vCard‑Datei, konvertiert den Kontakt zu MSG und speichert ihn mit einem neuen Bezeichner:

// Read the product identifier from a vCard file
VCardContact vcard = VCardContact.load("contact.vcf");
System.out.println("vCard product ID: " + vcard.getExplanatoryInfo().getProdId());

// The identifier is kept when the contact is converted to MSG
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
vcard.save(msgStream, ContactSaveFormat.Msg);
MapiMessage msg = MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray()));
MapiContact contact = (MapiContact) msg.toMapiMessageItem();
System.out.println("MapiContact product ID: " + contact.getProductId());

// Save the contact to MSG with a different product identifier
MapiContactSaveOptions options = new MapiContactSaveOptions();
options.setProductId("-//Contoso//Address Book 2.0//EN");
contact.save("contact_out.msg", options);

Um den Bezeichner für einen neuen Kontakt festzulegen, rufen Sie setProductId oder darüber leiten VCardSaveOptions beim Speichern des Kontakts als vCard:

MapiContact contact = new MapiContact("John Smith", "john.smith@example.com");
contact.setProductId("-//Contoso//Address Book 2.0//EN");
contact.save("contact_out.vcf", ContactSaveFormat.VCard);

// Override the identifier for a single save operation
VCardSaveOptions options = new VCardSaveOptions();
options.setProductId("-//Contoso//Export Tool 1.0//EN");
contact.save("contact_export.vcf", options);

Mehrfach‑Kontakt‑VCF‑Dateien in MapiDistributionList konvertieren

Aspose.Email unterstützt die Konvertierung von Mehrfach‑Kontakt‑VCF‑Dateien in MapiDistributionList Objekte, wodurch die Verwaltung und der Import mehrerer Kontakte direkt in Ihre Anwendungen erleichtert wird. Diese Funktion ist über die folgenden statischen Methoden der MapiDistributionList Klasse:

  • static MapiDistributionList fromVCF(String filePath)
  • static MapiDistributionList fromVCF(InputStream stream)

Das folgende Codebeispiel demonstriert die Verwendung dieser Funktion:

// Convert a multi-contact VCF file into a MapiDistributionList
MapiDistributionList dlist = MapiDistributionList.fromVCF("contacts.vcf");

System.out.println(dlist.getDisplayName() + ": " + dlist.getMembers().size() + " member(s)");

Die resultierende Verteilerliste kann in einer VCF‑Datei in der gewünschten vCard‑Version zurückgeschrieben werden:

MapiDistributionList dlist = MapiDistributionList.fromVCF("contacts.vcf");

MapiDistributionListSaveOptions options = new MapiDistributionListSaveOptions(ContactSaveFormat.VCard);
options.setVersion(VCardVersion.V30);

dlist.save("distribution_list.vcf", options);

Direktes Arbeiten mit vCard‑Dateien

Wenn Ihre Quelle und Ihr Ziel vCard‑Dateien sind und Sie keine Outlook/MAPI‑Funktionen benötigen, verwenden Sie die VCardContact Klasse anstelle von MapiContact. Liest und schreibt VCF direkt, unterstützt vCard 2.1/3.0/4.0 und mehrere Kontakte pro Datei. Siehe Arbeiten mit vCard (VCF)-Dateien für eine detaillierte Beschreibung des vCard‑Objektmodells.

Darstellung von Kontaktinformationen in MHTML

Outlook‑Kontakt kann mit der Aspose.Email‑API in MHTML konvertiert werden. Dieses Beispiel zeigt, wie eine VCard in MapiContact und dann mit Hilfe von MailMessage API.

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

// Load the VCF contact and convert it to a MailMessage for rendering to MHTML
MapiContact contact = MapiContact.fromVCard(dataDir + "Contact.vcf");

ByteArrayOutputStream os = new ByteArrayOutputStream();
contact.save(os, ContactSaveFormat.Msg);

MapiMessage msg = MapiMessage.load(new ByteArrayInputStream(os.toByteArray()));
MailMessage eml = msg.toMailMessage(new MailConversionOptions());

// Prepare the MHT format options
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setCheckBodyContentEncoding(true);
mhtSaveOptions.setPreserveOriginalBoundaries(true);
mhtSaveOptions.setMhtFormatOptions(MhtFormatOptions.RenderVCardInfo | MhtFormatOptions.WriteHeader);
mhtSaveOptions.setRenderedContactFields(ContactFieldsSet.NameInfo | ContactFieldsSet.PersonalInfo
        | ContactFieldsSet.Telephones | ContactFieldsSet.Events);

eml.save(dataDir + "Contact_out.mhtml", mhtSaveOptions);