C# 发邮件类可发送附件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 发邮件类可发送附件

C# 发邮件类可发送附件

 2014/8/22 12:32:43  王小贝  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Net.Mail;namespaceAjax.发邮件{publicclassGetMail{//MailAddressds=newMailAddress("1738819932@qq.com");//Send(ds,"674580075@qq.com","邮件主题","邮件内容","附件名称")
  • 标签:C# 邮件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace Ajax.发邮件
{
    public class GetMail
    {
         //MailAddress ds = new MailAddress("1738819932@qq.com");
         //   Send(ds, "674580075@qq.com", "邮件主题", "邮件内容","附件名称");
        public GetMail()
        { }
             /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="MessageFrom">发件人邮箱地址</param>
        /// <param name="MessageTo">收件人邮箱地址</param>
        /// <param name="MessageSubject">邮件主题</param>
        /// <param name="MessageBody">邮件内容</param>
        /// <param name="SUpFile">附件</param>
        /// <returns></returns>
        public bool Send(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody, 

string SUpFile)
        {
            MailMessage message = new MailMessage();
            message.From = MessageFrom;
            message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发

            message.Subject = MessageSubject;
            message.Body = MessageBody;

            if (SUpFile != "")
            {
               
                SUpFile = Server.MapPath("~/发邮件/Upfile/" + SUpFile);//获得附件在本地地址
                //将文件进行转换成Attachments
                Attachment data = new Attachment(SUpFile, MediaTypeNames.Application.Octet);
                // Add time stamp information for the file.
                ContentDisposition disposition = data.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(SUpFile);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(SUpFile);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(SUpFile);

                message.Attachments.Add(data);
                System.Net.Mime.ContentType  ctype=new System.Net.Mime.ContentType (); 
            }

            message.IsBodyHtml = true; //是否为html格式
            message.Priority = MailPriority.Normal; //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP
            sc.Port = 25; //指定发送邮件端口
            sc.Credentials = new System.Net.NetworkCredential("发送邮箱账号", "账号密码"); //指定登录服务器的try
            {
                sc.Send(message); //发送邮件
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}

 

发表评论
用户名: 匿名