C# 发送邮件

===============

app.config

<?xml version="1.0"?>
<configuration>
	<system.net>
		<mailSettings>
			<smtp from="liufjay@163.com">
				<network host="smtp.163.com" port="25" userName="test" password="test"/>
			</smtp>
		</mailSettings>
	</system.net>
</configuration>

发送邮件的方法

/// <summary>  
        /// 发送邮件方法  
        /// </summary>  
        /// <param name="from">Sender address</param>  
        /// <param name="to">Recepient address</param>  
        /// <param name="bcc">Bcc recepient</param>  
        /// <param name="cc">Cc recepient</param>  
        /// <param name="subject">Subject of mail message</param>  
        /// <param name="body">Body of mail message</param>  
        public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
        {
            // Instantiate a new instance of MailMessage  
            MailMessage mMailMessage = new MailMessage();
            // Set the sender address of the mail message  
            mMailMessage.From = new MailAddress(from);
            // Set the recepient address of the mail message  
            mMailMessage.To.Add(new MailAddress(to));
            // Check if the bcc value is null or an empty string  
            if ((bcc != null) && (bcc != string.Empty))
            {
                // Set the Bcc address of the mail message  
                mMailMessage.Bcc.Add(new MailAddress(bcc));
            }
            // Check if the cc value is null or an empty value  
            if ((cc != null) && (cc != string.Empty))
            {
                // Set the CC address of the mail message  
                mMailMessage.CC.Add(new MailAddress(cc));
            }       // Set the subject of the mail message  
            mMailMessage.Subject = subject;
            // Set the body of the mail message  
            mMailMessage.Body = body;
            // Set the format of the mail message body as HTML  
            mMailMessage.IsBodyHtml = true;
            // Set the priority of the mail message to normal  
            mMailMessage.Priority = MailPriority.Normal;
            // Instantiate a new instance of SmtpClient  
            SmtpClient mSmtpClient = new SmtpClient();
            // Send the mail message  
            mSmtpClient.Send(mMailMessage);
        }
调用的方法

 private void button1_Click(object sender, EventArgs e)
        {
            //SendMailMessage("test@163.com", "test@qq.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "mack", "mackmailinfo"); 
            SendMailMessage("testy@163.com", "test@qq.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "mack", "mackmailinfo");
        }  


===================================================================================

第二种方法(包含附件方法)

 /// <summary>
        /// C#发送邮件函数
        /// </summary>
        /// <param name="from">发送者邮箱</param>
        /// <param name="fromer">发送人</param>
        /// <param name="to">接受者邮箱</param>
        /// <param name="toer">收件人</param>
        /// <param name="Subject">主题</param>
        /// <param name="Body">内容</param>
        /// <param name="file">附件</param>
        /// <param name="SMTPHost">smtp服务器</param>
        /// <param name="SMTPuser">邮箱</param>
        /// <param name="SMTPpass">密码</param>

        /// <returns></returns>
        public bool sendmail(string sfrom, string sfromer, string sto, string stoer, string sSubject, string sBody, string sfile, string sSMTPHost, string sSMTPuser, string sSMTPpass)
        {
            //设置from和to地址
            MailAddress from = new MailAddress(sfrom, sfromer);
            MailAddress to = new MailAddress(sto, stoer);
            //创建一个MailMessage对象
            MailMessage oMail = new MailMessage(from, to);
            // 添加附件
            if (sfile != "")
            {
                oMail.Attachments.Add(new Attachment(sfile));
            }
            //邮件标题
            oMail.Subject = sSubject;
            //邮件内容
            oMail.Body = sBody;
            //邮件格式
            oMail.IsBodyHtml = false;
            //邮件采用的编码
            oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置邮件的优先级为高
            oMail.Priority = MailPriority.High;
            //发送邮件
            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = false;
            client.Host = sSMTPHost;
            client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                client.Send(oMail);
                return true;
            }
            catch (Exception err)
            {
                 MessageBox.Show( err.Message.ToString());
                return false;
            }
            finally
            {
                //释放资源
                oMail.Dispose();
            }

        }

方法调用

  //发送邮件
                string from = "test@163.com";  //发送者邮箱
                string fromer = "tt";  //发送人
                string to = "totest@163.com";  //接受者邮箱
                string toer = "ss";    //收件人
                string Subject = "test_mail";   //主题
                string file = System.AppDomain.CurrentDomain.BaseDirectory  + "test.xlsx";   //附件
                string Body = "发送表格邮件 请查收";  //内容
                string SMTPHost = "smtp.163.com";  //smtp服务器
                string SMTPuser = "test@163.com";  //邮箱
                string SMTPpass = "testpw";  //密码
                sendmail(from, fromer, to, toer, Subject, Body, file, SMTPHost, SMTPuser, SMTPpass);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值