How to send outlook mail through VBA/VB.net/C#

how to send outlook mail through VBA/ VB.net / C# with attachment.

please help on priority beacuse this is required in production env.

@itbuddy1,

We can use Send outlook mail or Send SMTP mail, why you need to send email using C# or VB.NET

C#:

SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);

smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");
// smtpClient.UseDefaultCredentials = true; // uncomment if you don't want to use the network credentials
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();

//Setting From , To and CC
mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");
mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));
mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));

smtpClient.Send(mail);

hi, thanks for reply.

Here I don’t want to use smtp because some time smtp in not working in my company. so I am prefer outlook rightnow.

so, please if you have any solution then share

thanks again

please help anyone.It’s inportant because this is required in Proruction Env.

Hi @itbuddy1
Check this:

Imports Microsoft.Office.Interop.Outlook
Sub SendEmailWithAttachment()
    Dim outlookApp As New Application()
    Dim mailItem As MailItem = outlookApp.CreateItem(OlItemType.olMailItem)
    
    mailItem.Subject = "Email Subject"
    mailItem.Body = "Email Body"
    mailItem.To = "recipient@example.com"
    
    mailItem.Attachments.Add("C:\Path\To\Your\File.pdf")
    
    mailItem.Send()
End Sub

Why not use Outlook activities?

Hi,
Thanks for the reply.
I am using this code in Invoke Code activity but this shows error.

You don’t have to bother with Outlook. Send SMTP Mail Message is much simpler and more reliable, doesn’t require Outlook installed, and won’t require any VBA code.