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.