首页 > Windows开发 > 详细

C# JMAIL发送邮件

时间:2015-04-25 18:11:34      阅读:386      评论:0      收藏:0      [点我收藏+]
  /// <summary>
    /// 发送邮件到网络
    /// </summary>
    public static void SendMail(string UserName, string PassWord, string SMTPServer, string Subject, string body, string FromEmail, string ToEmail,string FuJianList)
    {
        try
        {
            Message Jmail = new Message();
            DateTime t = DateTime.Now;

            //Slient属性:如果设置为true,Jmail不会抛出例外错误,Jmail.Send()会根据操作结果返回True或False
            Jmail.Silent = false;

            //Jmail创建的日志,提前loging属性设置为True
            Jmail.Logging = true;

            //字符集,缺省为"US-ASCII";
            Jmail.Charset = "GB2312";

            //信件的ContentType,缺省为"Text/plain",字符串如果你以Html格式发送邮件,改为"Text/Html"即可。
            //Jmail.ContentType = "text/html";

            //添加收件人
            Jmail.AddRecipient(ToEmail, "", "");
            Jmail.From = FromEmail;

            //发件人邮件用户名
            Jmail.MailServerUserName = UserName;

            //发件人邮件密码
            Jmail.MailServerPassWord = PassWord;

            //设置邮件标题
            Jmail.Subject = Subject;

            //邮件添加附件(多附件的话,可以再加一条Jmail.AddAttachment("c:\test.jpg",true,null);就可以搞定了。
            //注意:为了添加附件,要把上面的Jmail.ContentType="text/html";删掉,否则会在邮件里出现乱码
            string[] FuJian = FuJianList.Split(|);
            for (int kk = 0; kk < FuJian.Length; kk++)
            {
                if (FuJian[kk].Trim().Length > 0)
                {
                    Jmail.AddAttachment(System.Web.HttpContext.Current.Request.MapPath("../UploadFile") + "\\" + FuJian[kk].ToString(), true, null);
                }
            }

            //邮件内容
            Jmail.Body = body + t.ToString();

            //Jmail发送的方法
            Jmail.Send(SMTPServer, false);
            Jmail.Close();
        }
        catch (Exception e)
        {
            System.Web.HttpContext.Current.Response.Write("<script>alert(‘" + e.Message.ToString() + "‘);</script>");
        }
    }
 /**/
    /// <summary>
    /// 利用Jmail接收邮件
    /// </summary>
    public static bool ReciveByJmail(string UserName, string PassWord, string PopServer, int Port, DateTime MaxDate)
    {
        /**/
        ///建立收邮件对象
        jmail.POP3Class popMail = new POP3Class();

        /**/
        ///建立邮件信息接口
        jmail.Message mailMessage;

        /**/
        ///建立附件集接口
        jmail.Attachments atts;

        /**/
        ///建立附件接口
        jmail.Attachment att;
        bool resultbool = false;
        try
        {
            popMail.Connect(UserName, PassWord, PopServer, Port);

            /**/
            ///如果收到邮件
            if (0 < popMail.Count)
            {
                ZWL.BLL.ERPNetEmail MyModel = new ZWL.BLL.ERPNetEmail();
                MyModel.EmailState = "未读";
                MyModel.ToUser = ZWL.Common.PublicMethod.GetSessionValue("UserName");
                MyModel.TimeStr = MyModel.TimeStr = DateTime.Now;

                /**/
                ///根据取到的邮件数量依次取得每封邮件
                for (int i = 1; i <= popMail.Count; i++)
                {
                    /**/
                    ///取得一条邮件信息
                    mailMessage = popMail.Messages[i];

                    /**/
                    ///取得该邮件的附件集合
                    atts = mailMessage.Attachments;

                    /**/
                    ///设置邮件的编码方式                          
                    mailMessage.Charset = "GB2312";

                    /**/
                    ///设置邮件的附件编码方式                         
                    mailMessage.Encoding = "Base64";

                    /**/
                    ///是否将信头编码成iso-8859-1字符集                        
                    mailMessage.ISOEncodeHeaders = false;

                    /**/
                    ///邮件的优先级                     
                    // txtpriority.Text = mailMessage.Priority.ToString();

                    /**/
                    ///邮件的发送人的信箱地址                      
                    MyModel.FromUser = mailMessage.From;

                    /**/
                    ///邮件的发送人                     
                    MyModel.FromUser = mailMessage.FromName;

                    /**/
                    ///邮件主题                   
                    MyModel.EmailTitle = mailMessage.Subject;

                    /**/
                    ///邮件内容                   
                    MyModel.EmailContent = mailMessage.Body;

                    /**/
                    ///邮件大小                        
                    //MyModel.si = mailMessage.Size.ToString();

                    for (int j = 0; j < atts.Count; j++)
                    {
                        /**/
                        ///取得附件
                        att = atts[j];

                        /**/
                        ///附件名称                              
                        string attname = att.Name;
                        MyModel.FuJian = attname;
                        string path = System.Web.HttpContext.Current.Request.MapPath("../UploadFile") + "\\" + attname;
                        if (!File.Exists(path))
                        {
                            /**/
                            ///上传到服务器
                            att.SaveToFile(path);
                        }                     

                    }

                }

                att = null;
                atts = null;
                MyModel.Add();

                resultbool = true;
            }
            else
            {
                resultbool = false;
            }


        }
        catch
        {
            resultbool=false;
        }
        finally
        {
            popMail.DeleteMessages();
            popMail.Disconnect();
            popMail = null;
        }
        return resultbool;
    }
}

 

C# JMAIL发送邮件

原文:http://www.cnblogs.com/streamice/p/Jmail.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!