Reputation: 303
I designed a UI for sending basic email which can be done through
SMTPClient
deliverMailFrom: sender
to: recipients
text: message
usingServer: '' .
I have a method that will take Email ,subject all headers from user. Here is my Method.
AddHeader:Headername Email: asString Message:message --> "I want to update this"
" update is nothing but adding dictionay values to that "
|data|
data:= Dictionary new.
'To'= Headername ifTrue[data at:#To put:Email.].
'From'= Headername ifTrue[data at: #From put:Email].
'subject'= Headername ifTrue[data at: #subject put:Email].
"adding dicitonay values to message"
message:= String streamContents:[:stream|
data values do:[:each| stream nextPutAll: each ]
separatedBy: [ stream nextPut: Character space ]].
When i try to save this method i get an error saying Cannot store into message. so whats the right way to add values into message that's already been defined.
And other thing values added from dictionary are not added in the order they been put. so is there a way i can add these values in order they been put like To first, From next , then subject..
Upvotes: 0
Views: 400
Reputation: 1693
I don't know if you are asking just for the error or if you are trying to achieve something else on top of what your code does. Since the code is scattered and incomplete I can't give you a proper answer, but I'll adapted a code similar to yours and it seems to work:
|data|
data:= Dictionary new.
('To'= 'To') ifTrue: [data at:#To put:'[email protected]'].
('To'= 'Another thing') ifTrue: [data at:#To put:'shouldnt appear'].
data at:#From put:'[email protected]'.
message:= String streamContents:
[:stream|
data values
do:[:each| stream nextPutAll: each ]
separatedBy: [ stream nextPut: Character space ]].
If you copy this in a workspace and inspect the data
contents it will give you the following ByteString
'[email protected] [email protected]'
. But I think that you have already accomplished this task. Could you elaborate a bit more on what you are looking for and put the complete code, just as you coded it?
Edit based on the OP clarification
Ok, now I see the problem. The thing is that in Smalltalk you don't have "out" parameters and that is why the compiler is telling you that it can't store new contents in the message
parameter. From the point of view of the object sending the message, you see that message send as telling an object to perform an action and passing the required collaborators to that object to perform it. If there is a response from the message send it is expected to be in the return value. I re-wrote your method a little bit here to do that:
addHeader: headerName email: aString message: aMessage
| data newValues |
data:= Dictionary new.
'To'= headerName ifTrue: [data at:#To put: aString.].
'From'= headerName ifTrue: [data at: #From put: aString].
'subject'= headerName ifTrue: [data at: #subject put: aString].
"adding dicitonay values to message"
newValues := String streamContents:[:stream|
data values do:[:each| stream nextPutAll: each ]
separatedBy: [ stream nextPut: Character space ]].
^aMessage , newValues.
as you can see the idea is to use the #,
message to join the two strings and return the new value. Some things to note here are:
#asSymbol
message to avoid the if statements.HTH
Upvotes: 2