Reading Joachim Tuchel’s posting about problems with SMTP and sending EMails I tried the Mono interface to use the Mono infrastructure to send emails.
First to reduce complexity I created a new class under Mono to reduce the number of methods needed to call from Smalltalk:
using System;
using System.Net.Mail;
namespace MSK
{
public class SmtpHelper
{
public SmtpHelper (string pHost, string pFrom, string pTo, string pSubject, string pBody, string pAccount, string pPassword)
{
this.smtpClient = new SmtpClient(pHost);
this.mm = new MailMessage( pFrom, pTo, pSubject, pBody);
this.accountName = pAccount;
this.accountPassword = pPassword;
}
private MailMessage mm;
private SmtpClient smtpClient;
private string accountName;
private string accountPassword;
public void AddFile (string pPath, string pType)
{
switch (pType) {
case "jpg" :
mm.Attachments.Add (new Attachment (pPath, System.Net.Mime.MediaTypeNames.Image.Jpeg));
break;
}
}
public string SendEMail()
{
if (! String.IsNullOrEmpty( accountName)) {
smtpClient.Credentials = new System.Net.NetworkCredential(accountName, accountPassword);
}
string errorString = "";
try {
smtpClient.Send ( mm);
}
catch (Exception e) {
errorString = e.Message;
}
return errorString;
}
}
}
… and here is the example to use this class for sending an EMail with an attachment:
sendEmail
"MSKMonoInterfaceApp sendEmail"
| monoMethod argv aMonoClass aMonoObject result |
(MoDomain openAssembly: '.\SmtpLibraryHelper.dll') isNil
ifTrue: [^self error: 'Error: No Assembly found!'].
(aMonoObject := MoDomain
newInstanceOfClassNamed: 'SmtpHelper'
namespace: 'MSK') isNil
ifTrue:[ ^self error: 'Object cold not be created !' ].
(monoMethod := MoThread
methodName: '.ctor(string,string,string,string,string,string,string)'
className: 'SmtpHelper'
namespace: 'MSK') isNil
ifTrue:[ ^self error: 'No method found !' ].
argv := MonoArgumentList newAsync
addArgument: 'your smtp-server ;
addArgument: 'from' ;
addArgument: 'to' ;
addArgument: 'Hello from Mono C# via Smalltalk' ;
addArgument: 'I send you a specific image' ;
addArgument: 'accountname' ;
addArgument: 'password' ;
yourself.
result := MoThread
methodInvoke: monoMethod
receiver: aMonoObject
args: argv monoArgs
exception: nil.
argv freeMonoArgs.
argv := MonoArgumentList newAsync
addArgument: 'C:\Jellyfish.jpg' ;
addArgument: 'jpg'
yourself.
(monoMethod := MoThread
methodName: 'AddFile'
className: 'SmtpHelper'
namespace: 'MSK') isNil
ifTrue:[ ^self error: 'No method found !' ].
result := MoThread methodInvoke: monoMethod receiver: aMonoObject args: argv monoArgs exception: nil.
argv freeMonoArgs.
(monoMethod := MoThread
methodName: 'SendEMail'
className: 'SmtpHelper'
namespace: 'MSK') isNil
ifTrue:[ ^self error: 'No method found !' ].
result := (MoThread
methodInvoke: monoMethod
receiver: aMonoObject
args: nil
exception: nil) asString.
result inspect
Pingback: Sending emails from VA Smalltalk | Joachims Small World