Results 1 to 3 of 3
  1. #1
    anthrax is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default email servlet ( Connection reset)

    im making a web form that will submit entries and send data to my yahoo email so i found the codes below but when i try to run the program from web form i get error below can u help me pls:

    "There was a problem sending the email!

    javax.mail.MessagingException: Exception reading response;
    nested exception is: Connection reset


    here is the code below:

    public class EmailServletattachments extends HttpServlet {

    private static final String mailserver = "";
    private static final String correctPassword = "xxx";
    private String from, to, password, filename;
    private String cc, bcc, subject, body;
    private String warning = "\n\n\nWarning! This mail is sent by a Web form. Sender could be invalid!";

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    ServletOutputStream out = res.getOutputStream();
    File dir = (File)getServletContext().getAttribute("javax.serv let.context.tempdir");
    MultipartRequest mR = new MultipartRequest(req, dir.getAbsolutePath(), 1000000);

    Enumeration e = mR.getParameterNames();
    while (e.hasMoreElements()) {
    String key = (String)e.nextElement();
    if(key.equals("from")) {
    from = mR.getParameter(key);
    if(key.equals("to")) {
    to = mR.getParameter(key);
    if(key.equals("cc")) {
    cc = mR.getParameter(key);
    if(key.equals("bcc")) {
    bcc = mR.getParameter(key);
    if(key.equals("subject")) {
    subject = mR.getParameter(key);
    if(key.equals("message")) {
    body = mR.getParameter(key);
    if(key.equals("password")) {
    password = mR.getParameter(key);

    if(password.equals(correctPassword)) {
    try {
    // Get system properties
    Properties props = System.getProperties();

    // Set up the mail server
    props.put("", mailserver);

    // Get the email session
    Session session = Session.getDefaultInstance(props, null);

    // Define the message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    new InternetAddress(to));
    if(cc !=null && cc.length() != 0) {
    new InternetAddress(cc));
    if(bcc !=null && bcc.length() != 0) {
    new InternetAddress(bcc));

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(body += warning);
    Multipart multipart = new MimeMultipart();

    //Attached files
    Enumeration files = mR.getFileNames();
    while(files.hasMoreElements()) {
    String name = (String)files.nextElement();
    filename = mR.getFilesystemName(name);
    String type = mR.getContentType(name);
    File file = mR.getFile(name);
    if(file != null) {
    String attachment = dir.getAbsolutePath()+"\\"+filename;
    messageBodyPart = new MimeBodyPart();
    DataSource source = new FileDataSource(attachment);
    messageBodyPart.setDataHandler(new DataHandler(source));
    message.setSentDate(new Date());
    out.println("************Message Sent!************\n\n");

    Enumeration headerLines = message.getAllHeaderLines();
    while(headerLines.hasMoreElements()) {
    out.println("Message: " + body);
    catch(Exception ex) {
    out.println("There was a problem sending the email!\n\n" + ex);
    else {
    out.println("Bad password, go back and try again!");
    } // End class EmailServletAttachments
    Last edited by anthrax; 01-23-2010 at 05:43 AM.

  2. #2
    anthrax is offline Member
    Join Date
    Feb 2009
    Rep Power


    i need a way to send email without having smtp server installed on my pc.. i've been on this task for 3 days now searching on web but all samples i've seen uses local server

  3. #3
    FON is offline Senior Member
    Join Date
    Dec 2009
    Belgrade, Serbia
    Rep Power


    "i need a way to send email without having smtp server installed on my pc"

    U can use your ISP's smtp or google smtp...

    If AUTH is needed you must provide your accounts details - username
    and password (gmail account or your regular email account at yours ISP)

    It's easier if smtp does not require AUTH, so try first your ISP.

    First use 'telnet' to see if network part of problem exists.

    Java Code:
     telnet 25

    If you are connected type HELP and play around with commands
    smtp offers you, it's easy. Then write code and test it.

    Always use :

    session.setDebug(true); // Verbose!

    to see result on console and understand what server is telling you.

    For google smtp and AUTH u must use
    and set username and pass,
    and set instance of Authenticator in Session.

    it's peace of cake don't worry

    good luck!

Similar Threads

  1. Replies: 3
    Last Post: 05-22-2011, 04:16 PM
  2. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 05:12 PM
  3. Connection reset error in FTP client
    By Waterbottle in forum Networking
    Replies: 0
    Last Post: 03-23-2009, 10:03 AM
  4. Replies: 5
    Last Post: 03-13-2009, 01:45 PM
  5. Replies: 2
    Last Post: 01-15-2009, 10:01 PM

Posting Permissions

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