[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2644
  • Last Modified:

Sending Email from Java Servlet

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
p1598
Asked:
p1598
  • 10
  • 3
  • 3
  • +2
3 Solutions
 
p1598Author Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
p1598Author Commented:
and using netbeans 5.5
0
 
p1598Author Commented:
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
 
p1598Author Commented:
The error is: exception: javax.mail.MessagingException: Could not connect to SMTP host: smtp.ntlworld.com, port: 25, response: 421
0
 
elfe69Commented:
It seems that port 25 of smtp.ntlworld.com is not opened...
0
 
p1598Author Commented:
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
 
elfe69Commented:
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
 
p1598Author Commented:
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
 
p1598Author Commented:
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
 
CEHJCommented:
What error are you getting?
0
 
p1598Author Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
p1598Author Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
 
p1598Author Commented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
I don't see any static methods there - which line gave you the error?
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 10
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now