I'm developing a club members database from which I wish to send email messages to members which need to have a Subject & Body text, so I believe that I cannot use the Simple mail functions.
I have found and adapted the code from RPG's thread ('How to get parameters of objects, services', posted on 4th Feb 2013; sorry I don't know how to create a hyperlink to this).
When I execute this code step-by-step it fails as it attempts to do the 'service.connect(server, mailuser)' line. This calls the function 'smtp_getvaluebyname(what)' with arguments 'Servername', 'Port' & 'Timeout'. It fails after setting the 'Timeout' parameter with a BASIC run-time error, the last line of which reads:
C\Program Files (x86)\OpenOffice 4\program\mailmerge.py:114 in function connect() [self.server = smtplib.SMTP(server, port, timeout=tout)] Can anyone advise me what is wrong with my code and how to fix it?
My code is: -
Code: Select all
' ************************************************************************************************
' email routines
' ======= extract attachment data from file ========
' ------------------------------------------------------------------------------------------------
sub Testmail
smtpPassword = inputBox("Password", "Password:", "")
attachmentType = "application/pdf"
' sendMail(testRecipient, "Attachment", "Test the first", _
' ConvertToUrl("c:\Documents and Settings\James\Desktop\test.pdf"))
sendMail(testRecipient, "Test email", "Test email body text")
end sub
' ======== your details go here ========
global const myAddress = "jeff.asplin@gmail.com"
global const smtpServer = "smtp.gmail.com"
global const smtpPort = 465
global const smtpTimeout = 20
global const smtpSecure = true
global const smtpUser = "jeff.asplin@gmail.com"
global const testRecipient = "jeff.asplin@ntlworld.com"
global const attachmentMaxSize = 100000
' these variables are needed by listener functions, to which they cannot be passed as arguments
global smtpPassword as String
global messageBody as String
global attachmentContents as Object
global attachmentType as String
sub sendMail(recipient, subject, body, Optional attachmentUrl)
dim bodyObject as Object, attachment as Object, message as Object, _
serviceProvider as Object, service as Object, server as Object, mailUser as Object
bodyObject = CreateUNOListener("body_", "com.sun.star.datatransfer.XTransferable")
messageBody = body
if not IsMissing(attachmentUrl) then
if attachmentFromUrl(attachment, attachmentUrl) <> "OK" then
exit sub
end if
end if
message = com.sun.star.mail.MailMessage.createWithAttachment(recipient, myAddress, subject, bodyObject, attachment)
serviceProvider = CreateUNOService("com.sun.star.mail.MailServiceProvider")
service = serviceProvider.Create("com.sun.star.mail.SMTP")
server = CreateUNOListener("smtp_", "com.sun.star.uno.XCurrentContext")
mailUser = CreateUNOListener("user_", "com.sun.star.mail.XAuthenticator")
service.Connect(server, mailUser)
service.SendMailMessage(message)
service.Disconnect()
end sub
' --------------------------------------------------------------------------------------------------------
function attachmentFromUrl(attachment, url)
dim chunks() as String, fileName as String, fileService as Object, _
attachmentFile as Object, fileContents() as Byte
chunks() = Split(url, "/")
fileName = ConvertFromUrl(chunks(UBound(chunks()))
fileService = createUnoService("com.sun.star.ucb.SimpleFileAccess")
attachmentFile = fileService.OpenFileRead(url)
attachmentFile.ReadBytes(fileContents(), attachmentMaxSize + 1)
attachmentFile.CloseInput
if (UBound(fileContents()) + 1 > attachmentMaxSize) then
msgBox "Attachment file '" & fileName & "' is too big"
exit function
elseIf (UBound(fileContents()) < 0) then
msgBox "Attachment file '" & fileName & "' is empty"
exit function
end if
attachmentContents = CreateUnoValue( "[]byte", fileContents())
attachment = CreateObject("com.sun.star.mail.MailAttachment")
attachment.Data = CreateUNOListener("attachment_", "com.sun.star.datatransfer.XTransferable")
attachment.ReadableName = fileName
attachmentFromUrl = "OK"
end function
' ===== server properties =====
' ------------------------------------------------------------------------------------------------
function smtp_GetValueByName(what)
select case what
case "ServerName"
smtp_GetValueByName = smtpServer
case "Port"
smtp_GetValueByName = smtpPort
case "Timeout"
smtp_GetValueByName = smtpTimeout
case "ConnectionType"
if smtpSecure then
smtp_GetValueByName = "SSL"
else
smtp_GetValueByName = "Insecure"
endif
end select
end function
' ====== email user methods =======
' ------------------------------------------------------------------------------------------------
function user_GetUserName()
user_GetUserName = smtpUser
end function
' ------------------------------------------------------------------------------------------------
function user_GetPassword()
user_GetPassword = smtpPassword
end function
' ====== message body object methods ======
' ------------------------------------------------------------------------------------------------
function body_GetTransferDataFlavors()
dim flavor as new com.sun.star.datatransfer.DataFlavor
flavor.MimeType = "text/plain;charset=utf-16"
flavor.HumanPresentableName = "Unicode text"
body_getTransferDataFlavors = Array(flavor)
end function
' ------------------------------------------------------------------------------------------------
function body_GetTransferData(flavor) as any
if (flavor.MimeType = "text/plain;charset=utf-16") then
body_GetTransferData = messageBody
end if
end function
' ------------------------------------------------------------------------------------------------
function body_isDataFlavorSupported(x as Object)
body_isDataFlavorSupported = (x.MimeType = "text/plain;charset=utf-16")
end function
' ====== attachment object methods ======
' ------------------------------------------------------------------------------------------------
function attachment_GetTransferDataFlavors()
dim flavor as new com.sun.star.datatransfer.DataFlavor
flavor.MimeType = attachmentType
flavor.HumanPresentableName = attachmentType
attachment_getTransferDataFlavors = Array(flavor)
end function
' ------------------------------------------------------------------------------------------------
function attachment_GetTransferData(flavor) as any
if (flavor.MimeType = attachmentType) then
attachment_GetTransferData = attachmentContents
end if
end function
' ------------------------------------------------------------------------------------------------
function attachment_isDataFlavorSupported(x as Object)
attachment_isDataFlavorSupported = (x.MimeType = attachmentType)
end function
' ------------------------------------------------------------------------------------------------