Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Servlets
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Servlets
email not triggering
monis raza
Ranch Hand
Posts: 38
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
here is my code to trigger email.
when i am running email file as
java
application it is working fine but when i am adding it to
servlet
email is not triggering. can anyone tell me if my servlet is ok to access the code in model
package com.controller; import java.io.*; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.TestJavaMail; /** * Servlet implementation class emailservice */ public class emailservice extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); System.out.println("here"); PrintWriter writer = res.getWriter(); writer.println("HELLO WORLD"); writer.println(req.getRemoteHost()); String userid = getServletConfig().getInitParameter("userid"); //String value = getInitParameter("param1"); String password = getServletConfig().getInitParameter("password"); writer.println(userid); TestJavaMail t1 = new TestJavaMail(userid, password); t1.process(); writer.println("\n bhej di "); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
package model; import java.util.Date; import java.util.Properties; import javax.mail.*; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.naming.InitialContext; import javax.naming.NamingException; import com.sun.xml.internal.bind.CycleRecoverable.Context; public class TestJavaMail { String userid ; String password; public TestJavaMail(String userid , String password) { this.userid = userid; this.password =password; } /* public static void main(String args[]) { } */ public void process() { String[] to = {"faiz.akhtar@agnitio-technologies.com","manish.kaushik@agnitio-technologies.com", "sandeep.sharma@agnitio-technologies.com"}; Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "smtp.exchangemails.com"); props.put("mail.smtp.port", 25); props.put("mail.smtp.user", userid); props.put("mail.smtp.pass", password); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth ", "true"); Authenticator auth = new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userid, password); } }; Session session = Session.getDefaultInstance(props, auth); try { MimeMessage msg = new MimeMessage(session); System.out.println("Mimemessage ceated"); InternetAddress[] iaFrom = { new InternetAddress( "moonis.raza@agnitio-technologies.com") }; msg.setFrom(iaFrom[0]); InternetAddress[] iaTo = new InternetAddress[to.length] ; for(int i=0;i<to.length;i++) { iaTo[i] = new InternetAddress(to[i]); msg.addRecipient(Message.RecipientType.TO, iaTo[i]); } msg.setSubject("Test Java Mail"); msg.setSentDate(new Date()); msg.setText("Hello, Congrats - It is working\n pleases send acknowledgement mail to senderof you get this" + "\n as it is part of project"); Transport tran = session.getTransport("smtp"); System.out.println("Transport object created......"); //tran.setStarttls(true); tran.connect("smtp.exchangemails.com", 25, "moonis.raza@agnitio-technologies.com", "welcome"); //tran.connect(); msg.saveChanges(); System.out.println("Connect succeeded"); tran.sendMessage(msg, msg.getAllRecipients()); tran.close(); System.out.println("Mail Sent Successfully"); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); } } }
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How is the servlet invoked? is it with GET or POST request.
I see doPost() blank. Is it intentional?
Also, how about writing syslogs to see where the flow is going?
Tel us, what you have done till now, to DEBUG this?
Marek Krokosinski
Ranch Hand
Posts: 65
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Try with:
Session session = Session.getInstance(props, auth);
instead of:
Session session = Session.getDefaultInstance(props, auth);
sai rama krishna
Ranch Hand
Posts: 930
2
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
getDefaultInstance Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.
Therefore, if one does not already exist, it call getInstance()
getInstance Get a new Session object.
So, a new session object is created, regardless of whether one already exists.
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
SendFailedException for sending simple email by using JavaMail
SHA1 digest error for javax/mail/MessagingException
How to send mail form java application
Auto Generated Email using Java
Java Mail Authentication
More...