É possível enviar um e-mail que é construído a partir de uma mensagem MIME.
É possível transmitir uma mensagem MIME ao nó EmailOutput, que usa o analisador MIME para gravar a mensagem MIME em um fluxo de bits. Esta mensagem é, então, enviada para a lista de destinatários no EmailOutputHeader. As substituições do ambiente local não são consideradas quando uma mensagem MIME é transmitida. Não é necessário configurar o nó EmailOutput nos exemplos a seguir.
Os exemplos a seguir mostram como configurar as informações de destinatário, de emissor, de assunto, do servidor SMTP e do corpo da mensagem em ESQL (com um nó Compute) e Java™ (com um nó JavaCompute).
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
CALL CopyMessageHeaders();
-- Incluir informações do destinatário em EmailOutputHeader
SET OutputRoot.EmailOutputHeader.To = '<endereço de e-mail do destinatário>';
SET OutputRoot.EmailOutputHeader.Cc = '<endereço de e-mail do destinatário>';
SET OutputRoot.EmailOutputHeader.Bcc = '<endereço de e-mail do destinatário>';
-- Incluir informações do remetente em EmailOutputHeader
SET OutputRoot.EmailOutputHeader.From = '<endereço de e-mail do remetente>';
SET OutputRoot.EmailOutputHeader."Reply-To" = '<endereço de e-mail de resposta>';
-- Incluir assunto em EmailOutputHeader
SET OutputRoot.EmailOutputHeader.Subject = 'Dynamic MIME message in ESQL.';
-- Incluir informações do servidor SMTP no LocalEnvironment
SET OutputLocalEnvironment.Destination.Email.SMTPServer ='<smtp.server:port>';
-- Criar um novo corpo de mensagem MIME, que será enviado como texto principal do e-mail,
-- incluindo um anexo.
CREATE FIELD OutputRoot.MIME TYPE Name;
DECLARE M REFERENCE TO OutputRoot.MIME;
-- Criar o filho Content-Type de MIME explicitamente para assegurar a ordem correta. Se configurarmos
-- a propriedade ContentType em vez disso, o campo poderá aparecer como o último filho de MIME.
CREATE FIELD M."Content-Type" TYPE NameValue VALUE 'multipart/related; boundary=myBoundary';
CREATE FIELD M."Content-ID" TYPE NameValue VALUE 'new MIME document';
CREATE LASTCHILD OF M TYPE Name NAME 'Parts';
CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part';
DECLARE P1 REFERENCE TO M.Parts.Part;
-- Primeira parte:
-- Criar o corpo do e-mail.
-- O corpo do e-mail tem o texto 'This is the main body of the email.'.
CREATE FIELD P1."Content-Type" TYPE NameValue VALUE 'text/plain; charset=us-ascii';
CREATE FIELD P1."Content-Transfer-Encoding" TYPE NameValue VALUE '8bit';
CREATE LASTCHILD OF P1 TYPE Name NAME 'Data';
CREATE LASTCHILD OF P1.Data DOMAIN('BLOB') PARSE(CAST('This is the main body of the email.'
AS BLOB CCSID 1208));
CREATE LASTCHILD OF M.Parts TYPE Name NAME 'Part';
DECLARE P2 REFERENCE TO M.Parts.Part[2];
-- Segunda parte:
-- Criar o anexo de um e-mail.
-- O anexo é chamado de 'attachment.txt' e contém o texto 'This is an attachment.'.
CREATE FIELD P2."Content-Type" TYPE NameValue VALUE 'text/plain; charset=us-ascii; name=attachment.txt';
CREATE FIELD P2."Content-Transfer-Encoding" TYPE NameValue VALUE '8bit';
CREATE LASTCHILD OF P2 TYPE Name NAME 'Data';
CREATE LASTCHILD OF P2.Data DOMAIN('BLOB') PARSE(CAST('This is an attachment.' AS BLOB CCSID 1208));
RETURN TRUE;
END;
public void evaluate(MbMessageAssembly assembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal fail = getOutputTerminal("fail");
// Criar uma nova montagem para propagar fora deste nó, por desejamos
// atualizá-lo
MbMessage outMessage = new MbMessage();
copyMessageHeaders(assembly.getMessage(), outMessage);
MbMessage outLocalEnv = new MbMessage(assembly.getLocalEnvironment());
MbMessage outExceptionList = new MbMessage(assembly.getExceptionList());
MbMessageAssembly outAssembly = new MbMessageAssembly(assembly, outLocalEnv, outExceptionList, outMessage);
MbElement localEnv = outAssembly.getLocalEnvironment().getRootElement();
// Criar o analisador EmailOutputHeader. Este é o local onde incluímos as informações
// de destinatário, emissor e assunto.
MbElement root = outMessage.getRootElement();
MbElement SMTPOutput = root.createElementAsLastChild("EmailOutputHeader");
// Incluir informações do destinatário em EmailOutputHeader
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Para", "<endereço de e-mail do destinatário>");
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Cc", "<endereço de e-mail do destinatário>");
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Bcc", "<endereço de e-mail do destinatário>");
// Incluir informações do remetente em EmailOutputHeader
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "De", "<endereço de e-mail do remetente>");
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Responder para", "<endereço de e-mail de resposta>");
// Incluir informações do assunto em EmailOutputHeader
SMTPOutput.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Subject", "Dynamic MIME message in Java.");
// Criar Destination.Email. Este é o local onde incluiremos informações do servidor SMTP.
MbElement Destination = localEnv.createElementAsLastChild(MbElement.TYPE_NAME, "Destino", null);
MbElement destinationEmail = Destination.createElementAsLastChild(MbElement.TYPE_NAME, "E-mail", null);
destinationEmail.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "SMTPServer", "<smtp.server:port>");
// Configurar último filho da raiz (corpo da mensagem) como MIME.
MbElement MIME = root.createElementAsLastChild("MIME");
// Criar o filho Content-Type de MIME explicitamente para assegurar a ordem correta.
MIME.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "multipart/related;
boundary=myBoundary");
MIME.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-ID", "new MIME document");
MbElement parts = MIME.createElementAsLastChild(MbElement.TYPE_NAME, "Parts", null);
MbElement part, data, blob;
String text;
// Primeira parte:
// Criar o corpo do e-mail.
// O corpo do e-mail tem o texto 'This is the main body of the email.'.
part = parts.createElementAsLastChild(MbElement.TYPE_NAME, "Part", null);
part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "text/plain; charset=us-ascii");
part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Transfer-Encoding", "8bit");
data = part.createElementAsLastChild(MbElement.TYPE_NAME, "Data", null);
blob = data.createElementAsLastChild("BLOB");
text = "This is the main body of the email.";
try {
blob.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", text.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
fail.propagate(outAssembly);
}
// Segunda parte:
// Criar o anexo de um e-mail.
// O anexo é chamado 'attachment.txt' e contém o texto 'This is an attachment.'.
part = parts.createElementAsLastChild(MbElement.TYPE_NAME, "Part", null);
part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Type", "text/plain; charset=us-ascii;
name=attachment.txt");
part.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Content-Transfer-Encoding", "8bit");
data = part.createElementAsLastChild(MbElement.TYPE_NAME, "Data", null);
blob = data.createElementAsLastChild("BLOB");
text = "This is an attachment.";
try {
blob.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", text.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
fail.propagate(outAssembly);
}
outMessage.finalizeMessage(MbMessage.FINALIZE_VALIDATE);
out.propagate(outAssembly);
}