首页 > 代码库 > C#发送邮件

C#发送邮件

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;namespace SendMessage{    class SendEmail    {                /// <summary>        /// 使用163发送邮箱        /// </summary>        public static void SendMial163()        {            MailMessage mailMessage = new MailMessage();            mailMessage.To.Add("to@qq.com");            mailMessage.From = new System.Net.Mail.MailAddress("form@163.com");            mailMessage.Subject = "hello world";            mailMessage.Body = "这是一封用C#发送的邮件";            mailMessage.IsBodyHtml = true;            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;            mailMessage.Priority = System.Net.Mail.MailPriority.Normal;            SmtpClient smtpClient = new SmtpClient();            smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, "@form-pwd@");            smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;            smtpClient.Host = "smtp." + mailMessage.From.Host;            smtpClient.Port = 25;            smtpClient.Timeout = 3000;            smtpClient.EnableSsl = false;            smtpClient.Send(mailMessage);        }        /// <summary>    /// 发送GMail    /// </summary>    /// <param name="toEmail">客户的邮箱</param>    /// <param name="FromEmail">我们的邮箱</param>    /// <param name="pwd">密码</param>    /// <param name="dispalyName">显示的名称</param>    /// <param name="subject">标题</param>    /// <param name="body">内容</param>    /// <returns></returns>        public static int SendMailLocalhost(string toEmail,string FromEmail, string pwd,string dispalyName,string subject,string body)        {            MailMessage msg = new MailMessage();            msg.To.Add(toEmail);                      /*             * msg.To.Add("b@b.com");             * msg.To.Add("b@b.com");             * msg.To.Add("b@b.com");可以发送给多人             */            //msg.CC.Add(c@c.com);             /*             * msg.CC.Add("c@c.com");             * msg.CC.Add("c@c.com");可以抄送给多人             */            //上面这些可以不用管            msg.From = new MailAddress(FromEmail, dispalyName, System.Text.Encoding.UTF8);            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/            msg.Subject = subject;//邮件标题             msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码             msg.Body = body;//邮件内容             msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码             msg.IsBodyHtml = true;//是否是HTML邮件             msg.Priority = MailPriority.High;//邮件优先级             SmtpClient client = new SmtpClient();            client.UseDefaultCredentials = false;            client.Credentials = new System.Net.NetworkCredential(FromEmail, pwd);            //上述写你的GMail邮箱和密码             client.Port = 587;//Gmail使用的端口             client.Host = "smtp.gmail.com";            client.EnableSsl = true;//经过ssl加密             try            {                //client.SendAsync(msg, userState);                client.Send(msg);                return 1;            }            catch (System.Net.Mail.SmtpException ex)            {                return -1;            }        }    }   }