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

    Default email servlet ( java.net.SocketException: 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:
    java.net.SocketException: Connection reset

    "


    here is the code below:



    public class EmailServletattachments extends HttpServlet {

    private static final String mailserver = "mail.yahoo.com";
    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 {
    res.setContentType("text/plain");
    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("mail.smtp.host", mailserver);

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

    // Define the message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO,
    new InternetAddress(to));
    if(cc !=null && cc.length() != 0) {
    message.addRecipient(Message.RecipientType.CC,
    new InternetAddress(cc));
    }
    if(bcc !=null && bcc.length() != 0) {
    message.addRecipient(Message.RecipientType.BCC,
    new InternetAddress(bcc));
    }
    message.setSubject(subject);

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

    //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));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
    }
    }
    message.setContent(multipart);
    message.setSentDate(new Date());
    Transport.send(message);
    out.println("************Message Sent!************\n\n");

    Enumeration headerLines = message.getAllHeaderLines();
    while(headerLines.hasMoreElements()) {
    out.println((String)headerLines.nextElement());
    }
    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 04:43 AM.

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

    Default

    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
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    "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 stmp.someserver.com 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
    javax.mail.Authenticator
    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, 03:16 PM
  2. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 04:12 PM
  3. Connection reset error in FTP client
    By Waterbottle in forum Networking
    Replies: 0
    Last Post: 03-23-2009, 09:03 AM
  4. Replies: 5
    Last Post: 03-13-2009, 12:45 PM
  5. Replies: 2
    Last Post: 01-15-2009, 09: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
  •