⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sender.java

📁 Mail Of Java
💻 JAVA
字号:
/*这个程序没有身份验证,用户名,收件人都做死掉了,发的内容也是死的。看明白了能否改进一下做个图形界面多点选择?*//*程序输出结果,你应该开心,逃掉了这么多细节EBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]DEBUG SMTP: useEhlo true, useAuth falseDEBUG: SMTPTransport trying to connect to host "127.0.0.1", port 25DEBUG SMTP RCVD: 220 ok RsProxyServer ReadyDEBUG: SMTPTransport connected to host "127.0.0.1", port: 25DEBUG SMTP SENT: EHLO ddn-00887DEBUG SMTP RCVD: 250-RsProxyServer says hello250-SIZE 0250-8BITMIME250-DSN250-ETRN250-AUTH=LOGIN250 EXPNDEBUG SMTP Found extension "SIZE", arg "0"DEBUG SMTP Found extension "8BITMIME", arg ""DEBUG SMTP Found extension "DSN", arg ""DEBUG SMTP Found extension "ETRN", arg ""DEBUG SMTP Found extension "AUTH=LOGIN", arg ""DEBUG SMTP Found extension "EXPN", arg ""DEBUG SMTP: use8bit falseDEBUG SMTP SENT: MAIL FROM:<zenghai@ddn-00887>DEBUG SMTP RCVD: 250 ok MailToDEBUG SMTP SENT: RCPT TO:<mithrandir@ddn-00887>DEBUG SMTP RCVD: 250 ok RcptToDEBUG SMTP SENT: RCPT TO:<mithrandir@ddn-00887>DEBUG SMTP RCVD: 250 ok RcptToVerified Addresses  mithrandir@ddn-00887  mithrandir@ddn-00887DEBUG SMTP SENT: DATADEBUG SMTP RCVD: 354 ok Send it kkkDEBUG SMTP SENT:DEBUG SMTP RCVD: 250 Message QueueDEBUG SMTP SENT: QUIT*/package mailofjava;import javax.mail.*;import java.util.*;import java.io.*;import javax.mail.internet.*;public class Sender implements MailSymbols{//实现之后props里的符号就简单了 private String rec="mithrandir@ddn-00887"; private String sub="Hello! Frodo!"; private String cc="mithrandir@ddn-00887"; private String mail="Hello! Frodo! peril is approaching! go to Minas Tirith now@!"; private Session session;//session没有子类,可以被共享,来自javax.mail包 private Message msg;//放内容,实现接口part,有子类MimeMessage,来自javax.mail包public void sendNow(){  Properties props=new Properties();//dictionary-hashtable-properties  props.put("mail.smtp.host","127.0.0.1");//没有imail我这程序就跑不动。呵呵运行这程序的时候必须有活的smtp server  Auth au=new Auth();//  session=Session.getDefaultInstance(props,null);    session=Session.getDefaultInstance(props,au);  session.setDebug(false); //允许调试true,因此可以用getDebug取调试信息,消息多得吓人。哼哼  /*  取得缺省的session对象.  如果当前没有session对象就新建一个对象作为缺省的sessoin  缺省的session对象是共享的,在同一jvm中有效 .  ,而session中又包含用户名,密码等敏感的东西,所以对这个对象访问是有限制的。  创建session的能直接使用它。否则有更复杂的验证等着你。.它假设javamail要的东西放在prop里。要求请看javaMail 规范的Appendix A  mail.store.protocol,  mail.transport.protocol,  mail.host,  mail.user  mail.from  */   try{     msg= new MimeMessage(session);     msg.setFrom( new InternetAddress("zenghai@ddn-00887"));     //InternetAddress的超 类是个抽象的Address,可以取得地址类型,请大家试试     InternetAddress toAddress=new InternetAddress(rec);//收件人     msg.addRecipient(Message.RecipientType.TO,toAddress);//加收件人     InternetAddress ccAddress=new InternetAddress(cc);//收件人     msg.addRecipient(Message.RecipientType.CC ,ccAddress);//加收件人     msg.setSubject(sub);     msg.setText(mail) ;     Transport.send(msg);   }catch(MessagingException ex){     while((ex=(MessagingException)ex.getNextException() )!=null)       ex.printStackTrace() ;   }  // String myprop = getDefaultUserName();   //System.out.println("The default store protocol is "+ myprop);}//send now  public Sender() {  }  public static void main(String[] argv){    Sender sd=new Sender();    sd.sendNow() ;  }}class Auth extends Authenticator{  Properties pwd; public Auth(){  super();  try{    pwd= new Properties();    pwd.put("Frodo","123");    pwd.put("Sam","456");    pwd.put("Mithrandir","111");  }catch(Exception e){ e.printStackTrace() ;};}public PasswordAuthentication getPasswordAuthentication(){  String str=   getDefaultUserName();;  System.out.println("the user:"+str);  if(pwd.containsKey(str)){    return new PasswordAuthentication(str,(String) pwd.get(str));  }//if  else     return null;}//pass}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -