收发邮件

来源:互联网 发布:原生js实现加载更多 编辑:程序博客网 时间:2024/05/16 15:14

 发邮件:

//邮件发送
        public void GetMail(string toaddr,string fromaddr,string subject,string body,string uid,string pwd,string server)
        {
            MailMessage mail = new MailMessage();
            mail.To = toaddr;//发送邮件的地址,多个可用“,”隔开。;
            mail.From = fromaddr;
            mail.Subject = subject;
            mail.Body = body;
            mail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //是否需要验证,一般是要的   
            mail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendusername", uid);
            //自己邮箱的用户名   
            mail.Fields.Add("
http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd);
            //自己邮箱的密码
            SmtpMail.SmtpServer = server;
            SmtpMail.Send(mail);
        }

使用是JMAIL收邮件:

jmail.POP3Class jpop = new jmail.POP3Class();
    private void getMail()
    {
        this.LBtitle.Text = "";
        this.LBContent.Text = "";
        this.LBDate.Text = "";
        this.LBFrom.Text = "";
        this.Lb_attach.Text = "";
        jmail.Message Msg = new jmail.Message();
        jmail.Attachments atts;
        jmail.Attachment att;


        //Mime m = new Mime();

        //username为用户名,该方法通过用户名获取该用户的pop设置,即用户的POP用户名,密码,POP服务器地址以及端口号这四个参数,这四个参数是连接POP服务器的必用参数.

        //连接POP服务器


        jpop.Connect(this.TB_UserName.Text, this.TB_Pwd.Text, this.TB_pop3server.Text, 110);
        Msg = jpop.Messages[Convert.ToInt16(this.TB_mailNumber.Text)];
        Msg.ContentType = "text/html";
        Msg.Charset = "GB2312";
        this.LBnewNum.Text = jpop.Count.ToString();
        this.LBtitle.Text += "title:" + Msg.Subject + "<br/>";
        this.LBDate.Text += "date:" + Msg.Date.ToString()+"<br/>";
        this.LBFrom.Text += "from::" + Msg.FromName + "<" + Msg.From + ">";
        this.LBContent.Text += Msg.Body;


        if (Msg.Attachments.Count > 0)
        {
            this.Lb_attach.Text = Msg.Attachments[0].Name + ".pdf";
            Msg.Attachments[0].SaveToFile(System.Web.HttpContext.Current.Server.MapPath("
//mailattach//") + Msg.Attachments[0].Name + ".pdf");     
        }
        jpop.Disconnect();
        Msg.Close();

    }

原创粉丝点击