1. Daniel Seguin
  2. PowerBuilder
  3. Friday, 8 November 2024 05:38 AM UTC

Hello,

I have created an email.dll in C# to send email within a powerbuilder program.

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.Net.Mail;

namespace seguindev
{
public class Mail
{

public int SendEmail(string strFrom, string strTo, string strSubject, string strBody, string strHost, int intPort)
{

int li_status = 1;

MailMessage mail = new MailMessage();

mail.To.Add(strTo);
mail.From = new MailAddress(strFrom);
mail.Subject = strSubject;
mail.Body = strBody;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = strHost;
smtp.Port = intPort;

smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;

try
{
smtp.Send(mail);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}

return li_status;

}
}

}

Powerbuilder function calling the C# function:

// function of_afterSaveActivity
// parameter: current master row = ai_row

int li_status
nvo_mail mail
mail = create nvo_mail

li_status = mail.of_sendemail("daniel@seguin.dev","danieloseguin67@gmail.com","This is a test", "This is the body of the email","smtp.gmail.com",587)

if li_status = 1 then
MessageBox("Message","Message was sent successfully")
else
MessageBox("Message","Message was NOT sent successfully")
end if 

 


//*-----------------------------------------------------------------*/
//* .NET function : SendEmail
//* Argument:
//* String as_strfrom
//* String as_strto
//* String as_strsubject
//* String as_strbody
//* String as_strhost
//* Long al_intport
//* Return : Long
//*-----------------------------------------------------------------*/
/* Store the Return value from dotnet function */
Long ll_result

/* Create .NET object */
If Not This.of_createOnDemand( ) Then
SetNull(ll_result)
Return ll_result
End If

/* Trigger the dotnet function */
ll_result = This.sendemail(as_strfrom,as_strto,as_strsubject,as_strbody,as_strhost,al_intport)
Return ll_result

 

 

Here is the import of dll:

 

 

Any ideas?

 

Thanks

 

Daniel

 

 

Miguel Leeuwe Accepted Answer Pending Moderation
  1. Sunday, 10 November 2024 18:17 PM UTC
  2. PowerBuilder
  3. # 1

Hi,

Google and MS are making it more difficult (secure?) to use their smtp services.

For example, Google has several changes this year. Now you have to setup 2FA in you account, then get a client id and a "secret". Both of them, you'll have to store them somewhere on your disk (bye bye security). With C# you can then get a "token" which expires all the time, but there's ways to auto renew it. It's a real PITA and you'd have to setup each and every user of your customers.

regards.

Comment
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 9 November 2024 20:11 PM UTC
  2. PowerBuilder
  3. # 2

My office 365 settings for my email allows

 

Comment
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 9 November 2024 20:04 PM UTC
  2. PowerBuilder
  3. # 3

When I use my office 365 email in the C# email class, now I get this message

Comment
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 9 November 2024 19:39 PM UTC
  2. PowerBuilder
  3. # 4

It appears that google email is not allowing 3rd party so send emails anymore 

Comment
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 9 November 2024 19:16 PM UTC
  2. PowerBuilder
  3. # 5

I revised my email class, and now getting the same time of error message 

The advantage of writting it in C# is that it gives you more flexibility to debug.

//*-----------------------------------------------------------------*/
//* .NET function : SendMail
//* Argument:
//* String as_strfromemail
//* String as_strtoemail
//* String as_strsubject
//* String as_strbody
//* Return : String
//*-----------------------------------------------------------------*/
/* Store the Return value from dotnet function */
String ls_result

/* Create .NET object */
If Not This.of_createOnDemand( ) Then
SetNull(ls_result)
Return ls_result
End If

/* Trigger the dotnet function */
ls_result = This.sendmail(as_strfromemail,as_strtoemail,as_strsubject,as_strbody)
Return ls_result

 

 

 

Comment
There are no comments made yet.
Daniel Seguin Accepted Answer Pending Moderation
  1. Saturday, 9 November 2024 19:05 PM UTC
  2. PowerBuilder
  3. # 6

Hello,

 

I have just tried to use the Powerbuilder native smtp client using this example from the documentation.

I just replaced the email and password to mine.

I am getting smtp error -12 = The remote server denied the Curl login.

Any ideas?

 

Integer li_rc
SMTPClient  lnv_SmtpClient

lnv_SmtpClient = CREATE SMTPClient

//set the email account information     
lnv_SmtpClient.Host = "smtp.gmail.com"
lnv_SmtpClient.Port = 587
lnv_SmtpClient.Username = "tester001.appeon@gmail.com"
lnv_SmtpClient.password = "Mypassword001"
lnv_SmtpClient.EnableTLS = True

//set the email message
lnv_SmtpClient.Message.SetSender("tester001.appeon@gmail.com","Tester001")
lnv_SmtpClient.Message.AddRecipient("tester002.appeon@gmail.com")
lnv_SmtpClient.Message.Subject = "SMTPClient Test Message"
lnv_SmtpClient.Message.TextBody = "SMTPClient example message body"

//send the email message
li_rc = lnv_SmtpClient.Send()

IF li_rc = 1 THEN
 Messagebox('SMTPClient','Mail sent successfully')
ELSE
 Messagebox('SMTPClient' ,'Email sending failed. Return ' + String(li_rc) + '.', StopSign!)
END IF

DESTROY lnv_SmtpClient
Comment
There are no comments made yet.
Francisco Martinez @Appeon Accepted Answer Pending Moderation
  1. Friday, 8 November 2024 18:30 PM UTC
  2. PowerBuilder
  3. # 7

Like Bruce suggested, it would be preferable if you used the native SMTPObject in PowerBuilder; but if you must, then I would suggest debugging the C# project by opening it and attaching it to PowerBuilder, then running the program. This will allow you to step through the C# code when it's invoked.

There's a couple of things to keep in mind for this:
1. The assembly must have been compiled with Debug configuration
2. You must attach the debugger for Managed Code (.NET Core)

This might find more information about the error.

Additionally, does the C# code itself work? Have you ran it in a .NET console application?

 

Regards,
Francisco

Comment
There are no comments made yet.
Bruce Armstrong Accepted Answer Pending Moderation
  1. Friday, 8 November 2024 17:26 PM UTC
  2. PowerBuilder
  3. # 8

A few things:

1.  Why not use the native PowerScript SmtpClient object in the newer versions of PowerBuilder?  https://docs.appeon.com/pb/whats_new/Native_email_support_SMTP_Client.html

2.  You might change the return type to a string and return the text of any exception that occurs.  That would help during debugging.

3.  You haven't set any credentials (username/password or OAUTH).  Google is not going to allow you to send email without that.  It's most likely you're getting an authentication exception from Google.

4.  I have an example of sending email using a C# assembly at: https://community.appeon.com/index.php/codeexchange/powerbuilder/256-net-importer-demo#274

However, I would really recommend going with #1.

 

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.