How to enables AX email functionality without Outlook

/*****************************************************************
  (C) Copyright DENTSPLY International. All rights reserved.
  The use, disclosure, reproduction, modification, transfer, or
  transmittal of this work for any purpose in any form or by any
  means without written permission of DENTSPLY International is
  strictly prohibited.

  Created Date: 10/Jun/2013
  Created By: Jimmy Xie[Tectura]  
  Helpdesk Ticket#: KP000115
  Description of Behavior: Enables AX email functionality without Outlook
  Expected Input: Request to generate email
  Expected Output: Email sent out of AX
******************************************************************/

void reportSendMail(PrintJobSettings p1)
{
    //Start Declaration
    //SysINetMail m = new SysINetMail();     // Commented out old AX code
    System.Net.Mail.MailMessage             mailMessage;
    System.Net.Mail.Attachment              attachment;
    System.Net.Mail.AttachmentCollection    attachementCollection;
    System.Net.Mail.SmtpClient              myMail;
    str                                     userMailAddress;
    str                                     receiverMailAddress;
    str                                     receiverCCMailAddress;
    str                                     mailBody;
    str                                     smtpServer;
    str                                     mail;
    fileNameOpen                            fileNameForEmail;
    FileIOPermission                        perm;
    userinfo                                userInfo;
    //end Declaration
    str                                     fileName = 'axaptareport';
    SysEmailMessageTable                    sysEmailMessageTable;
    ;

    if (p1.format() == PrintFormat::ASCII)
    {
        fileNameForEmail = subStr(p1.fileName(),strLen(p1.fileName())-3,-999) + 'TXT'; // NL
    }
    //fileName = fileName + '.txt'; // Commented out this line

    else if (p1.format() == PrintFormat::RTF)
    {
        fileNameForEmail = subStr(p1.fileName(), strLen(p1.fileName())-3, -999) + 'RTF';
    }
    //fileName = fileName + '.rtf';

    else if (p1.format() == PrintFormat::HTML)
    {
        fileNameForEmail = subStr(p1.fileName(), strLen(p1.fileName())-3, -999) + 'HTM';
    }
    //fileName = fileName + '.htm';
    //else if (p1.format() == PrintFormat::PDF) // Performance Testing : commentign this line and replacing the line below.
    else if (p1.format() == PrintFormat::PDF || p1.format() == PrintFormat::PDF_EMBED_FONTS)// Performance Testing :(replacing the above line) addign this line as it was present in the jsRemotecontroller project.. can be removedd later..
    {
        fileNameForEmail = subStr(p1.fileName(), strLen(p1.fileName())-3, -999) + 'PDF';
    }

    //fileName = fileName + '.pdf';
    //Start Logic
    mail = subStr(fileNameforEmail, (strlen(fileNameforEmail)-8), 9);

    select firstonly name from userInfo where userInfo.id == SysuserInfo::find().Id; // to find the user name

    fileNameforEmail = winApi::getTempPath() + mail; // store attachment in a temp location

    perm = new FileIOPermission(fileNameforEmail, 'w');

    if(!perm)
    {
        throw error("Cannot move attachment to temp location.");
    }

    try
    {
        perm.assert();
    }
    catch
    {
        throw error("Cannot gain access to Temp location.");
    }

    // find current users email address setup up in user //options
    userMailAddress         = SysUserInfo::find().Email;

    if(!info::validateEmail(userMailAddress))
    {
        throw error("Senders email is not valid");
    }

    receiverMailAddress     = p1.mailTo() + "," + p1.mailCc();
    receiverMailAddress     = strReplace(receiverMailAddress, ";" , ",");

    mailBody = SysEmailMessageTable::find("SysEmail", SysEmailTable::find("SysEmail").DefaultLanguage).Mail;

    // using the SMTP server ip //setup in email Parameters
    smtpServer              = SysEmaiLParameters::find(false).SMTPRelayServerName;

    try
    {
        mailMessage             = new System.Net.Mail.MailMessage(userMailAddress, receiverMailAddress);
    }
    catch(Exception::Internal)
    {
        infolog.clear(0);
        throw error("This Message was undeliverable due to the following reason:"
                    +"
"
                    +"Your message was not delivered because a destination address was
"
                    +"not found.  Carefully check that it was spelled correctly and try
"
                    +"sending it again if there were any mistakes.
"
                    +"
"
                    +"**When separating multiple email address please use either a comma or semicolon.**"
                   );
    }

    mailmessage.set_Subject(p1.mailSubject());
    mailmessage.set_Body(mailBody);

    mailmessage.set_IsBodyHtml(true);

    //move attachment file to Temp folder
    winapi::moveFile(p1.fileName(), fileNameforEmail);




    attachementCollection = mailMessage.get_Attachments();
    attachment = new System.Net.Mail.Attachment(fileNameforEmail);
    attachementCollection.Add(attachment);

    myMail = new System.Net.Mail.SmtpClient(smtpServer);
    mymail.Send(mailmessage);

    //Disopse COM objects
    attachment.Dispose();

    attachementCollection.Dispose();
    mailMessage.Dispose();

    //Delete the temp file
    winApi::deleteFile(fileNameforEmail);
    CodeAccessPermission::revertAssert();
    //end
}
原文地址:https://www.cnblogs.com/Fandyx/p/3200180.html