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)

Mail sending through SMTPClient Dynamics AX through X++ code

(0) ShareShare
ReportReport
Posted on by

I am trying to write a job for sending mail through SMTPClient. I am using gmail server and I am using credentials of my gmail account that gmail gives us for sending email through a program. 

I am using these credentials in my dotNet Application and they are working fine. email is being sent.

BUT

Now I am working in X++ and have created a job for sending email and i am using same credentials. But I am getting an error on the line  with code:

mailClient.Send(mailMessage);

the error is: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. v2sm17359467wmd.24 - gsmtp

What is the issue and what is the possible solution ??

my complete code for the job is as following:

static void EmailThroughSMTPExperiment(Args _args)
{
// Set these variables.
str sender = '2011cs163@gmail.com';
str recipient = 'muhammad.g@coh.com';
str cc = 'muhammad.gulfam@coh.com';
str subject = 'Test';
str body = 'Testing';
str fileName = @'C:\test.txt';
str pwd;

List toList;
List ccList;
ListEnumerator le;

Set permissionSet;
System.Exception e;

str mailServer;
int mailServerPort;
System.Net.Mail.SmtpClient mailClient;
System.Net.Mail.MailMessage mailMessage;
System.Net.Mail.MailAddress mailFrom;
System.Net.Mail.MailAddress mailTo;
System.Net.Mail.MailAddressCollection mailToCollection;
System.Net.Mail.MailAddressCollection mailCCCollection;
System.Net.Mail.AttachmentCollection mailAttachementCollection;
System.Net.Mail.Attachment mailAttachment;

SysEmailParameters parameters = SysEmailParameters::find();
;

try
{
toList = strSplit(recipient, ';');
ccList = strSplit(cc, ';');

permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(filename, 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);

mailServer = SysEmaiLParameters::find(false).SMTPRelayServerName;
mailServerPort = SysEmaiLParameters::find(false).SMTPPortNumber;
mailClient = new System.Net.Mail.SmtpClient(mailServer, mailServerPort);

le = toList.getEnumerator();
le.moveNext();

mailFrom = new System.Net.Mail.MailAddress(sender);
mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current())));
mailMessage = new System.Net.Mail.MailMessage(mailFrom, mailTo);

mailToCollection = mailMessage.get_To();
while (le.moveNext())
{
mailToCollection.Add(strLTrim(strRTrim(le.current())));
}

le = ccList.getEnumerator();
mailCCCollection = mailMessage.get_CC();
while (le.moveNext())
{
mailCCCollection.Add(strLTrim(strRTrim(le.current())));
}

mailMessage.set_Priority(System.Net.Mail.MailPriority::High);
mailMessage.set_Subject(subject);
mailMessage.set_Body(body);

mailAttachementCollection = mailMessage.get_Attachments();
mailAttachment = new System.Net.Mail.Attachment(fileName);
mailAttachementCollection.Add(mailAttachment);
//mailClient.set_EnableSsl=true;
pwd=SysEmaiLParameters::password();
mailClient.set_Credentials(New System.Net.NetworkCredential(parameters.SMTPUserName, pwd));

mailClient.Send(mailMessage);
mailMessage.Dispose();

CodeAccessPermission::revertAssert();

info("Email sent.");
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();
while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
}

}

*This post is locked for comments

I have the same question (0)
  • Iulian Cordobin Profile Picture
    8,201 on at

    From what I know the connection should use SSL, and I can see from your code that you have commented out that line. Why? Can you try with that one on?

  • Verified answer
    Community Member Profile Picture
    on at

    I tried with that line un-commented too but it did not worked. by the way problem has been solved.

    Thanx for your reply.

    following code worked for me:

    System.Net.Mail.SmtpClient          mailClient;

       System.Net.NetworkCredential        credentials;

       System.Net.Mail.MailMessage         mailMessage;

       Email email;

       SysEmailParameters parameters = SysEmailParameters::find();

       str pwd;

       ;

           email ='muhammad.gh@ciz.com';

           if (email != "")

           {

               mailClient = new System.Net.Mail.SmtpClient();

               mailClient.set_DeliveryMethod(System.Net.Mail.SmtpDeliveryMethod::Network);

               mailClient.set_Host(parameters.SMTPRelayServerName);

               mailClient.set_Port(SysEmaiLParameters::find(false).SMTPPortNumber);

               mailClient.set_EnableSsl(true);

               pwd=SysEmaiLParameters::password();

               credentials = new System.Net.NetworkCredential(parameters.SMTPUserName, pwd);

               mailClient.set_UseDefaultCredentials(false);

               mailClient.set_Credentials(credentials);

               mailClient.set_Timeout(30000000);

               mailMessage = new System.Net.Mail.MailMessage("noreply@gggg.com", email);

               mailMessage.set_Subject("Test Subject");

               mailMessage.set_Body("<b> Test Email</b><br/>something for test");

               mailMessage.set_IsBodyHtml(true);

               try

               {

                   mailClient.Send(mailMessage);

               }

               catch (Exception::CLRError)

               {

                   info("@MTu824" + AifUtil::getClrErrorMessage());

               }

       }

  • Iulian Cordobin Profile Picture
    8,201 on at

    Excellent code example. Don't forget to mark your answer as Verified so it will help others when they will need the same.

  • Brandon Wiese Profile Picture
    17,788 on at

    So what solved the problem?  Did your e-mail administrator remove the requirement for encryption, or remove the requirement for authentication, or what?

  • Iulian Cordobin Profile Picture
    8,201 on at

    I believe it is a mix of adding this line (I know this property since I also had to use it recently in a local SMTP usage couple of months ago)

            mailClient.set_DeliveryMethod(System.Net.Mail.SmtpDeliveryMethod::Network);

    combined with

            mailClient.set_EnableSsl(true);

    but this is just a guess and will let Muhammad explain.

  • badduke Profile Picture
    259 on at

    Nice example

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