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#中发送邮件主要代码_第1张图片

版权所有·转载请注明出处!

谢谢合作^_^

你可能感兴趣的:(c#中发送邮件主要代码)