[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Sending Email from Java Servlet

Posted on 2007-04-05
21
Medium Priority
?
2,641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 3
  • 3
  • +2
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 236 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 232 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 232 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

656 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