使用VS2003 发送Email

使用VS2003发送Email与之后VS2005版本及以上VS版本不一样,记录一下,

需要引用using System.Web.Mail;

public void SendEmail()
    {
        try
        {
            //Tools.WriteLog("==============开始发送邮件==============");
            MailMessage mail = new MailMessage();
            //设置接收邮箱 (群发邮箱之间用","英文逗号隔开)
            mail.To = "接收者邮箱地址";
            //设置发送邮箱
            mail.From = "发送者邮箱地址";
            //设置支持服务器验证
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //设置用户名
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "发送者邮箱地址");

            //设置用户密码
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "发送者邮箱密码");
            //发送主题
            mail.Subject = "我的邮件";
            //发送邮件内容
            mail.Body = "这是我发送的邮件";
            //邮件内容格式
            mail.BodyFormat = MailFormat.Html;
            //邮件内容编码
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            //邮件等级
            mail.Priority = MailPriority.High;
            //添加邮件附件
            mail.Attachments.Add(new MailAttachment("D:/1.txt", MailEncoding.Base64));
            #region 可以传入一个ArrayList 存放附件地址,发送多个附件
            //ArrayList mailAttachments = new ArrayList();
            //mailAttachments.Add("path");
            //for (int i = 1; i < mailAttachments.Count + 1; i++)
            //{
            //    if (System.IO.File.Exists(mailAttachments[i - 1].ToString()))
            //    {
            //        mail.Attachments.Add(new MailAttachment(mailAttachments[i - 1].ToString(), MailEncoding.Base64));
            //    }
            //}
            #endregion
            //设置邮件发送smtp服务器
            SmtpMail.SmtpServer = "smtp.qq.com";
            //发送
            SmtpMail.Send(mail);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "');</script>");
        }

    }

 

posted @ 2013-12-10 10:13  命运爱上你  阅读(229)  评论(0编辑  收藏  举报