亲宝软件园·资讯

展开

C#调用OutLokk实现发送邮件

芝麻粒儿 人气:0

实践过程

效果

代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    #region 查找与指定文件关联在一起的程序的文件名
    /// <summary>
    /// 查找与指定文件关联在一起的程序的文件名
    /// </summary>
    /// <param name="hwnd">窗口句柄</param>
    /// <param name="lpOperation">指定字串“open”来打开lpFlie文档,或指定“Print”来打印它</param>
    /// <param name="lpFile">想用关联程序打印或打开一个程序名或文件名</param>
    /// <param name="lpParameters">如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数</param>
    /// <param name="lpDirectory">想使用的完整路径</param>
    /// <param name="nShowCmd">定义了如何显示启动程序的常数值</param>
    /// <returns>非零表示成功,零表示失败</returns>
    [DllImport("shell32.dll", EntryPoint = "ShellExecuteA")]
    public static extern int ShellExecute(
     IntPtr hwnd,
     String lpOperation,
     String lpFile,
     String lpParameters,
     String lpDirectory,
     int nShowCmd
     );
    #endregion

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
        {
            if (Regex.IsMatch(textBox1.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                ShellExecute(this.Handle, String.Empty, "mailto:" + textBox1.Text, String.Empty, String.Empty, 1);
            else
            {
                MessageBox.Show("请输入正确的邮箱格式");
                textBox1.Text = string.Empty;
            }
        }
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(128, 41);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(152, 23);
        this.button1.TabIndex = 1;
        this.button1.Text = "调用OutLook对其发送";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 15);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(77, 12);
        this.label1.TabIndex = 1;
        this.label1.Text = "邮件接收人:";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(95, 12);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(185, 21);
        this.textBox1.TabIndex = 0;
        this.textBox1.Text = "mingrisoft@mingrisoft.com";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 74);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "调用OutLook发送邮件";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
}

加载全部内容

相关教程
猜你喜欢
用户评论