选择显示字体大小

javamail快速入门-3

下一步,如下所示运行simplesender,用你自己的smtp替换你email设置中的smtp.myisp.net

  java com.lotontech.mail.simplesender smtp.myisp.net bill@lotontech.com
ben@lotontech.com "hello" "just to say hello."

  如果它能正常工作起来的话,在接收端你将看到如图2所示的内容


     图2 从simplesender读取的消息

  simplesender类主要由send(…)方法完成。其代码如下:

/**

* "send" method to send the message.

*/

public static void send(string smtpserver, string to, string from

, string subject, string body)

 {

  try

  {

   properties props = system.getproperties();

   // -- attaching to default session, or we could start a new one --

   props.put("mail.smtp.host", smtpserver);

   session session = session.getdefaultinstance(props, null);

   // -- create a new message --

   message msg = new mimemessage(session);

   // -- set the from and to fields --

   msg.setfrom(new inte.netaddress(from));

   msg.setrecipients(message.recipienttype.to,

   inte.netaddress.parse(to, false));

   // -- we could include cc recipients too --

   // if (cc != null)

   // msg.setrecipients(message.recipienttype.cc

   // ,inte.netaddress.parse(cc, false));

   // -- set the subject and body text --

   msg.setsubject(subject);

   msg.settext(body);

   // -- set some other header information --

   msg.setheader("x-mailer", "lotontechemail");

   msg.setsentdate(new date());

   // -- send the message --

   transport.send(msg);

   system.out.println("message sent ok.");

  }

  catch (exception ex)

  {

   ex.printstacktrace();

  }

 }

 }

 
  首先,请注意,你得到一个emailsession(java.mail.session),没有它你什么都做不了。在这个案例中,你调用了sesion.getdefultinstance(…)来得到一个共享session,其它的桌面应用程序也可以使用它;你也可以通过session.getinstance(…)方法建立一个新的session,它对于你的应用程序来说是唯一的。然后我们能够证明email客户端应用程序对每个用户来说,其使用方法都是一样的,比如它可以是一个用servlet实现的基于web的email系统。

  建立一个session需要设置一些属性;如果你通过smtp发送消息,那么至少需要设置mail.smtp.host属性。在api文档中你可以找到其它的属性。

  现在你有了一个session,创建了一个消息。在这个例子中,你就可以设置email地址信息、主题、正文了,所有这些都取自于命令行。你也可以设置一些头信息,包括日期等,并且你还可以指定复制(cc)的收件人。

  最后,你通过javax.mail.transport类发送消息。如果你想知道我们的emailsession,请看后面的消息构造器。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons