HTML文件 <HTML> <BODY> <FORM action="sendmail.jsp" method="post"> <TABLE align="center"> <TR> <TD width="50%"> To:<BR><INPUT name="to" size="25"> </TD> <TD width="50%"> From:<BR><INPUT name="from" size="25"> </TD> </TR> <TR> <TD colspan="2"> Subject:<BR><INPUT name="subject" size="50"> </TD> </TR> <TR> <TD colspan="2"> <P>Message:<BR> <TEXTAREA name="text" rows=25 cols=85></TEXTAREA> </P> </TD> </TR> </TABLE> <INPUT type="submit" name="cb_submit" value=" Send "> <INPUT type="reset" name="cb_reset" value=" Clear "> </FORM> </BODY> </HTML> |
sendmail.jsp文件 <%@ page import=" Javax.mail.*, Javax.mail.internet.*, Javax.activation.*,Java.util.*"%> <html> <head> <TITLE>JavaScript/" target="_blank">JSP meets JavaMail, what a sweet combo.</TITLE> </head> <body> <% try{ Properties props = new Properties(); Session sendMailSession; Store store; Transport transport; sendMailSession = Session.getInstance(props, null); props.put("mail.smtp.host", "smtp.jspinsider.com"); Message newMessage = new MimeMessage(sendMailSession); newMessage.setFrom(new InternetAddress(request.getParameter("from"))); newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ( request.getParameter ("to"))); newMessage.setSubject(request.getParameter("subject")); newMessage.setSentDate(new Date()); newMessage.setText(request.getParameter("text")); transport = sendMailSession.getTransport("smtp"); transport.send(newMessage); %> <P>Your mail has been sent.</P> <% }catch(MessagingException m) { out.println(m.toString()); } %> </body> </html> |
form.htm <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title> JavaMail - 发送HTML邮件</title> </head> <body> <table border="0" cellspacing="0" cellpadding="0"> <form method="post" action="send.jsp"> <tr> <td>SMTP主机:</td> <td><input type="text" name="smtp" size="80"></td> </tr> <tr> <td>发信人:</td> <td><input type="text" name="from" size="80"></td> </tr> <tr> <td>收信人:</td> <td><input type="text" name="to" size="80"></td> </tr> <tr> <td>抄送人:</td> <td><input type="text" name="cc" size="80"></td> </tr> <tr> <td>暗送人:</td> <td><input type="text" name="bcc" size="80"></td> </tr> <tr> <td>主题:</td> <td><input type="text" name="subject" size="80"></td> </tr> <tr> <td valign="top">内容:</td> <td><textarea name="body" rows="5" cols="80"></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="发送"></td> </tr> </form> </table> </body> </html> send.jsp <% //变量声明 Java.lang.String smtp,from,to,cc,bcc,subject,body; //获得用户输入数据 smtp = request.getParameter("smtp"); from = request.getParameter("from"); to = request.getParameter("to"); cc = request.getParameter("cc"); bcc = request.getParameter("bcc"); subject = request.getParameter("subject"); if(subject!=null){ subject = new Java.lang.String(subject.getBytes("iso-8859-1")); } body = request.getParameter("body"); //发送邮件 mymail.mail.HTML.send(smtp,from,to,cc,bcc,subject,body); %> mymail.jaf.StringDataSource.Java package mymail.jaf; public class StringDataSource implements Javax.activation.DataSource { private Java.lang.String data; private Java.lang.String type; public StringDataSource(Java.lang.String data,Java.lang.String type){ this.data = data; this.type = type; } public Java.io.InputStream getInputStream() throws Java.io.IOException{ return new Java.io.StringBufferInputStream(data); } public Java.io.OutputStream getOutputStream() throws Java.io.IOException{ throw new Java.io.IOException("it does not support this method now!"); } public Java.lang.String getContentType(){ return type; } public Java.lang.String getName(){ return " mymail "; } } mymail.mail.HTML.Java package mymail.mail; public final class HTML { public static void send( Java.lang.String smtp, /*SMTP主机地址*/ Java.lang.String from, /*发信人*/ Java.lang.String to, /*收信人*/ Java.lang.String cc, /*抄送人*/ Java.lang.String bcc, /*暗送人*/ Java.lang.String subject, /*主题*/ Java.lang.String body /*内容*/ ) throws Java.lang.Exception { //变量声明 Java.util.Properties props; //系统属性 Javax.mail.Session mailSession; //邮件会话对象 Javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象 //设置系统属性 props = Java.lang.System.getProperties(); //获得系统属性对象 props.put("mail.smtp.host",smtp); //设置SMTP主机 //获得邮件会话对象 mailSession = Javax.mail.Session.getDefaultInstance(props,null); //创建MIME邮件对象 mimeMsg = new Javax.mail.internet.MimeMessage(mailSession); //设置发信人 mimeMsg.setFrom(new Javax.mail.internet.InternetAddress(from)); //设置收信人 if(to!=null) { mimeMsg.setRecipients(Javax.mail.Message.RecipientType.TO,Javax.mail. internet.InternetAddress.parse(to)); } //设置抄送人 if(cc!=null) { mimeMsg.setRecipients(Javax.mail.Message.RecipientType.CC,Javax.mail. internet.InternetAddress.parse(cc)); } //设置暗送人 if(bcc!=null) { mimeMsg.setRecipients(Javax.mail.Message.RecipientType.BCC,Javax.mail. internet.InternetAddress.parse(bcc)); } //设置邮件主题 //mimeMsg.setSubject(subject); mimeMsg.setSubject(subject,"gb2312"); //设置邮件内容,将邮件body部分转化为HTML格式 mimeMsg.setDataHandler(new Javax.activation.DataHandler(new mymail.jaf. StringDataSource (body,"text/html"))); //发送邮件 Javax.mail.Transport.send(mimeMsg); } } |
// Setup properties Properties props = System.getProperties(); props.put("mail.pop3.host", host); // Setup authentication, get session Authenticator auth = new PopupAuthenticator(); Session session = Session.getDefaultInstance(props, auth); // Get the store Store store = session.getStore("pop3"); store.connect(); |
import Javax.mail.*; import Javax.swing.*; import Java.util.*; public class PopupAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication() { String username, password; String result = JOptionPane.showInputDialog("Enter 'username,password'"); StringTokenizer st = new StringTokenizer(result, ","); username = st.nextToken(); password = st.nextToken(); return new PasswordAuthentication(username, password); } } |
// Create the message to forward Message forward = new MimeMessage(session); // Fill in header forward.setSubject("Fwd: " + message.getSubject()); forward.setFrom(new InternetAddress(from)); forward.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Create your new message part BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText( "Here you go with the original message:\n\n"); // Create a multi-part to combine the parts Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Create and fill part for the forwarded content messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(message.getDataHandler()); // Add part to multi part multipart.addBodyPart(messageBodyPart); // Associate multi-part with message forward.setContent(multipart); // Send message Transport.send(forward); |
// Define message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Hello JavaMail Attachment"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("Pardon Ideas"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(multipart); // Send the message Transport.send(message); |
<FORM ENCTYPE="multipart/form-data" method=post action="/myservlet"> <INPUT TYPE="file" NAME="thefile"> <INPUT TYPE="submit" VALUE="Upload"> </FORM> |
Multipart mp = (Multipart)message.getContent(); for (int i=0, n=multipart.getCount(); i<n; i++) { Part part = multipart.getBodyPart(i)); String disposition = part.getDisposition(); if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) (disposition.equals(Part.INLINE))) { saveFile(part.getFileName(), part.getInputStream()); } } |
// from saveFile() File file = new File(filename); for (int i=0; file.exists(); i++) { file = new File(filename+i); } |
if (disposition == null) { // Check if plain MimeBodyPart mbp = (MimeBodyPart)part; if (mbp.isMimeType("text/plain")) { // Handle plain } else { // Special non-attachment cases here of image/gif, text/html, ... } ... } |
Message message = …; Transport t = session.getTransport("smtp"); t.connect(); message.setRecipient(Message.RecipientType.TO,recipient1); t.sendMessage(message,recipient); message.setRecipient(Message.RecipientType.TO,recipient2); t.sendMessage(message,recipient); message.setRecipient(Message.RecipientType.TO,recipient3); t.sendMessage(message,recipient); t.close(); |
FileOutputStream fos = new FileOutputStream("test.mail"); Mimemessage.writeTo(fos); |
……