Archive for the ‘Y&D Studio’ Category

JavaMail

星期日, 20 12 月, 2009

J2EE项目里有用到JavaMail,研究了下。下面是参考资料和我的JavaMail:

JavaMail

J2EE JavaMail

如何使用JSP发送邮件

java利用JavaMail实现邮件功能

package mail;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
     private Properties props;//系统属性,可以get和put属性名值对。
     private Session session;//根据系统属性设置会话
     private Message msg;//MimeMessage为其子类
     //管理员SMTP服务器信息
     private String host;
     private String username;
     private String password;
     private String from;

     public Mail(String host, String username, String password, String from) {
        props = System.getProperties();
        //props.put(“mail.smtp.host”, “smtp.jspinsider.com”);
        props.put(“mail.smtp.host”, host);
        props.put(“mail.smtp.auth”, “true”);
        session = Session.getDefaultInstance(props);
        msg = new MimeMessage(session);
        this.host = host;
        this.username = username;
        this.password = password;
        this.from = from;
     }

     public void send(String to) throws MessagingException {
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(“测试”);
        msg.setText(“测试”);
        Transport transport = session.getTransport(“smtp”);
        transport.connect((String)props.get(“mail.smtp.host”), username, password);
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
        transport.close();
        //Transport.send(msg);
     }
}

Lex Version0.4 class Heap

星期五, 4 12 月, 2009

Lex Version0.4到此为止已经完成了编译原理的实验,实现了从词法分析到语法分析再到语义分析生成三地址码的过程。

class Heap部分根据语义分析生成三地址码的需要进行了部分修改。全部代码稍后放出。

#ifndef _HEAP_H_
#define _HEAP_H_
class Heap {
private:
 int syn[100];//存储词法分析后的符号表种别码
 char token[100][10];//存储词法分析后的符号表词素
 int head, tail;//游标,head表示当前使用的位置,tail表示当前可用的第一个位置。
public:
 Heap();
 ~Heap();
 int first();//head=0并返回该syn。-1表示失败。
 int prev();//head前移并返回该syn。-1表示失败。
 int next();//head后移并返回该syn。-1表示失败。
 bool append(int, char*);//加入syn和token
 char* getToken();//获得当前词素
};
#endif

#include “StdAfx.h”
#include “Heap.h”
#include <string.h>

Heap::Heap()
{
 for(int i = 0; i < 100; i++)
  syn[i] = -1;
 memset(token, 0, sizeof(token));
 head = tail = 0;
}

Heap::~Heap()
{

}

int Heap::first()
{
 head = 0;
 return syn[head];
}

int Heap::prev()
{
 if(head == 0)
  return -1;
 return syn[–head];
}

int Heap::next()
{
 if(head == tail – 1)
  return -1;
 return syn[++head];
}

bool Heap::append(int tempsyn, char* temptoken)
{
 if(tail >= 100)
  return false;
 syn[tail] = tempsyn;
 strcpy(token[tail++], temptoken);
 return true;
}

char* Heap::getToken()
{
 return token[head];
}

SMTP

星期一, 30 11 月, 2009

参考资料:

http://hi.baidu.com/kwunkuklan/blog/item/455f121749120a044b90a75e.html

http://tool.chinaz.com/Tools/Base64.aspx

TCP/IP课要求写个程序用SMTP发送一封邮件。telnet试验了下163和新浪的SMTP。下面为过程(163的SMTP貌似有限制,新浪的可以发送成功):

telnet smtp.163.com 25
HELO 163.com
AUTH LOGIN
dXNlcm5hbWU6(username:)
base64加密后的用户名
UGFzc3dvcmQ6(Password:)
base64加密后的密码

telnet smtp.sina.com.cn 25
HELO
AUTH LOGIN
VXNlcm5hbWU6(Username:)
base64加密后的用户名
UGFzc3dvcmQ6(Password:)
base64加密后的密码
MAIL FROM: <***@sina.com.cn>//邮件发送地址,不可伪造
RCPT TO: <***@qq.com>//目的地址
DATA
TO://目的地址,可伪造
FROM://发送地址,可伪造
SUBJECT:
.
QUIT

注意:可能小小的输入错误都会导致操作不成功。代码暂时就不贴了。

Lex Version0.2 class Heap

星期六, 21 11 月, 2009

#ifndef _HEAP_H_
#define _HEAP_H_
class Heap {
private:
 int syn[100];//存储词法分析后的符号表
 int head, tail;//游标
public:
 Heap();
 ~Heap();
 int first();//head=0并返回该syn。-1表示失败。
 int prev();//head前移并返回该syn。-1表示失败。
 int next();//head后移并返回该syn。-1表示失败。
 bool append(int);//加入syn
};
#endif

#include “StdAfx.h”
#include “Heap.h”

Heap::Heap()
{
 for(int i = 0; i < 100; i++)
  syn[i] = -1;
 head = tail = 0;
}

Heap::~Heap()
{

}

int Heap::first()
{
 head = 0;
 return syn[head];
}

int Heap::prev()
{
 if(head == 0)
  return -1;
 return syn[–head];
}

int Heap::next()
{
 if(head == tail – 1)
  return -1;
 return syn[++head];
}

bool Heap::append(int tempsyn)
{
 if(tail >= 200)
  return false;
 syn[tail++] = tempsyn;
 return true;
}

Lex

星期五, 20 11 月, 2009

// Lex.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <process.h>

//P83 3.4.2保留字和标识符的识别
//初始化时就将各个保留字填入符号表中
const char* KEYWORD[6] = {“begin”, “if”, “then”, “while”, “do”, “end”};
char buffer[1024] = {0};//输入缓冲区
char token[10] = {0};//词素,数组大小限制着标识符的长度。
int forward = 0, syn = -1;//游标和种别码

void analyzer();

int main(int argc, char* argv[])
{
 //输入,以#结束。
 printf(“请输入:(示例输入begin x:=9;if x>0 then x:=2*x+1/3;end#)\n”);
 char input;
 do
 {
  input = getchar();
  buffer[forward++] = input;
 }
 while(input != ‘#’);

 //分析,以#的种别码0结束。
 forward = 0;
 do
 {
  analyzer();
  printf(“(%d, %s)\n”, syn, token);
 }
 while(syn != 0);
 system(“pause”);
 return 0;
}

void analyzer()
{
 memset(token, ‘\0’, sizeof(token));//清空前一个已分析的词素
 char ch = buffer[forward++];//当前分析的字符
 while(isspace(ch))//忽略空格
  ch = buffer[forward++];
 switch(ch)
 {
  case ‘+’:
   token[0] = ch;
   syn = 13;
   return;
  case ‘-‘:
   token[0] = ch;
   syn = 14;
   return;
  case ‘*’:
   token[0] = ch;
   syn = 15;
   return;
  case ‘/’:
   token[0] = ch;
   syn = 16;
   return;
  case ‘:’:
   token[0] = ch;
   if(buffer[forward] == ‘=’)
   {
    forward++;
    token[1] = ‘=’;
    syn = 18;
   }
   else
    syn = 17;
   return;
  case ‘<‘:
   token[0] = ch;
   if(buffer[forward] == ‘>’)
   {
    forward++;
    token[1] = ‘>’;
    syn = 21;
   }
   else if(buffer[forward] == ‘=’)
   {
    forward++;
    token[1] = ‘=’;
    syn = 22;
   }
   else
    syn = 20;
   return;
  case ‘>’:
   token[0] = ch;
   if(buffer[forward] == ‘=’)
   {
    forward++;
    token[1] = ‘=’;
    syn = 24;
   }
   else
    syn = 23;
   return;
  case ‘=’:
   token[0] = ch;
   syn = 25;
   return;
  case ‘;’:
   token[0] = ch;
   syn = 26;
   return;
  case ‘(‘:
   token[0] = ch;
   syn = 27;
   return;
  case ‘)’:
   token[0] = ch;
   syn = 28;
   return;
  case ‘#’:
   token[0] = ch;
   syn = 0;
   return;
  default:
   int i = 0;
   if(isdigit(ch))//数字
   {
    while(isdigit(ch))
    {
     token[i] = ch;
     ch = buffer[forward++];
     i++;
    }
    forward–;
    syn = 11;
    return;
   }
   else if(isalpha(ch))//字符组成保留字或标识符
   {
    while(isalnum(ch))
    {
     token[i] = ch;
     ch = buffer[forward++];
     i++;
    }
    forward–;
    for(int j = 0; j < 6; j++)
    {
     if(strcmp(token, KEYWORD[j]) == 0)
     {
      syn = j + 1;
      return;
     }
    }
    syn = 10;
    return;
   }
 }
}