Solved

Sending Email from Java Servlet

Posted on 2007-04-05
21
2,598 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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:mayankeagle
mayankeagle 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:mayankeagle
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:mayankeagle
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
Eclipse Neon and jdk 1.8.0 11 66
thymeleaf natural templating vs JSP 2 24
github account with ecipse 1 16
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…
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…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

11 Experts available now in Live!

Get 1:1 Help Now