Send e-mail with attachments
Abstract: Send e-mail with attachments
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> continue to Sina as an example, the use of the annex to upload components, the company O'Reilly The cos.jar. Please cos.jar, mail.jar and activation.jar, put it in your application's WEB WEB-INF/lib directory.
Test the JavaMail3.html
<head>
</ Head>
<body>
JavaMail use to send e-mail - Annex
<form Name="SendMessage" Method="post" action="JavaMail3.jsp" enctype="multipart/form-data">
Sender: <input type = "text" name = "From" size = "30" maxlength = "30">
Addressee: <input type = "text" name = "To" size = "30" maxlength = "30">
Subject: <input type = "text" name = "Subject" size = "30" maxlength = "30">
Format: <select name = "Type" size = "1">
<option Value="text/plain"> Text </ option>
<option Value="text/html"> HTML </ option>
</ Select>
Annex: <input type = "file" name = "FileName" size = "20" maxlength = "20">
:
<textarea Name="Message" cols=40 rows=5> </ textarea>
<table Width="669" border="0"> <tr> <td width="391"> <input type="submit" value="å‘é€">
<input Type="reset" value="é‡å¡«">
</ Form>
</ Body>
Annex mail address and JavaMail3.jsp
<% @ Page import = "javax.mail .*"%>
<% @ Page import = "javax.mail.internet .*"%>
<% @ Page import = "javax.activation .*"%>
<% @ Page import = "java.util .*, java.io. *"%>
<% @ Page import = "com.oreilly.servlet.MultipartRequest"%>
<% @ Page contentType = "text / html; charset = GB2312"%>
<head>
</ Head>
<body>
</ Td> <td width="268"> </ td> </ tr> </ table>
JavaMail use to send e-mail - Annex
<%
InternetAddress address [] = null;
Request.setCharacterEncoding ( "gb2312");
MultipartRequest multi = new MultipartRequest (request, ".", 5 * 1024 * 1024, "GB2312");
String mailserver = "smtp.sina.com.cn";
String From = multi.getParameter ( "From");
String to = multi.getParameter ( "To");
String Subject = multi.getParameter ( "Subject");
String type = multi.getParameter ( "Type");
String messageText = multi.getParameter ( "Message");
String FileName = multi.getFilesystemName ( "FileName");
Boolean sessionDebug = false;
Try (
/ / Set by the server and use the Mail Transfer Protocol used by the
Java.util.Properties props = System.getProperties ();
Props.put ( "mail.host" mailserver);
Props.put ( "mail.transport.protocol", "smtp");
Props.put ( "mail.smtp.auth", "true ");// specified the need for SMTP Authentication
/ / Create new services Session
Javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance (props, null);
MailSession.setDebug (sessionDebug);
Message msg = new MimeMessage (mailSession);
/ / Set sender
Msg.setFrom (new InternetAddress (From));
/ / Set the addressee
Address = InternetAddress.parse (to, false);
Msg.setRecipients (Message.RecipientType.TO, address);
/ / Set the theme of the letter
Msg.setSubject (Subject);
/ / Set the time messenger
Msg.setSentDate (new Date ());
If (FileName! = Null)
(
File file = new File (FileName);
/ / If there is annex, first e-mail to some depositors
MimeBodyPart mbp1 = new MimeBodyPart ();
/ / Set for the type of message content, text / plain or text / html
Mbp1.setContent (messageText, type + "; charset = GB2312");
/ / Come on the annex to deal with
MimeBodyPart mbp2 = new MimeBodyPart ();
FileDataSource fds = new FileDataSource (FileName);
Mbp2.setDataHandler (new DataHandler (fds));
Mbp2.setFileName (MimeUtility.encodeText (fds.getName (), "GB2312", "B"));
/ / Then finally integrate the two, as a message sent
Multipart mp = new MimeMultipart ();
Mp.addBodyPart (mbp1);
Mp.addBodyPart (mbp2);
Msg.setContent (mp);
)
Else
(
/ / If no attachment, the e-mail directly deposit
Msg.setContent (messageText, type + "; charset = GB2312");
)
Transport transport = mailSession.getTransport ( "smtp");
/ / E-mail please enter your user name and password, not to use my ^ _ ^.
Transport.connect (mailserver, "zz3zcwb", "1234567");
Transport.sendMessage (msg, msg.getAllRecipients ());
Transport.close ();
Out.println ( "e-mail has been successfully sent");
)
Catch (MessagingException mex)
(
Out.println (mex);
Mex.printStackTrace ();
)
%>
</ Body>
The Annex to me! Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back






