Results 1 to 3 of 3

Thread: Sending Emails...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Smile Sending Emails...

    Any body know of a way to send an email to someone through a servlet?

    Thanks
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    New Member
    Join Date
    Sep 2001
    Posts
    4
    U have to set the class path of two jar files Mail.jar,Activation.jar


    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class MySendMail extends HttpServlet {

    private String smtpHost;

    // smtpHost is used for storing hostname of the SMTP server

    public void init(ServletConfig config) throws ServletException {
    super.init(config);

    smtpHost = "203.200.51.3";
    /* use your mail server name or ip-address */
    }

    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, java.io.IOException {
    String from = req.getParameter("from");
    String to = req.getParameter("to");
    String cc = req.getParameter("cc");
    String subj = req.getParameter("subject");
    String txt = req.getParameter("text");

    String status;

    try {

    java.util.Properties properties = System.getProperties();
    properties.put("mail.smtp.host", smtpHost);
    Session session = Session.getInstance(properties, null);
    // Construct the message
    MimeMessage msg = new MimeMessage(session);

    // For Setting the from address
    Address frmAddress = new InternetAddress(from);
    msg.setFrom(frmAddress);
    // Parse and set the recipient addresses
    Address[] toAddresses = InternetAddress.parse(to);

    msg.setRecipients(Message.RecipientType.TO,toAddresses);
    Address[] ccAddresses = InternetAddress.parse(cc);
    msg.setRecipients(Message.RecipientType.CC,ccAddresses);

    // Set the subject and text
    msg.setSubject(subj);
    msg.setText(txt);
    Transport.send(msg);
    status = "Your message was sent.";
    }
    catch (AddressException e) {
    status = "There was an error parsing the addresses.";
    }
    catch (SendFailedException e) {
    status = "There was an error sending the message.";
    }
    catch (MessagingException e) {
    status = "There was an unexpected error.";
    }

    // Output a status message
    res.setContentType("text/html");
    java.io.PrintWriter writer = res.getWriter();
    writer.println("<html><head><title>Status</title></head>");
    writer.println("<body><p>"+ status +"</p></body></html>" );
    writer.close();
    }
    }
    Bhappi

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    anybody have those jar files? I cant find them on my system.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width