Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

use list<string> in X++

Posted on by

Hi All

I need to Sent Mails from Gmail

So I created Project in Visual studio using C# to sent Mails and i test it in Console Application and Working Correctly.

the Code of C# working Fine but when i used it in AX 

It give me an error of incompatible Argument because i define email attachments as List<String>

but in AX i use List.

this is C# Code : 

using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Collections;
using System.Net.Mime;

namespace SendMail
{
    public class SendMail
    {
        public string SendMessage(string recipientsEmail, string ccEmail, string emailSubject, string emailBody, List<String> emailAttachments)
        {
            try
            {
                //For Sending to more than one recipients
                string to = recipientsEmail;
                string[] ToMultiple = to.Split(';');

                //For Sending to more than one CC
                string CC = ccEmail;
                string[] CCMultiple = CC.Split(';');

                String ErrorMessage = "";

                //Connect to SMTP of Gmail
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                //Email Details
                mail.From = new MailAddress("Mail Account");

                //Sending To Addresses
                foreach (var substring in ToMultiple)
                {
                    bool Totest = IsValidEmail(substring);
                    if (Totest == true)
                    {
                        mail.To.Add(substring);
                    }
                    else
                    {
                        ErrorMessage = "Invalid Email : " + substring + "\n";
                    }
                }

                //CC Mail Details
                if (!string.IsNullOrEmpty(ccEmail))
                {
                    foreach (var ccString in CCMultiple)
                    {
                        bool CCtest = IsValidEmail(ccString);
                        if (CCtest == true)
                        {
                            mail.CC.Add(ccString);
                        }
                        else
                        {
                            ErrorMessage = "Invalid Email : " + ccString + "\n";
                        }
                    }
                }

                //add attachments
                if (emailAttachments != null)
                {
                    foreach (string attach in emailAttachments)
                    {
                        if (File.Exists(attach))
                        {
                            Attachment attached = new Attachment(attach, MediaTypeNames.Application.Octet);
                            mail.Attachments.Add(attached);
                        }
                        else
                        {
                            ErrorMessage = "File : " + attach + "Not Exist. \n";
                            return ErrorMessage;
                        }
                    }
                }

                //Email Body and Subject
                mail.Subject = emailSubject;

                if (ErrorMessage == "")
                {
                    mail.Body = emailBody;
                }
                else
                {
                    mail.Body = emailBody + "\n The Mail Ended with the Following Errors : \n" + ErrorMessage;
                }

                //Connect to Port and Credentials
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("Account Mail", "Password");
                SmtpServer.EnableSsl = true;

                //Send Mail
                SmtpServer.Send(mail);
                return "";

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return "Error in Connecting to Gmail SMTP.";
            }
        }

        //Validate Email
        bool IsValidEmail(string email)
        {
            try
            {
                MailAddress m = new MailAddress(email);

                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
}

So how can i use list to Correct this Error?

Regards.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: use list<string> in X++

    Refer the below links:

    community.dynamics.com/.../661343

    http://dotnet-posts.blogspot.in/2015/02/marshalling-net-c-types-to-ax2012-xx.html

  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    RE: use list<string> in X++

    So you've changed your code and now you're using string instead of List<String> for the fifth parameter, right? Please show us your updated code.

    Reqarding your problem, look at what string you're sending there - I think you'll find that it's a complete nonsense, because List.toString() doesn't give you anything useful.

    Why exactly are you trying to use a string instead of the solutions I suggested? I thought you wanted a collection, which seems to confirmed by the fact that you're still using a List in X++.

  • Walid Gamal Profile Picture
    Walid Gamal on at
    RE: use list<string> in X++

    Hi Matrin 

    I use it as String in C# Code but as List in X++ Code 

    and when call my C# Code in AX i use this Function List.ToString but it didn't sent any mail When Testing

    public str dispatchEmailWithError()
    {
        str errorMessege = "";
    
        print("Email Dispatching Satrted. Subject:");
    
        SsmGmail.SendMessage(recipientsEmail,ccEmail,emailSubject,emailBody,emailAttachments.toString());
        info(emailAttachments.toString());
        print("Email Dispatched");
    
    
        return errorMessege;
    }


    How Can i achieve this ?

    Regards.

  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    RE: use list<string> in X++

    You forgot to mention the type of emailAttachments variable, but it's clearly something else than List<string>.

    List`1 means a List with a single generic parameter; it's just a different syntax for List<T>.

    Again, you can't directly declare generic types in X++, therefore you should stop using List<T> as a parameter.

  • Walid Gamal Profile Picture
    Walid Gamal on at
    RE: use list<string> in X++

    Hi All

    Another Note when i write my SsmGmail.SendMessage(

    It Say in Emailattachments -> List ' 1 emailattachments

    What is the meaning of that list ' 1 ?

    C_2300_-Error.png

    Regards.

  • Walid Gamal Profile Picture
    Walid Gamal on at
    RE: use list<string> in X++
    public str dispatchEmailWithError()
    
    {
    
       str errorMessege = "";
    
       print("Email Dispatching Satrted. Subject:");
    
      SsmGmail.SendMessage(recipientsEmail,ccEmail,emailSubject,emailBody,emailAttachments);
    
       print("Email Dispatched");
    
       return errorMessege;
    
    }

    the Error is :

    Argument '5' is incompatible with the required type. \Classes\CustomEmailDispatcher\dispatchEmailWithError 7 dispatchEmailWithError Err:11

    Regards.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: use list<string> in X++

    Walid,

    The following link provides you detailed comparison of List class usage in X++ & C#.

    msdn.microsoft.com/.../cc967436.aspx

    Try it out!!

  • Martin Dráb Profile Picture
    Martin Dráb 230,149 Most Valuable Professional on at
    RE: use list<string> in X++

    Please show us the code you have problem with, which is your X++ code, not the C# code above.

    Nevertheless note that X++ in AX 2012 has no support for generic types, therefore if you really must use them, it's a bit tricky. A better approach is using non-generic types; for example, use a StringCollection or an array or strings.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans