How to send a mail using outlook vsto
To send an email using Outlook VSTO (Visual Studio Tools for Office), you can use the Microsoft.Office.Interop.Outlook
namespace and the Application
object to create a new email message and send it.
Here is an example code snippet that demonstrates how to send an email using Outlook VSTO:
using Microsoft.Office.Interop.Outlook;
// Create a new instance of the Outlook application
Outlook.Application app = new Outlook.Application();
// Create a new email message
Outlook.MailItem mail = app.CreateItem(Outlook.OlItemType.olMailItem);
// Set the subject and body of the email
mail.Subject = "Test Email";
mail.Body = "This is a test email sent using Outlook VSTO";
// Set the recipient(s) of the email
mail.To = "[email protected]";
// Set the CC and BCC fields (optional)
mail.CC = "[email protected]";
mail.BCC = "[email protected]";
// Add attachments (optional)
mail.Attachments.Add("C:\\Path\\To\\Attachment.txt");
// Send the email
mail.Send();
// Clean up
app.Quit();
This code creates a new email message, sets the subject, body, and recipient(s), and then sends the email using the Send()
method. You can also add attachments, CC, and BCC fields as needed.
Note that you need to have the Outlook VSTO installed and configured on your development machine in order to use this code. Additionally, you may need to adjust the code to fit your specific requirements, such as handling errors and exceptions.
Here are some additional tips and considerations:
- Make sure to use the correct namespace and object references. In this example, we're using the
Microsoft.Office.Interop.Outlook
namespace and theApplication
object. - You can use the
MailItem
object to set additional properties, such as the priority, importance, and categories. - You can use the
Attachments
collection to add multiple attachments to the email. - You can use the
SendUsingAccount
property to specify the account to use when sending the email. - You can use the
Display
method to display the email message before sending it. - You can use the
Cancel
method to cancel the sending of the email.