Mega Code Archive

 
Categories / Delphi / Files
 

Get attached files using NMPop3

Title: Get attached files using NMPop3. Question: How do I get attached files usingo NMPop3? Answer: To create this example, you will need to create a new blank Delphi application. Place 6 TEdits, a TMemo, a TCheckBox, 2 TButtons, and a TNMPOP3 on the Form. Component Descriptions: Edit1: Host Edit2: User ID Edit3: Password Edit4: Attachment Path Edit5: From (Person that sent the E-Mail) Edit6: Subject of the E-Mail Memo1: Message Body Button1: Connect/Disconnect Button2: Get Mail Message CheckBox1: Delete message after reading Insert the following code into Button1's OnClick event: procedure TForm1.Button1Click(Sender: TObject); begin if NMPOP31.Connected then NMPOP31.Disconnect else begin NMPOP31.Host := Edit1.Text; NMPOP31.UserID := Edit2.Text; NMPOP31.Password := Edit3.Text; NMPOP31.DeleteOnRead := Checkbox1.Checked; NMPOP31.AttachFilePath := Edit4.Text; NMPOP31.Connect; end; end; When Button1 gets clicked, if there is a connection with the remote host, the Disconnect method is executed to close the connection. If there is no connection present, the Host property, which specifies the mail server to connect to, is set to the value in Edit1. The UserID property, which specifies the user name to log in as, is set to the vlaue in Edit2. The Password property, which should correspond with the UserID property, is set to the value in Edit3. If CheckBox1 is checked (true), then messages are deleted as they are retrieved by the GetMailMessage method, since the DeleteOnRead property is set to the value of CheckBox1.Checked. the AttachFilePath property is set to the value in Edit4, and the Connect method establishes a connection with the remote host. Insert the following test into NMPOP31's OnConnect method: procedure TForm1.NMPOP31Connect(Sender: TObject); begin if NMPOP31.MailCount 0 then ShowMessage(IntToStr(NMPOP31.MailCount)+' messages in your mailbox') else ShowMessage('No messages waiting'); end; When the client connects to the mail host, the OnConnect event is called. If there are messages waiting on the server, a message box pops up displaying the number stored in the MailCount property, stating how many messages are waiting on the server. If there are no messages, a box is displayed stating that there are no messages. Insert the following code into Button2's OnClick event: procedure TForm1.Button2Click(Sender: TObject); var S: String; M: Integer; begin if NMPOP31.MailCount 0 then begin if InputQuery('Retrieve an E-Mail message', 'Which message? (1-'+IntToStr(NMPOP31.MailCount)+')', S) then begin M := StrToIntDef(S, -1); If (M NMPOP31.MailCount) then ShowMessage('Invalid message index') else NMPOP31.GetMailMessage(M); end; end else ShowMessage('No Messages to Get'); end; When Button2 is clicked, if there are messages on the server (NMPOP31.MailCount 0), the InputQuery function requests a message number to retrieve. If the OK button in clicked, the number typed in is checked for validity with the actual number of messages. If the specified mail number is invalid, a message box is displayed saying the message doesn't exist. If the specified message exists, it is retrieved using the GetMailMessage method. Insert the following code into NMPOP31's OnRetrieveEnd event: procedure TForm1.NMPOP31RetrieveEnd(Sender: TObject); begin Memo1.Text := NMPOP31.MailMessage.Body.Text; Edit6.Text := NMPOP31.MailMessage.Subject; Edit5.Text := NMPOP31.MailMessage.From; end; The OnRetrieveEnd event is called when a message has completed being retrieved from the remote host. In this case, Memo1 is set to the Body of the MailMessage property, displaying the contents of the message.Edit6 is set to the Subject of the MailMessage property, displaying the subject line of the message. Edit5 is set to the sender of the message, stored in the From property of the MailMessage property Insert the following code into NMPOP31's OnDecodeStart event: procedure TForm1.NMPOP31DecodeStart(var FileName: String); var S: String; begin S := FileName; if InputQuery('Save File Attachment', 'Filename?', S) then FileName := S; end; When a file attachment begins decoding, the OnDecodeStart event is called. In this example, the InputQuery function is used to retrieve a file name. If the Ok button is clicked, the FileName parameter is changed to the new value entered in the input box.