Sending Email

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


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



Here is the code

     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 "

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


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

Are you getting any error?

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

cant i use the invoke method?

I haven’t tried that with Invoke method and I don’t know much about that activity

Any issue if you use Invoke code?

its throwing exception

Post the error


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

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

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

Then you need to accept the less secure apps policy @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 @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

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("", 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")