Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
May 23, 2012, 10:41:06 PM

Pages: [1]   Go Down
  Print  
Author Topic: Sending email from Java servlet  (Read 991 times)
trackonweb
Space Explorer
***
Offline Offline

Posts: 9


WWW
« on: August 17, 2002, 09:36:00 PM »

Thanks for your servlet support, our subdomains are working now.

We need to send emails from our java servlets to select users of our subdomain. I saw that u have a sendmail dir /usr/sbin/sendmail. Any support details available how to use sendmail from our java servlets??

Thanks
M.Srinivasan
Logged
Max
Über Jedi
*****
Offline Offline

Posts: 2534



WWW
« Reply #1 on: August 18, 2002, 02:26:00 PM »

There should be many rescources out there how to do this, if you wanna check the website of the maker of our servlet/jsp engine goto http://www.caucho.com

Or try to search for sendmail jsp in google.

Anyone have any direct tips on location where to find good jsp/servlet coding examples?
Logged
jblashil
Pong! (the videogame) Master
*****
Offline Offline

Posts: 23


WWW
« Reply #2 on: August 18, 2002, 07:51:00 PM »

hello,

well I guess to completely answer your question I need to know what exactly you want to do... ie do you want a make a form that a user can compose an email on or do you have a servlet processing stuff and you periodically want to send out emails?

in the first case, you can just as easily use the cgi sendmail script..

in the second case, depending on your java knowledge you may want to look into the javamail api..  

JavaMail API

Javamail allows you to do a whole lot of mail related functions through a fairly decent set of classes.. and you can always find example code out there.. in fact, if you let me know what exactly you want to do I could probably throw together some code pretty quickly for you.. (I just happen to be working on a javamail application right now  "[Wink]"  )

norbu
Logged
jblashil
Pong! (the videogame) Master
*****
Offline Offline

Posts: 23


WWW
« Reply #3 on: August 18, 2002, 08:29:00 PM »

I just wanted to add that if all you want to do is use a cgi script from your servlet you could do so by just creating the url manually and redirect your servlet to that url.. for example, the form creating in the user guide ->  FormMail could be "faked" out using the following code:

String recipient = "email@yourdomain.com";
String subject = "Feedback from web site";
String redirect = "http://www.yourdomain.com/thankyou.htm";

StringBuffer url = "http://www.yourdomain.com/cgi-sys/FormMail.cgi";
url.append("?recipient=");
url.append(recipient);
url.append("&subject=");
url.append(subject);
url.append("&redirect=");
url.append(redirect);

response.sendRedirect(url.toString());
Logged
trackonweb
Space Explorer
***
Offline Offline

Posts: 9


WWW
« Reply #4 on: August 22, 2002, 06:25:00 PM »

Our requirement is very close to what Norbu has given. We would like to use Javamail API.

Our java servlet has to send the same message to few of our customers periodically. Everything in the mail is same and we would like to just change the TO address and send the email to multiple receivers in a loop. The TO addresses are availabe in our mysql table and can be accessed by my servlet.

The email message may be different in diffrent programs.

We are new to javamail API. Would appreciate your sending us a sample code for a java servlet and to connect with javamail API.

Srini    "[Big
Logged
jblashil
Pong! (the videogame) Master
*****
Offline Offline

Posts: 23


WWW
« Reply #5 on: August 22, 2002, 08:53:00 PM »

Hello,

well I came up with a little example for you.. I would really recommend looking at the javamail user guide because likely the examples in there are better than mine.. but here is my example anyway.
code:
  
/*
 * SendExample.java
 *
 * Created on August 23, 2002, 9:07 AM
 */

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;

public class SendExample
{
    public static void send(String toString, String ccString, String bccString,
                      String fromString, String subjectString, String plainTextBody,
                      String smtpHost)
                    throws MessagingException
    {
        // get the Session
        Session mailSession;
        Properties properties = new Properties();
        properties.put("mail.smtp.host", smtpHost);
        mailSession = Session.getInstance(properties);
        mailSession.setDebug(false);
       
        // create the Message
        MimeMessage message = new MimeMessage(mailSession);
        InternetAddress fromAddress = new InternetAddress(fromString);
        message.setFrom(fromAddress);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toString));
        message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccString));
        message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccString));
        message.setSubject(subjectString);
        message.setText(plainTextBody);
        message.saveChanges();
       
        // connect to mail server and send message
        Transport transport = mailSession.getTransport("smtp");
        transport.connect();
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
   
    public static void main(String args[])
    {
        try
        {
            SendExample.send("you@yourdomain.com","","","me@mydomain.com",
"Hey!!","How's it going?  Long time no talk! \n\nHave a great day!\nMe",
"yourmailserver.com");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

Hope it helps!

Norbu
Logged
trackonweb
Space Explorer
***
Offline Offline

Posts: 9


WWW
« Reply #6 on: August 25, 2002, 05:26:00 PM »

Thanks for your sample program for sending mail thru Javamail API. We just downloaded Javamail API and are trying the sendmail program you sent.

Will getback to you after it works.
Logged
trackonweb
Space Explorer
***
Offline Offline

Posts: 9


WWW
« Reply #7 on: September 10, 2002, 11:50:00 PM »

Hello Norbu

Sorry we took a little longer. We have just got the Javamail API and coded the sendmail java code. We are not sure what should be the SMTP address for Jupiter server. Our domain is www.trackonweb.net and the subdomain for which we are coding sendmail is the "vote" .

The section of your code in which the smtp address is required is cut and pasted below.

code:
-------------------------------------------------
// get the Session        Session mailSession;        Properties properties = new Properties();        properties.put("mail.smtp.host", smtpHost);        mailSession = Session.getInstance(properties);        mailSession.setDebug(false);                
--------------------------------------------------
Can u pl clarify what is the smtpHost address?

Would appreciate feedback.
Logged
trackonweb
Space Explorer
***
Offline Offline

Posts: 9


WWW
« Reply #8 on: September 12, 2002, 02:00:00 PM »

In the control panel there is an item called

Sendmail Path        /usr/sbin/sendmail

Can we use this to send mails. Especially if we need only to send and not use all email functions?

Srini
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: