1. 程式人生 > 其它 >C# 傳送電子郵件案例

C# 傳送電子郵件案例

先了解一下,常用SMTP地址

1、QQ郵箱(mail.qq.com)

POP3伺服器地址:pop.qq.com(埠:110)

SMTP伺服器地址:smtp.qq.com(埠:25)

2、搜狐郵箱(sohu.com):

POP3伺服器地址:pop3.sohu.com(埠:110)

SMTP伺服器地址:smtp.sohu.com(埠:25)

3、HotMail郵箱(hotmail.com):

POP3伺服器地址:pop.live.com(埠:995)

SMTP伺服器地址:smtp.live.com(埠:587)

4、移動139郵箱:

POP3伺服器地址:POP.139.com(埠:110)

SMTP伺服器地址:SMTP.139.com(埠:25)

5、景安網路郵箱:

POP3伺服器地址:POP.zzidc.com(埠:110)

SMTP伺服器地址:SMTP.zzidc.com(埠:25)

 

如果是163郵箱,很簡單

如果使用的是QQ郵箱,需要知道接收郵箱號,需要知道傳送QQ郵箱號和傳送郵箱密碼,主要是注意這個密碼(看下圖);

 

 

最後開始 C#設計

 

 

C# 程式碼區

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net.Mail;
using System.Net.Mime;
using System.Net;
using System.IO;


namespace SendEmail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.ImeMode = ImeMode.Hangul;
        }

        //窗體的Load事件
        private void Form1_Load(object sender, EventArgs e)
        {
            //新增倆個smpt伺服器的名稱
            cmbBoxSMTP.Items.Add("smtp.qq.com");
            cmbBoxSMTP.Items.Add("smtp.163.com"); //hqttfdfpxbpnbfhc
            cmbBoxSMTP.Items.Add("smtp.gmail.com");

            //設定為下拉列表
            cmbBoxSMTP.DropDownStyle = ComboBoxStyle.DropDownList;
            //預設選中第一個選項
            cmbBoxSMTP.SelectedIndex = 0;
            //在下面新增你想要初始化的內容,比如顯示姓名、使用者名稱等

        }

        //新增按鈕的單擊事件
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //定義並初始化一個OpenFileDialog類的物件
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = Application.StartupPath;
            openFile.FileName = "";
            openFile.RestoreDirectory = true;
            openFile.Multiselect = false;

            //顯示開啟檔案對話方塊,並判斷是否單擊了確定按鈕
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                //得到選擇的檔名
                string fileName = openFile.FileName;
                //將檔名新增到TreeView中
                treeViewFileList.Nodes.Add(fileName);
            }
        }

        //刪除按鈕的單擊事件
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //判斷是否選中了節點
            if (treeViewFileList.SelectedNode != null)
            {
                //得到選擇的節點
                TreeNode tempNode = treeViewFileList.SelectedNode;
                //刪除選中的節點
                treeViewFileList.Nodes.Remove(tempNode);
            }
            else
            {
                MessageBox.Show("請選擇要刪除的附件。");
            }
        }

        //傳送按鈕的單擊事件
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //確定smtp伺服器地址。例項化一個Smtp客戶端
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cmbBoxSMTP.Text);
                //生成一個傳送地址
                string strFrom = string.Empty;
                if (cmbBoxSMTP.Text == "smtp.163.com")  //cmbBoxSMTP
                {
                    strFrom = txtUserName.Text + "@163.com";
                }
                else if (cmbBoxSMTP.Text == "smtp.qq.com")
                {
                    strFrom = txtUserName.Text + "@qq.com";
                }
                else
                {
                    strFrom = txtUserName.Text + "@gmail.com";
                }


                //構造一個發件人地址物件
                MailAddress from = new MailAddress(strFrom, txtDisplayName.Text, Encoding.UTF8);
                //構造一個收件人地址物件
                MailAddress to = new MailAddress(txtEmail.Text, txtToName.Text, Encoding.UTF8);

                //構造一個Email的Message物件
                MailMessage message = new MailMessage(from, to);

                //為 message 新增附件
                foreach (TreeNode treeNode in treeViewFileList.Nodes)
                {
                    //得到檔名
                    string fileName = treeNode.Text;
                    //判斷檔案是否存在
                    if (File.Exists(fileName))
                    {
                        //構造一個附件物件
                        Attachment attach = new Attachment(fileName);
                        //得到檔案的資訊
                        ContentDisposition disposition = attach.ContentDisposition;
                        disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
                        //向郵件新增附件
                        message.Attachments.Add(attach);
                    }
                    else
                    {
                        MessageBox.Show("檔案" + fileName + "未找到!");
                    }
                }

                //新增郵件主題和內容
                message.Subject = txtSubject.Text;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = rtxtBody.Text;
                message.BodyEncoding = Encoding.UTF8;

                //設定郵件的資訊
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = false;

                //如果伺服器支援安全連線,則將安全連線設為true。
                //gmail支援,163不支援,如果是gmail則一定要將其設為true
                if (cmbBoxSMTP.Text == "smpt.163.com")
                {
                    client.EnableSsl = false;

                }
                else if (cmbBoxSMTP.Text == "smtp.qq.com")
                {
                    client.EnableSsl = false;
                }
                else
                {
                    client.EnableSsl = true;
                }

                //設定使用者名稱和密碼。
                //string userState = message.Subject;
                client.UseDefaultCredentials = false;
                string username = txtUserName.Text;
                string passwd = txtPassword.Text;
                //使用者登陸資訊
                NetworkCredential myCredentials = new NetworkCredential(username, passwd);
                client.Credentials = myCredentials;
                //傳送郵件
                client.Send(message);
                //提示傳送成功
                MessageBox.Show("傳送成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

 

測試結果如下圖:

QQ郵箱傳送到163郵箱

 

 

QQ郵箱 傳送到 QQ郵箱