From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

smtp authentication

Solved!
Go to solution

Hello everybody,

 

i'm trying to create an application to send e-mails with LabWindows/cvi 10.0.

 

but i'm having problem with smtp server, because it needs authentication.

is there any function that allow me to do the authentication???

 

THANKS for help.

 

 

0 Kudos
Message 1 of 24
(8,865 Views)

Currently, the CVI SMTP library does not support SMTP authentication. You will have to use a third party library if you need authentication. A CVI Idea Exchange topic already exists for this; please add a Kudo to this so that we can track the interest level of adding this feature.

 

http://ni.lithium.com/t5/LabWindows-CVI-Idea-Exchange/Email-with-user-authentication/idi-p/1449810

National Instruments
Message 2 of 24
(8,858 Views)

Hi,

 

the better link seems to be http://forums.ni.com/t5/LabWindows-CVI-Idea-Exchange/Email-with-user-authentication/idi-p/1449810 because the Lithium version does not recognize me as registered user while the NI version does...

0 Kudos
Message 3 of 24
(8,854 Views)

You can do this by using the .NET SmtpClient class from CVI. You must first generate a .NET controller for the System assembly and then you can use the following code.

 

#include "system.h" /* header for CVI wrapper of .NET System assembly */

#include <toolbox.h>

 

int main(int argc, char *argv[]) {

  int error;

  System_Net_NetworkCredential credentials = NULL;

  System_Net_Mail_SmtpClient smtpClient = NULL;

 

  if (argc != 4) {

    puts("USAGE: smtp-auth <user-name> <password> <to-address>");

    return -1;

  }

 

  errChk(Initialize_System());

 

  errChk(System_Net_Mail_SmtpClient__Create_2(&smtpClient, "smtp.gmail.com", 587, NULL));

  errChk(System_Net_Mail_SmtpClient_Set_EnableSsl(smtpClient, 1, NULL));

  errChk(System_Net_Mail_SmtpClient_Set_DeliveryMethod(smtpClient, 0x0, NULL));

  errChk(System_Net_Mail_SmtpClient_Set_Timeout(smtpClient, 5000, NULL));

  errChk(System_Net_NetworkCredential__Create(&credentials, argv[1], argv[2], NULL));

  errChk(System_Net_Mail_SmtpClient_Set_Credentials(smtpClient,

    (System_Net_ICredentialsByHost)credentials, NULL));

  errChk(System_Net_Mail_SmtpClient_Send(smtpClient, argv[1], argv[3], "Test email",

    "Hi,\nThis is a test mail.\n", NULL));

 

Error:

  if (credentials)

    CDotNetDiscardHandle(credentials);

  if (smtpClient)

    CDotNetDiscardHandle(smtpClient);

  Close_System();

  return 0;

}

Message 4 of 24
(8,836 Views)

hello mohan,

 

i tried your code but it gives me some errors:

 

Type error in argument 2 to `System_Net_NetworkCredential__Create'; found 'pointer to char' expected 'pointer to CDotNetHandle'.

Too many arguments to `System_Net_NetworkCredential__Create'.

Too many arguments to `System_Net_NetworkCredential__Create'.

 

0 Kudos
Message 5 of 24
(8,830 Views)

I think that the System_Net_NetworkCredential__Create wrapper is a different overload in your CVI wrapper. Find the System_Net_NetworkCredential__Create_N (N is a number 1, 2, ...) in your system.fp wrapper that has the signature: System_Net_NetworkCredential__Create_N(System_Net_NetworkCredential *__instance, char *userName, char *password, CDotNetHandle *exception) and call that instead of System_Net_NetworkCredential__Create.

0 Kudos
Message 6 of 24
(8,825 Views)

hello Mohan,

 

now i'm not having any error,

 but i can't send the mail, may be i put some wrong parameters.

 

my smtp server is: authsmtp.copromec.it

 

here is what i have done:

 

errChk(Initialize_System()); 

 

errChk(System_Net_Mail_SmtpClient__Create_2(&smtpClient,"authsmtp.copromec.it", 25, NULL)); 

 

 errChk(System_Net_Mail_SmtpClient_Set_EnableSsl(smtpClient, 1, NULL));  

 

 errChk(System_Net_Mail_SmtpClient_Set_DeliveryMethod(smtpClient, 0x0, NULL)); 

 

 errChk(System_Net_Mail_SmtpClient_Set_Timeout(smtpClient, 5000, NULL)); 

 

 errChk(System_Net_NetworkCredential__Create_1(&credentials, user, pass, NULL)); 

 

errChk(System_Net_Mail_SmtpClient_Set_Credentials(smtpClient,(System_Net_ICredentialsByHost)credentials, NULL)); 

 

errChk(System_Net_Mail_SmtpClient_Send(smtpClient,from, to, "Test email", "Hi,\nThis is a test mail.\n", NULL));

 

0 Kudos
Message 7 of 24
(8,818 Views)

This is probably because of incorrect parameters you are sending to your smtp server. One thing I notice is that you are using port 25 - typically this is the default port used by smtp servers that do NOT use authentication. Because you are using authentication you should check your server's documentation or IT department to see which port, etc to use. Also, you should find out what kind of authentication your smtp server support and see if the Microsoft .NET SmtpClient class supports that - you can find more information about the SmtpClient class at the following Microsoft link:

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

 

I used gmail in my example as it is the most common authenticated SMTP server. Finally, it is also possible that something on your computer is blocking the program from connecting to your smtp server. This could be a firewall, virus scanner, etc. You may want to check this too. First try to send email using the gmail server and if that works, then try with your own server.

Message 8 of 24
(8,806 Views)

IT WORKS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

i put "0" in  System_Net_Mail_SmtpClient_Set_EnableSsl(smtpClient, 0, NULL), and now works.

 

last question:

 

how can i send attachments??

 

this is the last thing that i need.

0 Kudos
Message 9 of 24
(8,798 Views)

To send attachments you need to use a different overload of the Send method - one that takes a MailMessage. You need to first create the MailMessage object and an Attachment object and add the Attachment object to the MailMessage object's Attachments collection. Please refer to the Microsoft documentation about System.Net.Mail namespace for more information. The code using CVI wrapper is:

 

System_Net_Mail_Attachment attachment = NULL;

System_Net_Mail_AttachmentCollection attachments = NULL;

System_Net_Mail_MailMessage message = NULL;

 

/* other code */

 

errChk(System_Net_Mail_MailMessage__Create_2(&message, argv[1], argv[3],   "Mail subject", "Mail body", NULL));

errChk(System_Net_Mail_MailMessage_Get_Attachments(message, &attachments, NULL));

errChk(System_Net_Mail_Attachment__Create(&attachment, "c:\\temp\\temp.txt", NULL));

errChk(System_Net_Mail_AttachmentCollection_Add(attachments, attachment, NULL));

errChk(System_Net_Mail_SmtpClient_Send_1(smtpClient, message, NULL));

 

0 Kudos
Message 10 of 24
(8,791 Views)