天天看點

c#中發送郵件主要代碼

         最近在做企業人事管理系統,前兩天老師突然說企業得有個能發送郵件的這麼一個功能,就讓我嘗試着找找能不能想辦法加到這個企業人事管理系統裡邊。

        之後小弟我就在網上開始了大海撈針搬的尋找,哎呦喂。。。我可算是知道了,對于我這一個專科院校才上兩個月的學生,找這個可真是難

啊,網上各式各樣的代碼都有,弄得我蒙蹬的···但是由于小兄弟我比較認學哈。就勉強的編出了這麼一個,發送郵件的小程式。希望大家批評指正。

        這是小弟第一次在在網上釋出自己的代碼,有點激動哈··· ···

··· ··· 首先,加上“using System.Net.Mail;”

··· ··· 其次,就是代碼啦。。接住噢。。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Net.Mail;  ----這個就是後加上的。

namespace 發送郵件

{

    public partial class Frm發送郵件 : Form

    {

        public Frm發送郵件()

        {

            InitializeComponent();

        }

        private void lab添加附件_Click(object sender, EventArgs e) 

        {

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            openFileDialog.Filter = "所有檔案(*.*)|*.*";

            if (openFileDialog.ShowDialog(this) == DialogResult.OK)

            {

                txt附件.Text = openFileDialog.FileName;

            }

        }

        private void btn發送_Click(object sender, EventArgs e)

        {

            string address = "";

            string displayName = "";

            string w_txt收件人 = txt收件人.Text.Trim();

            if (w_txt收件人 == "")

            {

                MessageBox.Show("請輸入收件人位址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;

            }

            MailAddress from = new MailAddress("發件人的郵箱位址", "發件人姓名"); //郵件的發件人

            MailMessage newMailMessage = new MailMessage();

            string[] mailNames = (txt收件人.Text).Split(',');

            {

                try

                {

                    foreach (string name in mailNames)

                    {

                        if (name != string.Empty)

                        {

                            if (name.IndexOf('<') > 0)

                            {

                                displayName = name.Substring(0, name.IndexOf('<'));

                                address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');

                            }

                            else

                            {

                                displayName = string.Empty;

                                address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');

                            }

                        }

                        newMailMessage.From = from;

                        newMailMessage.To.Add(new MailAddress(address, displayName));

                        newMailMessage.Body = txtbody.Text;

                        newMailMessage.Subject = txtTital.Text;

                        //設定SMTP伺服器位址

                        SmtpClient newclient = new SmtpClient("smtp.sina.com");   //以新浪為例(126的用不了,也不曉得為啥)

                        newclient.UseDefaultCredentials = false;

                        //此處設定發件人郵箱的使用者名和密碼

                        newclient.Credentials = new System.Net.NetworkCredential("賬号", "密碼"); //發件人的賬号和密碼

                        newclient.DeliveryMethod = SmtpDeliveryMethod.Network;

                        newMailMessage.Attachments.Add(new Attachment(txt附件.Text));          // 發送附件

                        newMailMessage.Priority = MailPriority.High;  //設定發送級别

                        //發送郵件

                        newclient.Send(newMailMessage);

                    }

                    MessageBox.Show("郵件發送完畢!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                catch (Exception exp)

                {

                    MessageBox.Show("郵件發送發生錯誤:" + exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

        }

    }

}

主界面的截圖,嘿嘿。。。

c#中發送郵件主要代碼

版權所有·轉載請注明出處!

謝謝合作^_^