网站通过gmail 实现邮件的发送功能

来源:互联网 发布:java中加密解密字符串 编辑:程序博客网 时间:2024/06/05 09:59

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
/*----------------------------------
 * CreateDate: 2010-6-12
 * Author:gjs
 * description:发送邮件操作类
 *
 -----------------------------------*/
public class MailSend
{
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址 </param>
        /// <param name="MessageTo">收件人邮箱地址 </param>
        /// <param name="MessageSubject">邮件主题 </param>
        /// <param name="MessageBody">邮件内容 </param>
        /// <returns> </returns>
        public static bool Send(string MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
        {
            MailMessage message = new MailMessage();

            MailAddress from = new MailAddress(MessageFrom);

            message.From = from;

            //收件人邮箱地址可以是多个以实现群发
            message.To.Add(MessageTo);    
         
            message.Subject = MessageSubject;

            message.Body = MessageBody;

            //是否为html格式
            message.IsBodyHtml = true;

            //发送邮件的优先等级
            message.Priority = MailPriority.High;

            SmtpClient sc = new SmtpClient();
            //指定发送邮件的服务器地址或IP,根据不同的邮件服务商,写不同的服务器名
            //smtp.Sina.com
            sc.Host = "smtp.gmail.com";
             //指定发送邮件端口
            //sc.Port = 587;                        
            sc.UseDefaultCredentials = true;

            sc.EnableSsl = true;
           
            //指定登录服务器的用户名和密码
            sc.Credentials = new System.Net.NetworkCredential("wsyth8566857", "wsyth85668577");
           
            try
            {
                //发送邮件
                sc.Send(message);

            }
            catch (Exception e)
            {
                throw new Exception("发送邮件失败,请与管理员联系!");
            }
            return true;
        }
}

原创粉丝点击