请问下用.net 怎么直接发送Eml邮件呢,EMl邮件中有可能有附件,那怎么和文字一起发送呢,请大家帮帮忙啊

解决方案 »

  1.   

    1).Net1.1  System.Web.Mail命名空间:
        这个命名控件下包含了以下的对象和三个属性:
        包含的对象:
            MailAttachment:与邮件附件有关的对象类
            MailMessage   :邮件主体
            SmtpMail      :负责发送邮件的SMTP协议。
        属性列表:
            MailEncoding :邮件的编码(Base64,UUEncode)  
            MailFormat    :邮件的格式(Html超文本格式,Text纯文本格式)
            MailPriority :邮件优先级(High, Medium, Low)    构建MailMessage对象:
        MailMessage对象是邮件的承载主体,通常可以先构建好MailMessage对象,然后设置它的属性的方式来构建邮件程序,下面列出了的是一些常用的属性:
            Attachments        :邮件附件
            Bcc                    :暗送地址
            Body                 :邮件主体
            BodyFormat           :邮件格式(html,text)
            Cc                    :抄送地址
            From                :发信人地址
            Priority              :邮件优先级(High, Medium,Low)
            Subject             :邮件主题
            To                    :接收人地址
            UrlContentBase     :在HTML格式邮件中的URL编码方式
            UrlContentLocation:邮件信息的优先级(High, Medium,Low)    使用SMTPMail发送邮件
        构建好MailMessage对象之后,还需要使用另外一个对象-SMTPMail-来发送邮件,SMTPMAIL有一个很重要的方法:Send,该方法有两个不同用法,其中一个可以仅仅发送整个的MailMessage对象:
            SmtpMail.Send(myEmailObject);
        另外一个允许你分别指定发送者,收邮件地址,邮件主题,邮件主题,然后再发送出去:
            SmtpMail.Send(strFrom, strTo, strSubject, strBody); 示例:void  SendMail(){      System.Web.Mail.MailMessage myEmail = new System.Web.Mail.MailMessage();     // SET MESSAGE PARAMETERS 
        myEmail.From =  "[email protected]";
        myEmail.To =  "[email protected]";
        myEmail.Subject = "咨询培训事宜"; 
        myEmail.BodyFormat = System.Web.Mail.MailFormat.Html; 
        myEmail.Body = "学习.net,请问你们的实训课程都何时开课?.";     //SEND THE MESSAGE 
        System.Web.Mail.SmtpMail.Send(myEmail); } 
    http://www.devdao.com
    2).Net2.0 System.Net.Mail命名空间:
      这个命名控件下包含了以下主要对象和主要属性:
        包含的对象:
            MailAddress:表示电子邮件发件人或收件人的地址
            Attachment   :表示电子邮件的附件
            MailAddressCollection   :存储与电子邮件关联的电子邮件地址
            MailMessage   :表示可以使用 SmtpClient 类发送的电子邮件
            SmtpClient     :允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。
        属性列表:
            DeliveryNotificationOptions:描述电子邮件的传送通知选项 
            MailPriority    :指定 MailMessage 的优先级
            SmtpAccess :指定允许的简单邮件传输协议 (SMTP) 服务器访问级别
            SmtpDeliveryMethod:指定如何发送电子邮件
            SmtpStatusCode:指定使用 SmtpClient 类发送电子邮件的结果    构建MailMessage对象:
        MailMessage对象是邮件的承载主体,通常可以先构建好MailMessage对象,然后设置它的属性的方式来构建邮件程序,下面列出了的是一些常用的属性:
            Attachments        :邮件附件
            Bcc                    :暗送地址
            Body                 :邮件主体
            Cc                    :抄送地址
            From                :发信人地址
            Subject             :邮件主题
            To                    :接收人地址    使用SmtpClient发送邮件
        构建好MailMessage对象之后,还需要使用另外一个对象-SmtpClient-来发送邮件,SmtpClient有一个很重要的方法:Send,可以发送整个的MailMessage对象:
            SmtpClient.Send(MailMessage);在这个例子中,使用System.Net.Mail命名空间   /// <summary>
            /// 自动发送邮件
            /// </summary>
            /// <param name="strMail">邮件接受地址</param>
            /// <param name="strRegistCode">注册码</param>
            /// <returns></returns>
            bool SendMail(string strMail, string strRegistCode)
            {
                string strHost = "mail.cdce.cn";   //STMP服务器地址
                string strAccount = "jackguo";       //SMTP服务帐号
                string strPwd = "111111";       //SMTP服务密码
                SmtpClient _smtpClient = new SmtpClient();
                _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                _smtpClient.Host = strHost; ;//指定SMTP服务器
                _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码            string content = "你的验证码是:" + strRegistCode + ",请获取后立即完成注册,关联基本信息。";
                content += "<br><br>该邮件为自动发送,请不要回复!";            MailAddress sender = new MailAddress("[email protected]", "统考信息");
                MailAddress receriver = new MailAddress(strMail);            MailMessage mail = new MailMessage(sender, receriver);
                MailAddressCollection ToMail = new MailAddressCollection();            mail.Subject = "统考信息系统验证码";//主题            mail.Body = content;//内容
                mail.BodyEncoding = System.Text.Encoding.Default;//正文编码
                mail.IsBodyHtml = true ;
                mail.Priority = MailPriority.High;//优先级            try
                {
                    _smtpClient.Send(mail);
                    return true;
                }
                catch (Exception e)
                {
                   return false;
                }        }
    文章来自: 86站长站转载请注明出处 详文参考:http://www.86master.com/wlbc/5/2009/23751.html
      

  2.   

    我们这里有一个用java做的是可以的,所以我想.net一定应该也是可以的
      

  3.   

    2.0以上的SDK中写的已经很全了,参考那个就可以
      

  4.   

      public static string SendMail(string txtHost, string txtFrom, string txtPass, string txtTo, string txtSubject, string txtBody, bool isBodyHtml, System.Net.Mail.MailPriority priority, System.Text.Encoding encoding,string[] files)
            {
                //电子邮件附件
                Attachment data = null;
                //传送的电子邮件类
                MailMessage message = new MailMessage(txtFrom, txtTo);
                //设置标题
                message.Subject = txtSubject;
                //设置内容
                message.Body = txtBody;
                //是否采用HTML编码
                message.IsBodyHtml = isBodyHtml;
                //电子邮件的优先级别 
                message.Priority = priority;
                //内容采用的编码方式
                message.BodyEncoding = Encoding.Unicode;
                try
                {
                    //添加附件
                    if (files.Length > 0 && files != null) 
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            //实例话电子邮件附件,并设置类型
                            data = new Attachment(files[i], System.Net.Mime.MediaTypeNames.Application.Octet );
                            //实例邮件内容
                            System.Net.Mime.ContentDisposition disposition = data.ContentDisposition;
                            //取得建档日期
                            disposition.CreationDate = System.IO.File.GetCreationTime(files[i]);
                            //取得附件修改日期
                            disposition.ModificationDate = System.IO.File.GetLastWriteTime(files[i]);
                            //取得读取日期
                            disposition.ReadDate = System.IO.File.GetLastAccessTime(files[i]);
                            //设定文件名称
                            System.IO.FileInfo fi = new System.IO.FileInfo(files[i]);
                            disposition.FileName = fi.Name.ToString();
                            //添加附件
                            message.Attachments.Add(data);
                        }
                    }
                    //实例从送电子邮件类
                    SmtpClient client = new SmtpClient(); 
                    //设置电子邮件主机名称
                    client.Host = txtHost;
                    //取得寄信人认证
                    client.Credentials = new NetworkCredential(txtFrom, txtPass);
                    //发送电子邮件
                    client.EnableSsl = false;
                    client.Send(message);
                    return "邮件发送成功";
                }
                catch (Exception Err)
                {
                    //返回错误信息
                    return Err.Message;
                } 
                finally
                {
                    //销毁电子邮件附件
                    if (data != null)
                    {
                        data.Dispose();
                    }
                    //销毁传送的电子邮件实例 
                    message.Dispose();
                }
            }
      

  5.   

    学习。将收藏。。我也想问。好像可以用JMail组件。
      

  6.   

    补充:其实中files  是附件路径~
      

  7.   

    大家别跑题啊,我是说生成一个eml文件,在eml文件中有附件,然后发送eml文件也直接就把附件发送出去了
      

  8.   

    http://www.gotdotnet.com/userarea/default.aspx   
    非常好的例子
      

  9.   

    public bool SendMail()   
        {   
            MailMessage myEmail = new MailMessage();   
            myEmail.From = new MailAddress(fromMail.Text.Trim());   
            myEmail.To.Add(toMail.Text.Trim());   
            myEmail.Subject = subject.Text.Trim();   
            myEmail.IsBodyHtml = format.SelectedItem.Value == "0" ? false : true;   
            //附件   
                    string ServerFileName = "";   
                    if (this.upfile.PostedFile.ContentLength != 0)   
                    {   
                        string upFileName = this.upfile.PostedFile.FileName;   
                        string[] strTemp = upFileName.Split('.');   
                        string upFileExp = strTemp[strTemp.Length - 1].ToString();   
                        ServerFileName = Server.MapPath("upload/"+DateTime.Now.ToString("yyyyMMddHHmmss") + "." + upFileExp);   
                        this.upfile.PostedFile.SaveAs(ServerFileName);   
                        myEmail.Attachments.Add(new Attachment(ServerFileName));   
                           
                    }   
      
            myEmail.Body = body.Text.Trim();   
            myEmail.BodyEncoding = Encoding.UTF8;   
            myEmail.Priority = MailPriority.High;   
            SmtpClient smtp = new SmtpClient();   
            smtp.Credentials = new NetworkCredential("[email protected]", "邮件密码");   
            smtp.Port = 25;    
            smtp.Host = "smtp.sina.com";    
            smtp.EnableSsl = false;   
            try  
            {   
      
                smtp.Send(myEmail);   
                File.Delete(ServerFileName);   
            }   
            catch (Exception e)   
            {   
                File.Delete(ServerFileName);   
                throw;   
            }   
            return true;   
        }   还有一个发邮件的jmail组件,可以下载下来看看
    非常好用