C # Call outlook to send emails

Source: Internet
Author: User
Original article: C # Call outlook to send emails

A simple windows form program is written to implement the function of sending emails using Outlook. Next we will explain how to implement it step by step, and add the specific code.

  1. Open vs2010 and create a new windows form program.
  2. Right-click the reference of the project file and select Add reference.
  3. Click the com tab, select Microsoft. Office. InterOP. Outlook. dll, and click OK.
  4. Add reference: using Outlook = Microsoft. Office. InterOP. outlook;
  5. If you don't want to talk about anything else, you can directly access the code.

 

View code
 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using Outlook = Microsoft.Office.Interop.Outlook;
10
11 namespace SendMailByOutlook
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 Outlook.Application olApp = new Outlook.Application();
23 Outlook.MailItem mailItem = (Outlook.MailItem)olApp.CreateItem(Outlook.OlItemType.olMailItem);
24 mailItem.To = "[email protected]";
25 mailItem.Subject = "A test";
26 mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
27 mailItem.HTMLBody = "Hello world";
28 ((Outlook._MailItem)mailItem).Send();
29
30 mailItem = null;
31 olApp = null;
32 MessageBox.Show("Mail has been sent successfully!");
33
34
35 }
36 }
37 }


Finally, click send to prompt that the email is successfully sent!

Note that outlook must be enabled when calling outlook to send emails. Otherwise, the program throws an exception. What is a better solution for everyone? Please contact us!

C # Call outlook to send emails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.