ImapUploadMessage Method (MailMessage, String, String, String, Boolean, UidPlusResult) |
Namespace: MailBee.ImapMail
public bool UploadMessage( MailMessage msg, string folderName, string flags, string dateTimeString, bool batchMode, UidPlusResult result )
Exception | Condition |
---|---|
MailBeeException | An error occurred and ThrowExceptions is true. |
This method implements APPEND command of the IMAP4 protocol.
To upload a file, the developer can first load it into MailMessage object using LoadMessage(String) method, and then call UploadMessage(MailMessage, String, String, String, Boolean, UidPlusResult) passing a reference to this MailMessage object. There is no extra overhead on parsing the message since LoadMessage(String) just reads a file into memory. The MailMessage won't parse the contained message unless the application starts accessing its properties or methods (so-called "lazy" model).
To upload a mail message just sent using Smtp component, the developer call UploadMessage(MailMessage, String, String, String, Boolean, UidPlusResult) method passing a reference to the Message property.
To get standard message flags as string in IMAP4 format (e.g. "\Seen \Flagged"), the developer can use SystemFlagsToString(SystemMessageFlags) method.
In addition to the flags specified by the application, the server also sets "\Recent" flag. Thus, the uploaded message will always have at least "\Recent" flag set.
To get datetime string in IMAP4 format (e.g. "01-May-2004 05:26:59 -0600"), the developer can use GetImapDateTimeString method.
batchMode value is ignored if the server does not support LITERAL+ extension. However, even if LITERAL+ is supported, the developer may still decide not to use batch mode if there is a risk that the server will reject the message due to its size. If this happens, the client will waste bandwidth transferring the entire message. If the client wouldn't have used the LITERAL+, this could have been avoided, because the server would have rejected the initial small request containing the length of the data to be uploaded, and thus second large request containing the actual message data would have never been sent. In general, however, if the application is not going to upload very large messages frequently, it's more effective to keep batch mode on. Other overloads of UploadMessage(MailMessage, String, String, String, Boolean, UidPlusResult) method always upload messages in batch mode if LITERAL+ is supported.
When result is specified and the server supports and correctly implements UIDPLUS extension, UploadMessage(MailMessage, String, String, String, Boolean, UidPlusResult) method will set the supplied UidPlusResult object properties as below:
Property | Value |
---|---|
IsValid | true |
IsSupported | true |
SrcUids | null |
SrcUidString | null |
DestUids | The UidCollection object containing a single value of the UID assigned to the uploaded message. |
DestUidString | The string containing the UID assigned to the uploaded message. |
DestUidValidity | The UIDVALIDITY of the folder the message was uploaded to. |
If UIDPLUS capability is not supported by the server, IsSupported will be set to false. When working with the server which lacks UIDPLUS support, the application can obtain UID of the uploaded message from UidNext value of FolderStatus object returned by GetFolderStatus(String) method. However, the application must call GetFolderStatus(String) method BEFORE making upload (see the sample).
The developer can also try to avoid calling GetFolderStatus(String) method if the UIDNEXT value is already available in UidNext property (the destination folder must be selected in this case, and some other restrictions apply, see remarks in UidNext topic for more information).
Some servers claim they support UIDPLUS but in fact there is no UIDPLUS data in the response (of the data has incorrect format). IsSupported will still be true but IsValid will be false in this case.
using System; using MailBee; using MailBee.ImapMail; using MailBee.Mime; class Sample { static void Main(string[] args) { Imap imp = new Imap(); // Connect to the server and log in the account. imp.Connect("mail.server.com"); imp.Login("jdoe", "secret"); // Load the message from C:\Temp\message.eml file. MailMessage msg = new MailMessage(); msg.LoadMessage(@"C:\Temp\message.eml"); // Prepare the object that will receive upload results. UidPlusResult res = new UidPlusResult(); long uid = 0; if (imp.GetExtension("UIDPLUS") == null) { FolderStatus status = imp.GetFolderStatus("Inbox"); uid = status.UidNext; } // Upload the message and fill res with upload results. imp.UploadMessage(msg, "Inbox", MessageFlagSet.SystemFlagsToString( SystemMessageFlags.Seen | SystemMessageFlags.Flagged), "12-Apr-2006 19:30:00 +0100", true, res); if (res.IsSupported) { Console.WriteLine("UID of the uploaded message is " + res.DestUidString + ", UIDPLUS supported."); } else { Console.WriteLine("UID of the uploaded message is " + uid.ToString() + ", UIDPLUS not supported."); } // Disconnect from the server. imp.Disconnect(); } }