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
Refer the below links:
community.dynamics.com/.../661343
http://dotnet-posts.blogspot.in/2015/02/marshalling-net-c-types-to-ax2012-xx.html
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++.
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.
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.
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 ?
Regards.
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,
The following link provides you detailed comparison of List class usage in X++ & C#.
msdn.microsoft.com/.../cc967436.aspx
Try it out!!
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156