How to Send SMTP mail from Navision Application

2 Steps to use it -

1. Setup the SMTP mail in Navision.
2. Use the functions avaliable in Codeunit 400 (SMTP Mail)

1. Setup the SMTP mail in Navision:

Go to Administration> IT adminisration > General Setup > SMTP Setup. The following screen will open -


You need a SMTP server address (IP or link). I have used my office SMTP server. If your SMTP server permits anonymous mailing, you can choose authentication as 'Anonymous' where you don't need to mention any user id and password.

If your SMTP Server does not allow anonymous mailing, you need to choose authentication as 'Basic' and need to mention one valid User id and Password.




2. Use the functions available in Codeunit 400 (SMTP Mail) :

Once the setup is done, you can start mailing using the functions available in SMTP mail codeunit.

a) Take a new form and take a command button on it. On clicking of that button the mail will be send (Just for the example)
b) Take a global variable 'SMAIL' of data type 'Codeunit' and subtype 'SMTP mail' codeunit. This will give you the access to the functions available in that codeunit.

c) Now the actual code. Write the following code on 'On Push' trigger of the command button

--------------------------------------------------------------------
CLEAR(SMAIL);
SMAIL.CreateMessage('Snehanshu','snehanshu.mandal@gmail.com','snehanshu.mandal@gmail.com','Warning - Inventory', 'Item 50005 has a inventory below minimum inventory level',FALSE);
[Syntax for the Createmessage function - Createmessage([sender's name] ,[sender's id],[recipient's id or ids],[subject] ,[body line] ,[html formatted or not]]
SMAIL.AddAttachment('d:\Sales Invoice.xlsx');
SMAIL.Send;
MESSAGE('mail send');
-----------------------------------------------------------------------------------

That's it. Now click on the command button, and the mail will be sent to the recipients.

Note:

1. There can be multiple recipients. Just put them as comma separated text in the recipient parameter.
2. You can add more body lines using 'AppendBody' function.
3. You can also mark a CC or BCC copy of the mail using 'AddCC' 'AddBCC' functions.


If you are not sure about the SMTP server, just search in google how to use, rediffmil, Gmail or Hotmail or any other mailing system as your smtp server.

8 comments:

boby said...

2 ways we can use smtp mail in nav
1) In administration setup we can configure smtp mail and this can be used as a default mail and is used for outlook integration
2) Name DataType Subtype Length
Mail Codeunit Mail
If the out look is configured with the smtp mail then
Mail.NewMessage( moulikaku@hotmail.com, CCName,Message1 , Message1, AttachFileName, FALSE );
then the mail will go to moulikau@hotmail.com from the system configured mail.

Anonymous said...

Bobby, what you have written is actually will use Outlook object model not smtp mail system.

What Snehanshu said is correct for sending SMTP mail.

Anonymous said...

Thx Snehanshu! I needed a little code example and you helped me out!

Greets from Austria

Unknown said...

hi..
if the mail is invalid then the exchange server will give an error message. even after that message, "Mail sent" will display...
how to solve this?

Anonymous said...

Dear All,
I have a solution for SMTP Server as smtp.gmail.com and it is working Successfully.
reach me at sami.3187@gmail.com

Regards,
Sami mohammed
Navision technical Consultant.
Microsoft Certified Professional.

Anonymous said...

Hello Export,
I followed ur instructions,but i am getting an error like "The SMTP mail system returned the following error:(0x80040213):The transport failed to connect to the server."
So what do to do...?
Thanks In Advance.
Shravan
NAV Developer
mr.srkb@gamil.com

Anonymous said...

Hi again,
this error is fixed.but got new one saying "The SMTP mail system returned The following error:(0x8004020E):The server rejected the sender address.The server responce was: 530 5.7.0 Must issue a STARTTLS command first. nv10sm3635088pbb.49"

What to do...?

Thanks in Advance
B.shravan
mr.srkb@gmail.com

Anonymous said...

Still no answer for the last query.