Send email using .Net framework

This code will send email using .Net Framework.

Create email template: 

    Goto : Organization adminstration > Setup > E-mail templates.

Create E-mail template give E-mail ID, E-mail description, Default language, Sender name, Sender e-mail.

set the email body of above-created record.

Goto: E-mail message and set e-mail body as below.









static void SendEmployeesDataInEmail(Args _args)

{

    System.Net.Mail.MailMessage    mailMessage;

    System.Net.Mail.SmtpClient     smtpClient;

    SystemParameters               systemParameters;

    Email                          emailFrom, emailTo;


    SysEmailMessageTable           message;

    SysEmailId                     sysEmailId;


    EmployeeTable                  employeeTable;


    Map                            mappings;

    str                            subject, smtpServer, messageBody;


    systemParameters = SystemParameters::find();


    // Email template Id

    sysEmailId = 'Employees';         gfdgf

                                

    // Sender email address

    emailFrom = SysEmailTable::find(sysEmailId).SenderAddr;

    emailTo   = 'emplyee@outlook.com';


    mappings = new Map(Types::String, Types::String);


    // Table headers

    // this will replace variable with actual value.

    mappings.insert('empid',    'Employee Id');

    mappings.insert('empname',  'Employee Name');

    mappings.insert('location', 'Location');

    mappings.insert('email',    'Email Address');


    // Return SysEmailMessageTable e.g Email subject, email or template body

    message = SysEmailMessageTable::find(sysEmailId, SysEmailTable::find(sysEmailId).DefaultLanguage);


    // messageBody is a string type, message.Mail = Email template or body

    messageBody = SysEmailMessage::stringExpand(message.Mail , SysEmailTable::htmlEncodeParameters(mappings));


    while select EmpId, EmployeeName, Location, Email from employeeTable

    {

        // this will replace variable with actual value.

        mappings.insert('empid',    employeeTable.EmpId);

        mappings.insert('empname',  employeeTable.EmployeeName);

        mappings.insert('location', employeeTable.Location);

        mappings.insert('email',    employeeTable.Email);


        // Old body is concatinated with the new body, eg. Table header + first row or record of the table

        messageBody = messageBody + SysEmailMessage::stringExpand(message.Mail , SysEmailTable::htmlEncodeParameters(mappings));

    }


    smtpServer = SysEmaiLParameters::find(false).SMTPRelayServerName;

    mailMessage = new System.Net.Mail.MailMessage(emailFrom,emailTo);


    mailMessage.set_Subject(message.Subject);


    // Email body

    mailMessage.set_Body(messageBody);

    mailMessage.set_IsBodyHtml(true);


    smtpClient = new System.Net.Mail.SmtpClient(smtpServer);

    smtpClient.Send(mailmessage);

    mailMessage.Dispose();


    info('Email sent');

}

Comments

Popular posts from this blog

Assembly reference containing type is not referenced, Object 'CLRObject' could not be created

Open Packing slip posting form when updating Generate picking list

Extract data of PO from the System in Ax 2012