Sending Email

How to invoke a dotnet method and send email in uipath. without using inbuilt activities?

2 Likes

Fine that’s possible
But may I know the reason why we are neglecting the inbuilt activities
Where we facing any issue with them in sending or receiving mails

No worries
We can use this in Invoke code method but I would recommend to go for the inbuilt activity

Cheers @vishal.kp

2 Likes

Here is the code @vishal.kp

     Dim e_mail As New MailMessage()
    e_mail.From = New MailAddress(" FROM Mail ID")
    e_mail.[To].Add(" TO Mail ID")
    'e_mail.CC.Add(" CC mail ID")
    e_mail.Subject = "MAIL subject"

    Dim smtp As New SmtpClient(" SMTP server you want to use") 
    e_mail.Body = "BODY of the mail "
   smtp.Send(e_mail)

If you have any attachments to be sent in the mail, add these two lines of code

         Dim fileTXT As String = "Path of the file"
        Dim data As Net.Mail.Attachment = New Net.Mail.Attachment(fileTXT)
        data.Name = "The name of the file you want to display in the mail"

Do remember, before using the code , you need to import the mail package

Imports System.Net.Mail

4 Likes

i am not able to set the e_mail.[to] @HareeshMR

Are you getting any error? @vishal.kp

Have you imported the reference?

yea ive added the package. is the e_mail.[TO] should be done using assign?

How are you trying to use the code?

You need to paste the entire code in Invoke Code activity @vishal.kp

cant i use the invoke method?

I haven’t tried that with Invoke method and I don’t know much about that activity @vishal.kp

Any issue if you use Invoke code?

its throwing exception

Post the error @vishal.kp

19.7.0+Branch.master.Sha.8c253d13718eed5c7db27daef6facd1fe1b0d067

Source: Invoke code

Message: Exception has been thrown by the target of an invocation.

Exception Type: System.Reflection.TargetInvocationException

RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> RemoteException wrapping System.Net.Mail.SmtpException: 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. k3sm18409137pfg.23 - gsmtp
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at UiPathCodeRunner_5adf547f43b848f0b96124091bcef175.Run()
— End of inner exception stack trace —
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object providedArgs, ParameterModifier modifiers, CultureInfo culture, String namedParams)
at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object args)
at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List1 inArgs, IEnumerable1 imps, Object args)
at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

Its throwing above exception…

its above @HareeshMR

is the local host mentioned correctly buddy
if possible can i have view on the code been used in your workflow
Cheers @Manish540 @vishal.kp

Can you post what is the smtp you are using? @vishal.kp @Manish540

Dim smtp As New SmtpClient(“smtp.gmail.com”)
e_mail.Body = “Sending the mail without inbuilt activity”
smtp.Send(e_mail)

Then you need to accept the less secure apps policy @vishal.kp @Manish540

here is the useful link

And the above code is when you need to send the mail using the already opened or accessed outlook in your machine @vishal.kp @Manish540

As you are using gmail, you need to pass the credentials as well. so here is the code

This is more clear I hope. Let me know if you still have issues :slight_smile:

Leave all the above stuff @vishal.kp

Here is the working code. I just tried it and it is working perfectly

  Dim mail As New MailMessage()
    mail.To.Add("Mail id")
    mail.From = New MailAddress("Mail id")
    mail.Subject = "Confirmation of Registration on Job Junction."
    Dim Body As String = "Hi, this mail is to test sending mail using Gmail in ASP.NET"
    mail.Body = Body
    mail.IsBodyHtml = True
    Dim smtp As New SmtpClient("smtp.gmail.com", 587)

    smtp.UseDefaultCredentials = False
    smtp.EnableSsl = True
    smtp.Credentials = New System.Net.NetworkCredential("Mail id which you gave access to less secure apps", "password of that mail")
    smtp.Send(mail)
3 Likes