| View previous topic :: View next topic |
| Author |
Message |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Fri Jan 06, 2006 6:37 am Post subject: module com.sun.star.mail |
|
|
Hi,
Caolan offered with 2.0.1 the possibility to mailmerge with email (/program/mailmerge.py).
I would like to use the subject module for mailing in some cases.
I know the css.system.SimpleSystemMail service, but it has some restrictions (no access to body, standard mailhandler required).
Has anybody played with this?
I can use the service com.sun.star.mail.MailserviceProvider, but for that I need the context for the server settings...
I can't get the service com.sun.star.mail.Mailserver (null-object), where I can provide these server settings
Is the JavaMail api required here?
Sincerely
Peter |
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
Posted: Tue Jan 10, 2006 1:34 pm Post subject: |
|
|
Hi,
I have played around a bit with the mail API in 2.0.1 on Win XP, but to no success. In the code below, all the objects are created, and I beleive that the provider is even accessed. At the end, however, a strange error (mailmerge.py expecting 2 parameters but got 3) arises ... maybe you are more successful
Good luck,
ms777
| Code: |
Sub Main
oMSP = createUnoService("com.sun.star.mail.MailServiceProvider")
'xray oMSP
oMS = oMSP.create(com.sun.star.mail.MailServiceType.POP3)
'xray oMS
oCurCont = CreateUnoListener("CurCont_", "com.sun.star.uno.XCurrentContext")
oAuthent = CreateUnoListener("Authent_", "com.sun.star.mail.XAuthenticator")
'xray oCurCont
'xray oAuthent
oMS.connect(oCurCont, oAuthent)
xray oMS
End Sub
function CurCont_getValueByName(s as String) as Any
select case LCase(s)
case "servername"
CurCont_getValueByName = "pop3.web.de"
case "port"
CurCont_getValueByName = CLng(110)
case "connectiontype"
CurCont_getValueByName = "ssl"
end select
end function
function Authent_getUserName() as String
Authent_getUserName = "myUserName_changeme"
end function
function Authent_getPassword() as String
Authent_getPassword = "myPassword_changeme"
end function |
|
|
| Back to top |
|
 |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Fri Jan 20, 2006 8:52 am Post subject: |
|
|
Thanks for the hint with the listeners and the callback functions, where did you found that stuff?
Yes, I can connect to the SMTP-server and will try to send a mail in a next step.
Pop with ssl: Isn't it port 995 ?
Peter |
|
| Back to top |
|
 |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Thu Jan 26, 2006 4:24 am Post subject: |
|
|
Here's the code for connecting to SMTP:
oMailProvider = CreateUNOService("com.sun.star.mail.MailServiceProvider")
oCont = CreateUNOListener("CurCont_","com.sun.star.uno.XCurrentContext")
oAuth = CreateUNOListener("Authent_","com.sun.star.mail.XAuthenticator")
xMailServer = oMailProvider.Create("com.sun.star.mail.SMTP")
xMailServer.Connect(oCont,oAuth)
msgbox xMailServer.isConnected
XRay xMailServer
xMailServer.Disconnect()
'the message can be sent with xMailserver.sendMailMessage(xMailMessage) but I found no way to instanciate a com.sun.star.mail.MailMessage
Function CurCont_GetValueByName(s) as Any
Select Case s
Case "ServerName"
CurCont_GetValueByName = "MyMailServer"
Case "Port"
CurCont_GetValueByName = 25
Case "ConnectionType"
CurCont_GetValueByName = "Insecure"
End Select
End Function
Function Authent_GetUserName() as Any
Authent_GetUserName = ""
End Function
Function Authent_GetPassword()
Authent_GetPassword = ""
End Function |
|
| Back to top |
|
 |
-Manu_ Newbie

Joined: 28 May 2006 Posts: 2
|
Posted: Sun May 28, 2006 7:50 am Post subject: sending mail |
|
|
this is very intersting,
but when i didn't succed in sending mail.
When you said "'the message can be sent with xMailserver.sendMailMessage(xMailMessage) but I found no way to instanciate a com.sun.star.mail.MailMessage "
Does is it mean that you are in the same case ?
How can you creat a xMailMessage ?
Thanks
Emmanuel |
|
| Back to top |
|
 |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Sun May 28, 2006 10:34 pm Post subject: Re: sending mail |
|
|
| -Manu_ wrote: |
How can you creat a xMailMessage ?
|
Nope, I can't. That's the problem.
I think it can't be done in BASIC.
Caolan did it in the python mailmerge script, so I'm hoping an experienced python programmer can finish this task. And the python script could be invoked by a BASIC routine.
Peter |
|
| Back to top |
|
 |
-Manu_ Newbie

Joined: 28 May 2006 Posts: 2
|
Posted: Wed May 31, 2006 11:34 am Post subject: sending mail |
|
|
hi,
just a question :
why this :
MonMsg=CreateUnoService("com.sun.star.mail.MailMessage")
MonMsg.Create("from@mail.com", "to@maill.com", "subject", "msg")
doesn't work ? (or shouldn't work)
Manu
PS :i'm not an expert of openoffice basic api! |
|
| Back to top |
|
 |
andreasm Power User

Joined: 06 Dec 2003 Posts: 54 Location: South Tyrol
|
Posted: Sun May 24, 2009 10:53 am Post subject: |
|
|
Hello,
I know this is a old thread, but I would like to use the mailing functions of openoffice.
I tried the code posted by Peter but I got an exception.
I looked at
http://api.openoffice.org/docs/common/ref/com/sun/star/mail/XMailServer.html#Name
and I think there are some attributes missing.
Nowhere in the code the destination smtp server is set !
According to the documentation the name and port attributes must be set.
Can anyone tell how to set attributes in ooobasic ?
I tried with
xMailserver.name="mail.gmx.net"
rem xMailserver.port=25
but this does not work
Bye
Andreas |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3531 Location: Hamburg, Germany
|
|
| Back to top |
|
 |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Sun May 24, 2009 11:34 pm Post subject: |
|
|
| andreasm wrote: |
Nowhere in the code the destination smtp server is set !
According to the documentation the name and port attributes must be set.
|
Take a look at the function CurCont_GetValueByName() and you will find the server name and port.
Peter |
|
| Back to top |
|
 |
andreasm Power User

Joined: 06 Dec 2003 Posts: 54 Location: South Tyrol
|
Posted: Tue May 04, 2010 11:17 pm Post subject: |
|
|
Hello,
maybe I am wrong, but I studied Caolan's python source and IMHO he does not only use the sendmail function, he implemented it in python !
I added this lines to Peter's basic code ( above )
oMail = CreateUNOListener("Mail_","com.sun.star.mail.XMailMessage")
and after the connect
xMailServer.SendMailMessage(oMail)
and this callback functions:
function Mail_SenderAddress()
Msgbox ("Sender")
Mail_SenderAddress="sender@test.net"
end function
function Mail_GetRecipients()
msgbox("Recipients")
Mail_GetRecipients="recipient@test.net"
end function
GetRecipient IS called, but I get a conversion error, because a array( or somethin similar ) is expected as return value.
Does someone know how to return the expected data ?
Bye
Andreas |
|
| Back to top |
|
 |
Peter OOo Enthusiast

Joined: 28 May 2004 Posts: 105 Location: Berlin / Germany
|
Posted: Wed May 05, 2010 1:08 am Post subject: |
|
|
Did you try Mail_GetRecipients=Array("recipient@test.net") ?
Peter |
|
| Back to top |
|
 |
andreasm Power User

Joined: 06 Dec 2003 Posts: 54 Location: South Tyrol
|
Posted: Thu May 06, 2010 12:11 am Post subject: |
|
|
Hello,
no, Now there is another error
type coercion failed: TYPE is not supported
Bye
Andreas |
|
| Back to top |
|
 |
andreasm Power User

Joined: 06 Dec 2003 Posts: 54 Location: South Tyrol
|
Posted: Fri May 14, 2010 11:32 pm Post subject: |
|
|
Hello,
thanks to Caolan we are near to a solution
http://qa.openoffice.org/issues/show_bug.cgi?id=111508
What I miss now is a way to send a attachment.I tried with
oMail = oMailServiceObj.createWithAttachment()
but I have to create a com:sun:star:mail:MailAttachment out of a file and don't know how to do it.
Bye
Andreas |
|
| Back to top |
|
 |
DonJaime Newbie

Joined: 05 Jan 2012 Posts: 2
|
Posted: Thu Jan 05, 2012 3:15 pm Post subject: |
|
|
With the python fix it is now possible to create and send a message with a body. None of the 'add' methods of XMailMessage work (they all cause an error when sending the message). And it is still completely obscure how an attachment is meant to be created from a file: (whining went here).
Edit next day:
Quit whinging and solved the attachment problem: the documentation for com.sun.star.mail.MailAttachment says that the attachment data should be provided as a "sequence of bytes", but in fact what is required is a byte sequence. Which can be created from a sequence (Array) of bytes as returned by the com.sun.star.ucb.SimpleFileAccess service's readBytes() function by using the CreateUnoValue() function. So here is how I send an email with a body and an attachment from OpenOffice.org/LibreOffice:
| Code: |
Option Explicit
' ======= just for testing =======
const testRecipient = "you@example.com"
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, "No attachment", "Test the second")
end sub
' ======== your details go here ========
const myAddress = "you@example.com"
const smtpServer = "mail.example.com"
const smtpPort = 25
const smtpSecure = true
const smtpUser = "you@example.com"
const attachmentMaxSize = 100000
' these variables are needed by listener functions, to which they cannot be passed as arguments
private smtpPassword as String
private messageBody as String
private attachmentContents as Object
private 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
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
' ======= extract attachment data from file ========
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 "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
|
|
|
| Back to top |
|
 |
|