Sending Email by System.Net.Mail in AX

  We could easyly to write a method to send the mail by C#, in axapta we can use the System.Net.Mail to send mail as C#.

  1 void sendMail
  2 (
  3     str _Subject,
  4     str _MailTo,
  5     str _CCTo,
  6     str _Body,
  7     str _Attachment
  8 )
  9 {
 10     System.Net.Mail.MailAddress                 mailFrom;
 11     System.Net.Mail.MailMessage                 mailer;
 12     System.Net.Mail.MailAddressCollection       mailerConn;
 13     System.Net.Mail.AttachmentCollection        mailerAttachment;
 14     System.Net.Mail.SmtpClient                  smtpClient;
 15 
 16     System.Exception                            ex;
 17 
 18     List              m_MailTo, m_CCTo, m_Attachment;
 19     ListEnumerator    m_LE;
 20     str               m_MailAddress, m_MailerAttachment;
 21    InteropPermission interopPermission;
 22    ;
 23 
 24    interopPermission = new InteropPermission(InteropKind::ClrInterop);
 25    interopPermission.assert();
 26     mailFrom = new System.Net.Mail.MailAddress("*********", "**********");//Sender address; display name
 27     mailer   = new System.Net.Mail.MailMessage();
 28 
 29     mailer.set_From(mailFrom);
 30     mailer.set_Subject(_Subject);
 31     mailer.set_Body(_Body);
 32 
 33     //Set mail to
 34     mailerConn = mailer.get_To();
 35     m_MailTo   = Global::strSplit(_MailTo, ";");
 36     m_LE       = m_MailTo.getEnumerator();
 37     while (m_LE.moveNext())
 38     {
 39         m_MailAddress = m_LE.current();
 40         mailerConn.Add(m_MailAddress);
 41     }
 42 
 43     //Set CC to
 44     mailerConn = mailer.get_CC();
 45     m_CCTo     = Global::strSplit(_CCTo, ";");
 46     m_LE       = m_CCTo.getEnumerator();
 47     while (m_LE.moveNext())
 48     {
 49         m_MailAddress = m_LE.current();
 50         mailerConn.Add(m_MailAddress);
 51     }
 52 
 53     //Set attachment
 54     mailerAttachment = mailer.get_Attachments();
 55     m_Attachment     = Global::strSplit(_Attachment, ";");
 56     m_LE             = m_Attachment.getEnumerator();
 57     while (m_LE.moveNext())
 58     {
 59         m_MailerAttachment = m_LE.current();
 60         mailerAttachment.Add(new System.Net.Mail.Attachment(m_MailerAttachment));
 61     }
 62 
 63     //Set sender parameters
 64     mailer.set_BodyEncoding(System.Text.Encoding::get_UTF8());
 65     mailer.set_IsBodyHtml(true);
 66     mailer.set_Priority(System.Net.Mail.MailPriority::Normal);
 67     mailer.set_DeliveryNotificationOptions(System.Net.Mail.DeliveryNotificationOptions::OnSuccess);
 68 
 69     try
 70     {
 71         //Set mailer client
 72         smtpClient = new System.Net.Mail.SmtpClient();
 73         smtpClient.set_Host("************************");//Mail server
 74         smtpClient.set_UseDefaultCredentials(false);
 75         smtpClient.set_Credentials(new System.Net.NetworkCredential("*****", "***********"));//User name, pass word
 76         smtpClient.set_DeliveryMethod(System.Net.Mail.SmtpDeliveryMethod::Network);
 77 
 78         //Send mail
 79         smtpClient.Send(mailer);
 80     }
 81     catch (Exception::CLRError)
 82     {
 83         ex = CLRInterop::getLastException();
 84         info(ex.ToString());
 85     }
 86     catch (Exception::CodeAccessSecurity)
 87     {
 88         info("Code Access Security Error");
 89     }
 90     catch (Exception::Internal)
 91     {
 92         ex = CLRInterop::getLastException();
 93         if (ex)
 94             info(ex.ToString());
 95         else
 96             info("Internal Error");
 97     }
 98     catch (Exception::Error)
 99     {
100         Error('Error exception!');
101     }
102     catch (Exception::Warning)
103     {
104         Warning("Warning exception!");
105     }
106 }

 

  

posted @ 2013-08-22 09:00  Sprite.z  Views(995)  Comments(0Edit  收藏  举报