Form1.cs
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:2k
源码类别:

Email客户端

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Reflection;
  9. using Outlook;
  10. namespace OutLookDemo
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.         private void btn发送Email_Click(object sender, EventArgs e)
  19.         {
  20.             Outlook._Application oApp = new Outlook.Application();
  21.             Outlook.NameSpace oNameSpace = oApp.GetNamespace("MAPI");
  22.             bool html = false;
  23.             try
  24.             {
  25.                 oNameSpace.Logon(Missing.Value, Missing.Value, true, true);
  26.                 Outlook.MAPIFolder oOutboxFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
  27.                 Outlook.ApplicationClass a = new Outlook.ApplicationClass();
  28.                 Outlook.MailItem tt;
  29.                 tt = (Outlook.MailItem)a.CreateItem(OlItemType.olMailItem);
  30.                 tt.To = txt目标地址.Text;
  31.                 tt.CC = "";
  32.                 tt.Subject = txt标题.Text;
  33.                 if (html)
  34.                 {
  35.                     tt.HTMLBody = txt内容.Text;
  36.                 }
  37.                 else
  38.                 {
  39.                     tt.Body = txt内容.Text;
  40.                 }
  41.                 tt.SaveSentMessageFolder = oOutboxFolder;
  42.                 tt.Attachments.Add(txt附件.Text, Missing.Value, Missing.Value, Missing.Value);
  43.                 tt.Display(null);
  44.             }
  45.             finally
  46.             {
  47.                 oNameSpace.Logoff();
  48.             }
  49.            
  50.         }
  51.         private void btn附件_Click(object sender, EventArgs e)
  52.         {
  53.             DialogResult dr=openFileDialog1.ShowDialog();
  54.             if (dr == DialogResult.OK)
  55.             {
  56.                 this.txt附件.Text = openFileDialog1.FileName;
  57.             }
  58.         }
  59.     }
  60. }