lunes, 4 de junio de 2012

Enviar mail mediante codigo X++

Hola, este es el fragmento de codigo que sirve para poder enviar correos electrónicos mediante Dynamics AX (probado en version ax 2009)

SysEmailParameters sysEmailParameters = SysEmailParameters::find();
InteropPermission  interopPermission;
SysMailer            sysMailer;
;
 
//Inicialización de los parámetros de correo electrónico.
interopPermission = new InteropPermission(InteropKind::ComInterop);
interopPermission.assert();
sysMailer = new SysMailer();
 
if(sysEmailParameters.smtpRelayServerName)
{
 sysMailer.SMTPRelayServer(sysEmailParameters.smtpRelayServerName,
 sysEmailParameters.smtpPortNumber,
 sysEmailParameters.smtpUserName,
 SysEmailParameters::password(),
 sysEmailParameters.ntlm);
}
else
{
 sysMailer.SMTPRelayServer(sysEmailParameters.smtpServerIPAddress,
 sysEmailParameters.smtpPortNumber,
 sysEmailParameters.smtpUserName,
 SysEmailParameters::password(),
 sysEmailParameters.ntlm);
}
 
//Escribimos el Mail
sysMailer.htmlBody("Mensaje de prueba :)");
sysMailer.subject("Test 1");
sysMailer.fromAddress("dirección mail origen");
sysMailer.tos().appendAddress("dirección mail destino");
 
//Si queremos documento adjunto
sysMailer.attachments().add("Ruta y nombre dle fichero adjunto");
 
sysMailer.sendMail();

Si tuvieramos algún problema al momento de ejecutar el envío del mail, el error generalmente es por algo que ha ocurrido en la clase 'CDO.Message'.
El sistema mostrara un aviso de error parecido a este:
Method 'send' in COM object of class 'CDO.Message' returned error code 0x800...

Les dejo una lista de lo que significa cada posible codigo que les pudiera salir:

0x80040201Exception %1 was generated at address %2.
0x80040202
No data source has been opened for the object.
0x80040203
The object does not support this type of data source.
0x80040204
The object does not support the requested property name or namespace.
0x80040205
The object does not support the requested property.
0x80040206
The object is not active. It may have been deleted or it may not have been opened.
0x80040207
The object does not support storing persistent state information for objects.
0x80040208
The requested property or feature, while supported, is not available at this time or in this context.
0x80040209
No default drop directory has been configured for this server.
0x8004020A
The SMTP server name is required, and was not found in the configuration source.
0x8004020B
The NNTP server name is required, and was not found in the configuration source.
0x8004020C
At least one recipient is required, but none were found.
0x8004020D
At least one of the From or Sender fields is required, and neither was found.
0x8004020E
The server rejected the sender address. The server response was: %1
0x8004020F
The server rejected one or more recipient addresses. The server response was: %1
0x80040210
The message could not be posted to the NNTP server. The transport error code was %2. The server response was %1
0x80040211
The message could not be sent to the SMTP server. The transport error code was %2. The server response was %1
0x80040212
The transport lost its connection to the server.
0x80040213
The transport failed to connect to the server.
0x80040214
The Subject, From, and Newsgroup fields are all required, and one or more was not found.
0x80040215
The server rejected the logon attempt due to authentication failure. The server response was: %1
0x80040216
The content type was not valid in this context. For example, the root of an MHTML message must be an HTML document.
0x80040217
The transport was unable to log on to the server.
0x80040218
The requested resource could not be found. The server response was: %1.
0x80040219
Access to the requested resource is denied. The server response was: %1.
0x8004021A
The HTTP request failed. The server response was: %1.
0x8004021B
This is a multipart body part. It has no content other than the body parts contained within it.
0x8004021C
Multipart body parts must be encoded as 7bit, 8bit, or binary.
0x8004021E
The requested property was not found.
0x80040220
The "SendUsing" configuration value is invalid.
0x80040221
The "PostUsing" configuration value is invalid.
0x80040222
The pickup directory path is required and was not specified.
0x80040223
One or more messages could not be deleted.
0x80040227
The property is read-only.
0x80040228
The property cannot be deleted.
0x80040229
Data written to the object are inconsistent or invalid.
x8004022A
The requested property is not in the mail header namespace.
0x8004022B
The requested character set is not installed on the computer.
0x8004022C
The ADO stream has not been opened.
0x8004022D
The content properties are missing.
0x8004022E
Content properties XML must be encoded using UTF-8.
0x8004022F
Failed to parse content properties XML.
0x80040230
Failed to convert a property from XML to a requested type.
0x80040231
No directories were specified for resolution.
0x80040232
Failed to resolve against one or more of the specified directories.
0x80040233
Could not find the Sender's mailbox.
0x80040234
Binding to self is not allowed.
0x80044000
The first argument is invalid.
0x80044001
The second argument is invalid.
0x80044002
The third argument is invalid.
0x80044003
The fourth argument is invalid.
0x80044004
The fifth argument is invalid.
0x800CCE05
The requested body part was not found in this message.
0x800CCE1D
The content encoding type is invalid.
Saludos