JAVA郵件發(fā)送_第1頁
JAVA郵件發(fā)送_第2頁
JAVA郵件發(fā)送_第3頁
JAVA郵件發(fā)送_第4頁
JAVA郵件發(fā)送_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、properties prop = new properties(); prop.setproperty(tocol, smtp); prop.setproperty(mail.smtp.host, mailserver); prop.setproperty(mail.smtp.auth, true); session session = session.getinstance(prop, new authenticator() protected passwordauthentication getpasswordauthentication() retu

2、rn new passwordauthentication( user,password);); transport trans = session.gettransport(); message msg = new mimemessage(session); trans.connect(); msg.setcontent(中文message,text/plain;charset=gb2312); trans.sendmessage(msg,new address new internetaddress(user); trans.close(); 發(fā)表者:bluesmile979 10)bre

3、ak; fileout.println(line); fileout.flush(); fileout.close(); % % string host=request.getparameter(textfield); if(host=null)|(host.trim().equals() throw new runtimeexception(no mailhost parameter specified); string username=request.getparameter(textfield2); if(username=null)|(username.trim().equals()

4、 throw new runtimeexception(no username parameter specified); string password=request.getparameter(textfield3); if(password=null)|(password.trim().equals() throw new runtimeexception(no password parameter specified); out.println(get mail log); out.println(); parameters parms=new parameters(); parms.

5、request=request; parms.out=out; parms.socket=new socket(host,110); parms.mailout=new printwriter(parms.socket.getoutputstream(); parms.mailin=new bufferedreader(new inputstreamreader(parms.socket.getinputstream(); string line=parms.mailin.readline(); out.println(line); sendcommand(parms,user +userna

6、me); sendcommand(parms,pass +password); sendcommand(parms,stat); sendcommand(parms,uidl); /sendcommand(parms,list); / sendcommand(parms,retr 2); /sendcommand(parms,dele 2); / sendcommand(parms,rset); parms.mailout.print(list+rn); parms.mailout.flush(); for(;) line=parms.mailin.readline(); if(line.st

7、artswith(.)break; parms.out.println(s: +line); vector messagelist=new vector(); for(;) line=parms.mailin.readline(); if(line.startswith(.)break; messagelist.addelement(line); int n=messagelist.size(); for(int i=1;in;i+) getmessage(parms,i); /sendcommand(parms,dele +i); sendcommand(parms,quit); parms

8、.socket.close(); out.println(); %發(fā)表者:netnice這是一個(gè)完整的servlet搞得,webmail的代碼。 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.mail.*; import ernet.*; public class webmail extends httpservlet public void doget(httpservletrequest request

9、,httpservletresponse response) throws servletexception, ioexception string command = request.getparameter(command); try if (login.equalsignorecase(command) dologin(request, response); else if (list.equalsignorecase(command) dolist(request, response); else if (read.equalsignorecase(command) doread(re

10、quest, response); else if (reply.equalsignorecase(command) doreply(request, response); else if (send.equalsignorecase(command) dosend(request, response); else if (logout.equalsignorecase(command) dologout(request, response); catch (messagingexception e) throw new servletexception(messagingexception:

11、 + e); private string defaultfrom; private session session; private store store; private folder folder; private void dologin(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception, messagingexception string smtp = request.getparameter(smtp); string pop3 = reque

12、st.getparameter(pop3); string user = request.getparameter(user); string pass = request.getparameter(pass); / save a default from address defaultfrom = user + + pop3; / start the session perties properties = system.getproperties(); properties.put(mail.smtp.host, smtp); session = session.

13、getinstance(properties, null); / connect to the store store = session.getstore(pop3); store.connect(pop3, user, pass); / open the inbox folder folder = store.getfolder(inbox); folder.open(folder.read_only); / list the messages dolist(request, response); private message messages = null; private void

14、dolist(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception, messagingexception messages = folder.getmessages(); response.setcontenttype(text/html); printwriter writer = response.getwriter(); / start a table and print the header writer.println( list + + + dat

15、e + from + subject + ); / print each message for (int i = 0; i messages.length; +i) writer.println( + + messagesi.getsentdate() + + + messagesi.getfrom()0 + + + messagesi.getsubject() + + ); / end the table writer.println(); / add a logout link writer.println(logout); / end the page writer.println()

16、; writer.close(); private void doread(httpservletrequest request,httpservletresponse response) throws servletexception, ioexception, messagingexception int num = integer.parseint(request.getparameter(message); response.setcontenttype(text/html); printwriter writer = response.getwriter(); mimemessage

17、 message = (mimemessage) messagesnum; writer.println(read: + message.getsubject() + ); / print some select headers writer.println( + date: + message.getsentdate() + from: + message.getfrom()0 + to: + message.getrecipients( message.recipienttype.to)0 + subject: + message.getsubject() + ); contenttype

18、 ct = new contenttype(message.getcontenttype(); / if the text is in html, just print it if (text/html.equalsignorecase(ct.getbasetype() bufferedreader reader = new bufferedreader( new inputstreamreader( message.getinputstream(); string s; while (s = reader.readline() != null) writer.println(s); else

19、 object o = message.getcontent(); / if the text is plain, just print it if (o instanceof string) writer.println( + o + ); else / print the content type writer.println(message.getcontenttype(); / if it is a multipart, list the parts if (o instanceof mimemultipart) listparts(mimemultipart) o, writer);

20、 / end the message writer.println(); / print a link to reply writer.println(reply ); / print a link to logout writer.println(logout); / end the page writer.println(); writer.close(); 發(fā)表者:yanbuhan/* * (#)msgsend.java 1.15 00/05/24 * * copyright 1997-2000 sun microsystems, inc. all rights reserved. */

21、 import java.io.*; import .inetaddress; import perties; import java.util.date; import javax.mail.*; import ernet.*; /* * demo app that shows how to construct and send an rfc822 * (singlepart) message. * * xxx - allow more than one recipient on the command line * * author m

22、ax spivak * author bill shannon */ public class msgsend public static void main(string argv) new msgsend(argv); public msgsend(string argv) string to, subject = null, from = null, cc = null, bcc = null, url = null; string mailhost = null; string mailer = msgsend; string protocol = null, host = null,

23、 user = null, password = null; string record = null; / name of folder in which to record mail boolean debug = false; bufferedreader in = new bufferedreader(new inputstreamreader(system.in); int optind; for (optind = 0; optind argv.length; optind+) if (argvoptind.equals(-t) protocol = argv+optind; el

24、se if (argvoptind.equals(-h) host = argv+optind; else if (argvoptind.equals(-u) user = argv+optind; else if (argvoptind.equals(-p) password = argv+optind; else if (argvoptind.equals(-m) mailhost = argv+optind; else if (argvoptind.equals(-f) record = argv+optind; else if (argvoptind.equals(-s) subjec

25、t = argv+optind; else if (argvoptind.equals(-o) / originator from = argv+optind; else if (argvoptind.equals(-c) cc = argv+optind; else if (argvoptind.equals(-b) bcc = argv+optind; else if (argvoptind.equals(-l) url = argv+optind; else if (argvoptind.equals(-d) debug = true; else if (argvoptind.equal

26、s(-) optind+; break; else if (argvoptind.startswith(-) system.out.println( usage: msgsend -l store-url | -t prot -h host -u user -p passwd); system.out.println( t-s subject -o from-address -c cc-addresses -b bcc-addresses); system.out.println( t-f record-mailbox -m transport-host -d address); system

27、.exit(1); else break; try if (optind argv.length) / xxx - concatenate all remaining arguments to = argvoptind; system.out.println(to: + to); else system.out.print(to: ); system.out.flush(); to = in.readline(); if (subject = null) system.out.print(subject: ); system.out.flush(); subject = in.readline

28、(); else system.out.println(subject: + subject); properties props = system.getproperties(); / xxx - could use session.gettransport() and transport.connect() / xxx - assume were using smtp if (mailhost != null) props.put(mail.smtp.host, mailhost); / get a session object session session = session.getd

29、efaultinstance(props, null); if (debug) session.setdebug(true); / construct the message message msg = new mimemessage(session); if (from != null) msg.setfrom(new internetaddress(from); else msg.setfrom(); msg.setrecipients(message.recipienttype.to, internetaddress.parse(to, false); if (cc != null) msg.setrecipients(message.recipienttype.cc, internetaddress.parse(cc, false); if (bcc != null) msg.setrecipients(message.recipienttype.bcc, internetaddress.parse(bcc, false); msg.setsubject(subject); collect(in, msg

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論