web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

use list<string> in X++

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    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.

  • Community Member Profile Picture
    on at

    Walid,

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

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

    Try it out!!

  • Walid Gamal Profile Picture
    on at
    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.

  • Walid Gamal Profile Picture
    on at

    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.

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    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
    on at

    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
    237,976 Most Valuable Professional on at

    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++.

  • Community Member Profile Picture
    on at

    Refer the below links:

    community.dynamics.com/.../661343

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans