Click here to Skip to main content
16,012,153 members
Home / Discussions / Java
   

Java

 
QuestionSOS Pin
Boukhezna18-Mar-12 4:09
Boukhezna18-Mar-12 4:09 
AnswerRe: SOS Pin
Boukhezna18-Mar-12 5:10
Boukhezna18-Mar-12 5:10 
QuestionRe: SOS Pin
Boukhezna18-Mar-12 5:32
Boukhezna18-Mar-12 5:32 
AnswerRe: SOS Pin
jschell18-Mar-12 7:33
jschell18-Mar-12 7:33 
GeneralRe: SOS Pin
Boukhezna18-Mar-12 8:02
Boukhezna18-Mar-12 8:02 
GeneralRe: SOS Pin
Richard MacCutchan18-Mar-12 22:46
mveRichard MacCutchan18-Mar-12 22:46 
GeneralRe: SOS Pin
jschell19-Mar-12 8:32
jschell19-Mar-12 8:32 
Questionjava Pin
Member 857496415-Mar-12 6:51
Member 857496415-Mar-12 6:51 
XML
hii I have done coding on netbeans 6.9 in java for sending e-mail after downloading javamailer nd varios jar files bt the when the project is run the mail is not sending.I think it me a virtual server problem.I have window 7 64 bit how can I create virtual server.If there is another problem.Plz tell me..the code is as follows

index.jsp

 Collapse | Copy Code
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title> Java Mail </title>
    </head>
    <body>
        <form action="sendMail.jsp" method="POST">
            <table border="0" align="center" cellpadding="5">
                <tbody>
                    <thead><tr> <td colspan="3" align="center">
                     Send Mail  </td> </tr> </thead>
                    <tr>
                        <td> To </td> <td> : </td>
                        <td> <input type="text" name="to" value="" /> </td>
                    </tr>
                    <tr>
                        <td> Subject </td> <td> : </td>
                        <td> <input type="text" name="subject" value="" /> </td>
                    </tr>
                    <tr>
                        <td> Message </td> <td> : </td>
                        <td> <textarea name="message" rows="8" cols="30">
                        </textarea></td>
                    </tr>
                    <tr>
                        <td colspan="3" align="center">
                        <input type="submit" value="Send Mail" />

                        <input type="reset" value="Reset" />
                        <td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

Mail.java
 Collapse | Copy Code

package jMail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class Mail
{
private String to;
    private String from;
    private String message;
    private String subject;
    private String smtpServ;

    /**
     * @return the to
     */
    public String getTo() {
        return to;
    }

    /**
     * @param to the to to set
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * @param from the from to set
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the smtpServ
     */
    public String getSmtpServ() {
        return smtpServ;
    }

    /**
     * @param smtpServ the smtpServ to set
     */
    public void setSmtpServ(String smtpServ) {
        this.smtpServ = smtpServ;
    }
public int sendMail(){
        try
        {
            Properties props = System.getProperties();
              // -- Attaching to default Session, or we could start a new one --
              props.put("mail.transport.protocol", "smtp" );
              props.put("mail.smtp.starttls.enable","true" );
              props.put("mail.smtp.host",smtpServ);
              props.put("mail.smtp.auth", "true" );
              Authenticator auth = new SMTPAuthenticator();
              Session session = Session.getInstance(props, auth);
              // -- Create a new message --
              Message msg = new MimeMessage(session);
              // -- Set the FROM and TO fields --
              msg.setFrom(new InternetAddress(from));
              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
              msg.setSubject(subject);
              msg.setText(message);
              // -- Set some other header information --
              msg.setHeader("MyMail", "Mr. XYZ" );
              msg.setSentDate(new Date());
              // -- Send the message --
              Transport.send(msg);
              System.out.println("Message sent to"+to+" OK." );
              return 0;
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
          System.out.println("Exception "+ex);
          return -1;
        }
  }
private class SMTPAuthenticator extends javax.mail.Authenticator {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            String username =  "Java.Mail.CA@gmail.com";           // specify your email id here (sender's email id)
            String password = "javamail";                                      // specify your password here
            return new PasswordAuthentication(username, password);
        }
  }
}

Sendmail.jsp

 Collapse | Copy Code

<pre lang="xml"><%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="mail" scope="session" class="jMail.Mail" />
<jsp:setProperty name="mail" property="to" param="to" />
<jsp:setProperty name="mail" property="from" value="Java.Mail.CA@gmail.com" />
<jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
<jsp:setProperty name="mail" property="subject" param="subject" />
<jsp:setProperty name="mail" property="message" param="message" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
       <%
String to = mail.getTo();
int result;
result = mail.sendMail();
if(result == 0){
    out.println(" Mail Successfully Sent to "+to);
}
else{
    out.println(" Mail NOT Sent to "+to);
}
%>

    </body>
</html

AnswerRe: java - Repost Pin
Richard MacCutchan15-Mar-12 7:01
mveRichard MacCutchan15-Mar-12 7:01 
AnswerRe: java Pin
altafmohd15-Mar-12 7:03
altafmohd15-Mar-12 7:03 
GeneralRe: java Pin
jschell15-Mar-12 8:11
jschell15-Mar-12 8:11 
AnswerRe: java Pin
jschell15-Mar-12 8:14
jschell15-Mar-12 8:14 
QuestionXMPP File Transfer Pin
Praveen Singh Rajpoot 15-Mar-12 2:44
Praveen Singh Rajpoot 15-Mar-12 2:44 
AnswerRe: XMPP File Transfer Pin
Richard MacCutchan15-Mar-12 4:10
mveRichard MacCutchan15-Mar-12 4:10 
QuestionWifi LAN connection with java Pin
Member 805479114-Mar-12 1:18
Member 805479114-Mar-12 1:18 
AnswerRe: Wifi LAN connection with java Pin
Nagy Vilmos14-Mar-12 3:39
professionalNagy Vilmos14-Mar-12 3:39 
AnswerRe: Wifi LAN connection with java Pin
jschell14-Mar-12 8:42
jschell14-Mar-12 8:42 
QuestionJavaAccessBridge setTextContents returns false Pin
Bernhard Hiller11-Mar-12 20:47
Bernhard Hiller11-Mar-12 20:47 
AnswerRe: JavaAccessBridge setTextContents returns false Pin
TorstenH.11-Mar-12 22:42
TorstenH.11-Mar-12 22:42 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
Bernhard Hiller11-Mar-12 23:52
Bernhard Hiller11-Mar-12 23:52 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
Richard MacCutchan12-Mar-12 0:23
mveRichard MacCutchan12-Mar-12 0:23 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
Bernhard Hiller12-Mar-12 0:39
Bernhard Hiller12-Mar-12 0:39 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
TorstenH.12-Mar-12 1:37
TorstenH.12-Mar-12 1:37 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
Bernhard Hiller12-Mar-12 4:30
Bernhard Hiller12-Mar-12 4:30 
GeneralRe: JavaAccessBridge setTextContents returns false Pin
jschell12-Mar-12 9:04
jschell12-Mar-12 9:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.