Solved

Sending Email from Java Servlet

Posted on 2007-04-05
21
2,605 Views
Last Modified: 2013-11-24
Hi,

I would like to be able to send an email from my java servlet page. This email will contain the users details and a web site address.

Thanks,
0
Comment
Question by:p1598
  • 10
  • 3
  • 3
  • +2
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 59 total points
ID: 18856679
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18856686
0
 

Author Comment

by:p1598
ID: 18856702
I'm not sure what version of java I have so not sure if i need to download the mail program or not. How can I find this out?

Im on a mac.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:p1598
ID: 18856705
and using netbeans 5.5
0
 

Author Comment

by:p1598
ID: 18856770
I have it instaled ok but cannot get a few of the settings correct:


              Properties props = new Properties();
              props.setProperty("mail.transport.protocol", "smtp");
              props.setProperty("mail.host", "smtp.ntlworld.com");
              //props.setProperty("mail.user", "");
              //props.setProperty("mail.password", "");

These are commented out because I do not require a login to send smtp with ntl.


THanks,
0
 

Author Comment

by:p1598
ID: 18856772
The error is: exception: javax.mail.MessagingException: Could not connect to SMTP host: smtp.ntlworld.com, port: 25, response: 421
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18857033
It seems that port 25 of smtp.ntlworld.com is not opened...
0
 

Author Comment

by:p1598
ID: 18857294
Hi,

I checked my mail setting from my mail client and I send to it from port 25.

Is there anything else I can do? Do all my other settings look ok?

Thanks,
0
 
LVL 11

Assisted Solution

by:elfe69
elfe69 earned 58 total points
ID: 18857315
I guess you have to provide a user/password to the mail server. (otherwise, it would be that anyone can use that mail server to send mails...)
0
 

Author Comment

by:p1598
ID: 18858496
I have also tried it with another 2 mail servers:

             /props.setProperty("mail.transport.protocol", "smtp");
              props.setProperty("mail.host", "mail.domainNameHere.co.uk");
              props.setProperty("mail.user", "management+domainNameHere.co.uk");
              props.setProperty("mail.password", "password here");
             
              props.setProperty("mail.transport.protocol", "smtp");
              props.setProperty("mail.host", "postbox.domainHere.ac.uk");
              props.setProperty("mail.user", "userNameHere");
              props.setProperty("mail.password", "password here");

Thanks,


0
 

Author Comment

by:p1598
ID: 18858780
Really need some more help with this.
I just want to be able to send an email from java. I have both a standard SMTP server through ntl and also a private smtp auth server.

Thanks,


(Points have been increased)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18858940
What error are you getting?
0
 

Author Comment

by:p1598
ID: 18859093
The error is: exception: javax.mail.MessagingException: Could not connect to SMTP host: smtp.ntlworld.com, port: 25, response: 421

and

exception: javax.mail.SendFailedException: Invalid Addresses; nested exception is: class javax.mail.SendFailedException: 571 Remote sending only allowed with authentication!


The second one is for a server that requires a username password which I had entered like so:
props.setProperty("mail.user", "userNameHere");
              props.setProperty("mail.password", "password here");

Thanks!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 58 total points
ID: 18860696
You need to use this code: http://www.exampledepot.com/egs/javax.mail/SendApp.html?l=new

The mail.smtp.host and mail.smtp.port values need to be set.

Download the Java Beans activation framework for that from: http://java.sun.com/products/javabeans/jaf/downloads/index.html

Java mail download from: http://java.sun.com/products/javamail/downloads/

If your server needs authentication, you need to give a user-ID and password:

public class MyAuthenticator extends Authenticator {
    private PasswordAuthentication obj ;
   
    public MyAuthenticator () {
        obj = new PasswordAuthentication ( "yourUserName", "yourPassword" ) ;
    }
   
    public PasswordAuthentication getPasswordAuthentication () {
        return obj ;
    }
}

In the code:

>> Session session = Session.getDefaultInstance(props, null);

becomes: Session session = Session.getDefaultInstance ( props, new MyAuthenticator () ) ;
0
 

Author Comment

by:p1598
ID: 18863022
Hi,

Thanks for the response. Got 2 errors while setting this up. Hopefully you can help.

1. The line: Session session = Session.getDefaultInstance ( props, new MyAuthenticator () ) ;
non-static variable this cannot be referenced from a static context

2. The authenticator method errors:

reference to Authenticator is ambiguous, both class javax.mail.Authenticator in javax.mail and class java.net.Authenticator in java.net match


Thanks,
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18866189
>> reference to Authenticator is ambiguous, both class javax.mail.Authenticator in javax.mail

Make it: public class MyAuthenticator extends javax.mail.Authenticator {

>> non-static variable this cannot be referenced from a static context

You are calling this in a static method? If so, you cannot access data members of the class. You need to define props as static too, I guess - in case you have not defined it as static within the class (or local to the method).

Can we see the complete code? that will pin point where the error is
0
 

Author Comment

by:p1598
ID: 18867223
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

//servlet html here
try{
              send("mail.myDomain.co.uk", 25, "management@myDomain.co.uk", "petehinds@gmail.com","Test", "Test Message");
            }catch  (Exception ex){out.println("exception: "+ex);}
}
 public void send(String smtpHost, int smtpPort,String from, String to, String subject, String content)
                throws AddressException, MessagingException {
            // Create a mail session
            java.util.Properties props = new java.util.Properties();
            props.put("mail.smtp.host", smtpHost);
            props.put("mail.smtp.port", ""+smtpPort);
            //Session session = Session.getDefaultInstance(props, null);
            Session session = Session.getDefaultInstance ( props, new MyAuthenticator () ) ;

            // Construct the message
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject(subject);
            msg.setText(content);
   
            // Send the message
            Transport.send(msg);
        }
   
    public class MyAuthenticator extends javax.mail.Authenticator {
    private javax.mail.PasswordAuthentication obj ;
   
    public MyAuthenticator () {
        obj = new javax.mail.PasswordAuthentication ( "management+myDomain.co.uk", "passwordHere" ) ;
    }
   
    public javax.mail.PasswordAuthentication getPasswordAuthentication () {
        return obj ;
    }

Thanks,
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 19010823
I don't see any static methods there - which line gave you the error?
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20143933
Forced accept.

Computer101
EE Admin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
tomcat not starting 6 45
hibernate example issues from command prompt 10 40
jsp login check 12 28
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question