La modération vous a écrit: Titre modifié pour plus de clarté
Bonjour à Tous,
Juste ce petit message pour vous dire qu'il est désormais possible d'envoyer des emails en utilisant quelques lignes en Basic :
Code : Tout sélectionner
Sub Main
Rem Ask the user for a sender email address.
sSender = InputBox("Please enter the sender email address")
Rem User clicked Cancel.
if sSender = "" then
exit sub
endif
Rem Ask the user for recipient's email address.
sRecipient = InputBox("Please enter the recipient's email address")
Rem User clicked Cancel.
if sRecipient = "" then
exit sub
endif
Rem Ask the user for subject's email.
sSubject = InputBox("Please enter the subject's email")
Rem User clicked Cancel.
if sSubject = "" then
exit sub
endif
Rem Ask the user for content's email.
sBody = InputBox("Please enter the content's email")
Rem User clicked Cancel.
if sBody = "" then
exit sub
endif
Rem Ok now we have everything, we start sending an email.
Rem We will use 4 UNO services which are:
Rem - com.sun.star.mail.MailUser: This is the service which will ensure the correct configuration
Rem of SMTP and IMAP servers (we can thank Mozilla for the ISPBD database that I use).
Rem - com.sun.star.mail.MailServiceProvider: This is the service that allows you to use SMTP and
Rem IMAP servers. We will use this service with the help of the previous service.
Rem - com.sun.star.datatransfer.TransferableFactory: This service is a forge for the creation of
Rem Transferable which are the basis of the body of the email as well as these attached files.
Rem - com.sun.star.mail.MailMessage: This is the service that implements the email message.
Rem Now that everything is clear we can begin.
Rem First we create the email.
Rem This is our Transferable forge, it greatly simplifies the LibreOffice mail API...
oTransferable = createUnoService("com.sun.star.datatransfer.TransferableFactory")
Rem oBody is the body of the email. It is created here from a String but could also
Rem have been created from an InputStream, a file Url (file://...) or a sequence of bytes.
oBody = oTransferable.getByString(sBody)
Rem oMail is the email message. It is created from the com.sun.star.mail.MailMessage service.
Rem It can be created with an attachment with the createWithAttachment() method.
oMail = com.sun.star.mail.MailMessage.create(sRecipient, sSender, sSubject, oBody)
Rem Ask the user for the URLs of the attached files.
oDialog = createUnoService("com.sun.star.ui.dialogs.FilePicker")
oDialog.setMultiSelectionMode(true)
if oDialog.execute() = com.sun.star.ui.dialogs.ExecutableDialogResults.OK then
oFiles() = oDialog.getSelectedFiles()
Rem These two services are simply used to get a suitable file name.
oUrlTransformer = createUnoService("com.sun.star.util.URLTransformer")
oUriFactory = createUnoService("com.sun.star.uri.UriReferenceFactory")
for i = lbound(oFiles()) To ubound(oFiles())
oUri = getUri(oUrlTransformer, oUriFactory, oFiles(i))
oAttachment = createUnoStruct("com.sun.star.mail.MailAttachment")
Rem ReadableName must be entered. This is the name of the attached file
Rem as it appears in the email. Here we get the file name.
oAttachment.ReadableName = oUri.getPathSegment(oUri.getPathSegmentCount() - 1)
Rem The attachment is retrieved from an Url but same as for oBody
Rem it can be retrieved from a String, an InputStream or a sequence of bytes.
oAttachment.Data = oTransferable.getByUrl(oUri.getUriReference())
oMail.addAttachment(oAttachment)
next i
endif
Rem End of creating the email.
Rem Now we need to send the email.
Rem First we create a MailUser from the sender address. This is not necessary the
Rem sender address but it must follow the rfc822 (ie: my surname <myname@myisp.com>).
Rem The IspDB Wizard will automatically be launched if this user has never been configured.
oUser = com.sun.star.mail.MailUser.create(sSender)
Rem User canceled IspDB Wizard.
if isNull(oUser) then
exit sub
endif
Rem Now that we have the user we can check if they want to use a Reply-To address.
if oUser.useReplyTo() then
oMail.ReplyToAddress = oUser.getReplyToAddress()
endif
Rem In the same way I can test if the user has an IMAP configuration with oUser.supportIMAP()
Rem and then create an email thread if necessary. In this case you must:
Rem - Construct an email message thread (as done previously for oMail).
Rem - Create and connect to an IMAP server (as we will do for SMTP).
Rem - Upload this email to the IMAP server with: oServer.uploadMessage(oServer.getSentFolder(), oMail).
Rem - Once it has been uploaded, retrieve the MessageId with the oMail.MessageId property.
Rem - Set the oMail.ThreadId property to MessageId for all subsequent emails.
Rem Great you have successfully grouped the sending of emails into a thread.
Rem To send the email we need to create an SMTP server. Here's how to do it:
SMTP = com.sun.star.mail.MailServiceType.SMTP
oServer = createUnoService("com.sun.star.mail.MailServiceProvider").create(SMTP)
Rem Now we connect using the SMTP user's configuration.
oServer.connect(oUser.getConnectionContext(SMTP), oUser.getAuthenticator(SMTP))
Rem Well, that's it, we are connected, all we have to do is send the email.
oServer.sendMailMessage(oMail)
Rem Don't forget to close the connection.
oServer.disconnect()
MsgBox "The email has been sent successfully." & chr(13) & "Its MessageId is: " & oMail.MessageId
Rem Et voilà le tour est joué...
Rem Ok all done, this took 40 lines of code so enjoy...
Rem On the other hand, this is only an example of popularization,
Rem and all the necessary error checks are not in place...
End Sub
Function getUri(oUrlTransformer As Variant, oUriFactory As Variant, sUrl As String) As Variant
oUrl = createUnoStruct("com.sun.star.util.URL")
oUrl.Complete = sUrl
oUrlTransformer.parseStrict(oUrl)
oUri = oUriFactory.parse(oUrlTransformer.getPresentation(oUrl, false))
getUri = oUri
End Function
Il faut bien sur installer la dernière version de eMailerOOo...
Et si vous allez dans: Outils -> Macros -> Éditer les Macros... -> eMailerOOo -> SendEmail vous pourrez vous amuser avec cette macro.
Ah oui pour les fainéant qui ne prend pas la peine de lire les documentations:
- eMailerOOo ne fonctionne plus sous OpenOffice et sous Windows il faut la version 7 minimum de LibreOffice.
- Il faut aussi un JRE Java 11 minimum quelque soit l'installation.
- La macro SendEmail ne fonctionne pas si aucun document n'est ouvert dans LibreOffice (je ne sais pas pourquoi...)
Voilà...