Solved

Sending Email from Java Servlet

Posted on 2007-04-05
21
2,603 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 103
rhino JavaScript import, load 25 69
xampp tool 12 31
configure dependency in POM for new database 3 18
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now