Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

Send email with X++ and a free SMTP server

(0) ShareShare
ReportReport
Posted on by

Has anyone had any success with using a free SMTP server like smtp.gmail.com or smtp.live.com to send email in Dynamics AX? 

I have a normal (not business) gmail account that I enter into the email parameters form. I use smtp.gmail.com and tried ports 587 and 465. I’ve tried with and without NTLM. We've also tried Hotmail and outlook email accounts. 

We have tried this in AX 4 and AX 2012. We run a job like the ones below; the job runs, gives no error, but no email is sent. We've checked spam folders and tried different addresses as receivers. We've pinged the smtp servers and that works from the aos server and client. 

My guess is that it does not work because there is no option to enable TLS, which both the live and gmail servers require. Is there a way to overcome this? 

 

Are we missing anything obvious? Has anyone every used a gmail/Live smtp to send email from AX?

 

-----------------------------------------------------------------------------------------------------------------------

 I don't think the problem is with the code, but I'll add it anyway. We've tried a number of ways to send email via X++, here are two of the main ones:

 

SysMailer mailer = new SysMailer();

SysEmailParameters parameters = SysEmailParameters::find();

;

if (parameters.SMTPRelayServerName)

{

mailer.SMTPRelayServer(parameters.SMTPRelayServerName,

parameters.SMTPPortNumber,

parameters.SMTPUserName,

SysEmailParameters::password(),

parameters.NTLM);

}

else

{

mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,

parameters.SMTPPortNumber,

parameters.SMTPUserName,

SysEmailParameters::password(),

parameters.NTLM);

}

 

mailer.fromAddress("someoneElse@gmail.com");

mailer.tos().appendAddress("someone@gmail.com");

mailer.body("Hallo");

mailer.sendMail();

 

We have also tried System.Net.Mail:

System.Net.Mail.MailMessage mailMessage;

System.Net.Mail.SmtpClient smtpClient;

System.Net.Mail.MailAddress mailFrom;

System.Net.Mail.MailAddress mailTo;

str smtpServer;

;

mailFrom = new System.Net.Mail.MailAddress('sender@contoso.com',"Sender name");

mailTo = new System.Net.Mail.MailAddress('recipient@contoso.com',"Recipient name");

smtpServer = SysEmaiLParameters::find(false).SMTPRelayServerName;

mailMessage = new System.Net.Mail.MailMessage(mailFrom,mailTo);

mailmessage.set_Subject('This is email subject');

mailmessage.set_Body('This is email body');

 

smtpClient = new System.Net.Mail.SmtpClient(smtpServer);

smtpClient.Send(mailmessage);

mailMessage.Dispose();


 

*This post is locked for comments

  • Vinod321 Profile Picture
    Vinod321 15 on at
    RE: Send email with X++ and a free SMTP server

    Hi Tina,

    did you get resolved this query. if you resolved it. so please let me know. I also want to do the same task but unable to do that.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Send email with X++ and a free SMTP server
    The following code can be used to send an email using SMTP using those services.  The str receiverMailAddress should be a string of email addresses separated by semi-colons(;).


    public void new(str mailSubject, str mailBody, str receiverMailAddress, str _attachment) { List toList; List ccList; ListEnumerator le; System.Net.Mail.AttachmentCollection mailAttachementCollection; System.Net.Mail.Attachment mailAttachment; System.Net.Mail.MailMessage mailMessage; System.Net.Mail.SmtpClient myMail; System.Net.Mail.MailAddressCollection mailToCollection; System.Net.Mail.MailAddress mailFrom; System.Net.Mail.MailAddress mailTo; System.Net.Mail.MailAddress mailCC; str smtpServer; str CcMailAddress; int SMTPPort; #File str mail; userinfo userInfo; str pwd; SysEmailParameters parameters = SysEmailParameters::find(); ; new InteropPermission(InteropKind::ClrInterop).assert(); mailFrom = new System.Net.Mail.MailAddress(parameters.SMTPUserName ,"Name"); toList = strSplit(receiverMailAddress, ';'); le = toList.getEnumerator(); le.moveNext(); mailTo = new System.Net.Mail.MailAddress(strLTrim(strRTrim(le.current()))); try { smtpServer = SysEmaiLParameters::find(false).SMTPRelayServerName;// using the SMTP server ip //setup in email Parameters mailMessage = new System.Net.Mail.MailMessage(mailFrom,mailTo); mailmessage.set_Subject(mailSubject); mailmessage.set_Body(mailBody); mailToCollection = mailMessage.get_To(); while (le.moveNext()) { mailToCollection.Add(strLTrim(strRTrim(le.current()))); } if (_attachment != "") { mailAttachementCollection = mailMessage.get_Attachments(); mailAttachment = new System.Net.Mail.Attachment(_attachment); mailAttachementCollection.Add(mailAttachment); } SMTPPort = SysEmaiLParameters::find(false).SMTPPortNumber; myMail = new System.Net.Mail.SmtpClient(smtpServer, SMTPPort); myMail.set_EnableSsl(true); pwd = SysEmaiLParameters::password(); mymail.set_Credentials(New System.Net.NetworkCredential(parameters.SMTPUserName, pwd)); mymail.Send(mailmessage); } catch(Exception::CLRError) { throw Exception::CLRError; } mailMessage.Dispose(); CodeAccessPermission::revertAssert(); }

  • Suggested answer
    dolee Profile Picture
    dolee 11,279 on at
    RE: Send email with X++ and a free SMTP server

    I had to use hMailServer (open source) as a middle man between AX and Gmail.

    You can try it out here (https://www.hmailserver.com/)

  • Suggested answer
    Jonathan  Halland Profile Picture
    Jonathan Halland 11,306 on at
    RE: Send email with X++ and a free SMTP server

    Hi.

    We also had this requirement, but after looking at the internal support in AX, we decided to make use of a third party component to connect to SMTP servers that require intensive attachments or alternative authentication methods. LimiLabs mail.dll client is very good, but I'm sure there are others out there too.

  • PA-22040759-0 Profile Picture
    PA-22040759-0 6,194 on at
    RE: Send email with X++ and a free SMTP server

    I know this is not an answer to your question, but I just want to point you to an alternative service: www.agermark.com/.../send-mail-with-smtp-through-cloud.html.

    This service is free for up to 200 mails per day.

    At least you could try it out as a way of testing if your firewall accepts SMTP relay.

  • Sasha Nazarov Profile Picture
    Sasha Nazarov 901 on at
    RE: Send email with X++ and a free SMTP server

    Hi Tina

    Consider adding a try/catch for CLR-exception type, this might reveal what happens with your email.

    Also, there may be some firewall rules in your domain that don't allow for external SMTP relay.

    Finally, I heard Gmail would require some confirmation procedure that would make sending from a particular PC allowed. I am not sure about details, though.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,476 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans