首頁 > 軟體

C#呼叫OutLokk實現傳送郵件

2022-12-27 14:01:02

實踐過程

效果

程式碼

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;
}

到此這篇關於C#呼叫OutLokk實現傳送郵件的文章就介紹到這了,更多相關C# OutLokk傳送郵件內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com