首页 > 代码库 > STMP发送邮件

STMP发送邮件

  注意事项:

  1.要使用自定义验证机制的system.net.mail类发送。不要用system.web.mail类发送,通常后者发送的邮件都会被当垃圾邮件,这是跟验证机制和有关。

  2.要声明邮件地址和头信息(msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); 披上outlook的马甲)

  3.特殊情况,就是可能有些服务器在你已经披上合法的马甲后还是会当作垃圾邮箱处理,如网易,那是因为你的邮件内容可能存在一些,敏感的字眼,或者违法的信息导致的。

  引用类:

  using System.Net.Mail;

  using System.Net;

  

        /// <summary>        /// 发送邮件           /// </summary>           /// <param name="Subject">主题</param>           /// <param name="Body">邮件内容</param>           /// <param name="To">对方邮箱</param>           /// <param name="MailFrom">发件人</param>           /// <param name="MailFromName">邮件显示发送人的名称</param>           /// <param name="MailDomain">服务器地址</param>           /// <param name="MailServerUserName">用户名和密码</param>           /// <param name="MailServerPassWord">密码</param>           /// <returns></returns>           public bool SendEmail(string Subject, string Body, string To, string MailFrom, string MailFromName, string MailDomain, string MailServerUserName, string MailServerPassWord) {            try {                MailMessage msg = new MailMessage();                msg.From = new MailAddress(MailFrom, MailFromName);                msg.To.Add(new MailAddress(To, To));                msg.Subject = Subject;                msg.Body = Body;                msg.IsBodyHtml = true;                msg.Priority = MailPriority.Normal;                msg.SubjectEncoding = System.Text.Encoding.UTF8;                msg.BodyEncoding = System.Text.Encoding.UTF8;                //不被当作垃圾邮件的关键代码--Begin                           msg.Headers.Add("X-Priority", "3");                msg.Headers.Add("X-MSMail-Priority", "Normal");                msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");   //本文以outlook名义发送邮件,不会被当作垃圾邮件                           msg.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");                msg.Headers.Add("ReturnReceipt", "1");                //不被当作垃圾邮件的关键代码--End                           SmtpClient client = new SmtpClient(MailDomain);                client.UseDefaultCredentials = false;                client.Credentials = new NetworkCredential(MailServerUserName, MailServerPassWord); //帐号密码                           client.DeliveryMethod = SmtpDeliveryMethod.Network;                client.Send(msg);                return true;            }            catch {                return false;            }        }

 

STMP发送邮件