hi..
Following code is used to send email (using JavaMail api)
thru one of my xml apps..try this...remove all
xml stuff and make it simple java program (or function)..
this code facilitates also sending attachments with email...
--------------------------------------------------
<xsp:include>javax.mail.*</xsp:include>
<xsp:include>javax.mail.internet.*</xsp:include>
</xsp:structure>
<xsp:logic>
private static String SendMailTest() {
String mailTo = "[EMAIL PROTECTED]";
String mailFrom = "[EMAIL PROTECTED]";
String mailHost = "corporate.planet.net";
boolean debug = true; // change to get more information
String msgText = "This is just a test mail.\nThis email is from
JavaMail app.";
String msgText2 = "You can write more lines from here.";
String[] filesToBeAttached =
{"C:"+File.separator+"XmlDTD.ppt","C:"+File.separator+"Introduction.xml","C:
"+File.separator+"Introduction.xsl"};
//boolean sendmultipart = Boolean.valueOf(debug).booleanValue();
boolean sendmultipart = true;
// set the host
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
// create a message
Message msg = new MimeMessage(session);
// set the from
InternetAddress from = new InternetAddress(mailFrom);
msg.setFrom(from);
InternetAddress[] address = {new InternetAddress(mailTo)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("JavaMail APIs Test");
if (!sendmultipart) {
// send a plain text message
msg.setContent(msgText, "text/plain");
} else {
// send a multipart message
//create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(msgText, "text/plain");
mp.addBodyPart(mbp1);
//mbp2.setContent(msgText2, "text/plain");
<![CDATA[
//attach the file to the message
for(int i=0;i<filesToBeAttached.length;i++){
// create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
javax.activation.FileDataSource fds = new
javax.activation.FileDataSource(filesToBeAttached[i]);
mbp2.setDataHandler(new javax.activation.DataHandler(fds));
mbp2.setFileName(filesToBeAttached[i]);
mp.addBodyPart(mbp2);
}
]]>
// add the Multipart to the message
msg.setContent(mp);
}
Transport.send(msg);
return "successfully sent email";
} catch (MessagingException mex) {
mex.printStackTrace() ;
}
return "unsuccessful to send email";
}
</xsp:logic>
--------------------------------
hope this helps you..
Nishit
-----Original Message-----
From: Supreme Being [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 11, 2001 2:24 PM
To: [EMAIL PROTECTED]
Subject: JavaMail with JSP
i succesfully downloaded the JavaMail and JAF from sun's site in order to
use them with my JSP which i'm running on JRun Studio on my win98 PC. i
added the CLASSPATH (in autoexec.bat) as told by the docs along with them.
Now what? coz nothing is working. I have this HTML page and a JSP page which
is supposed to send email...but it's not working... i'm getting a
java.SendMailException error. what do i have to do? is there something i
missed?
The codes i'm using are as follows :
[The HTML Page] : javamail.html
<FORM action="sendjavamail.jsp" method="post">
<TABLE>
<TR>
<TD width="50%"><font face="arial" color=#ffdead size=2>
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%"><font face="arial" color=#ffdead size=2>
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2"><font face="arial" color=#ffdead size=2>
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2"><font face="arial" color=#ffdead size=2>
<P>Message:<BR><TEXTAREA name="text" rows=5 cols=30></TEXTAREA></P>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
[The JSP Page] : javasendmail.jsp
<%@ page
import=" javax.mail.*, javax.mail.internet.*,
javax.activation.*,java.util.*"
%>
<html>
<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>
Tell me what i'm doing wrong? or if i have to do some other thing or use
some other codes....
Regards,
T. Edison jr.
------------------------------------------------------------
Are you a Hoboe? Hobnob at http://www.hoboe.com
Click here -> http://www.hoboe.com Global Mail Access
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets