File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Code to send subject & CC in mail system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Code to send subject & CC in mail system " Watch "Code to send subject & CC in mail system " New topic
Author

Code to send subject & CC in mail system

jahannu shah
Greenhorn

Joined: Dec 10, 2007
Posts: 2
HI,
I HAVE MADE A PROGRAM IN SERVLETS FROM WHERE I AM TO SEND A MAIL TO THE USER BUT I AM NOT ABLE TO SEND THE SUBJECT & CC FROM THE PROGRAM REST ALL WORK FINE I AM ABLE TO SEND AN EMAIL ONLY TO ONE PERSON. PLEASE HELP ME WITH THE CODE I M POSTING MY PROGRAM BELOW

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.*;
public class Callreg extends HttpServlet
{
public void sendEmail( String from, String to, String CC, String message)
{
try
{
// get a socket connection to the mail
// server at SMTP port 25
Socket socket = new Socket("192.168.0.20", 25);

// Create an output stream for sending message
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// send mail using SMTP protocol
out.println("MAIL FROM: " + from);
out.println("RCPT TO: " + to);
out.println("RCPT CC: " + CC);
out.println("DATA\n"); // Skip line after DATA
out.println(message);
out.println("."); // End message with a single period
out.flush();
}
catch (Exception e)
{
System.out.println("Failed to send email: " + e);
}
}
private static final long serialVersionUID = 3203389779404308723L;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String Ticket_id = request.getParameter("Ticket_id");
String Emp_id = request.getParameter("Emp_id");
String Email_id = request.getParameter("Email_id");
String Location = request.getParameter("Location");
String Desk_no = request.getParameter("Desk_no");
String Pc_no = request.getParameter("Pc_no");
String Ext_no = request.getParameter("Ext_no");
String Call_type = request.getParameter("Call_type");
String Description = request.getParameter("Description");
DateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date date = new java.util.Date();
String dt =dateFormat.format(date);
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
// Database connection query

try{
String url = "jdbc dbc:Tickethardware";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url,"","");
Statement st = conn.createStatement();
String sql = "insert into TicketRegistrations values(" + Ticket_id+"," + Emp_id +",'" + Email_id+"','" + Location+"',"
+ Desk_no+"," + Pc_no +"," + Ext_no + ",'" + Call_type+ "','" + Description + "','Pending','" +
dt +"'," + null +"," + null +"," + null +"," + null +")";
int rowCount = st.executeUpdate(sql);
if(rowCount!=0){
out.print("<br><br> Your request has been registered successfully. ");
{
String from = "jahannu.shah@aurionprosolutions.com"; //request.getParameter("from");
String to = request.getParameter("Email_id");
String CC = "jahannu.shah@aurionprosolutions.com";
String message = "Your call has been registered successfully. Your Ticket id is : " + Ticket_id;
sendEmail(from, to, CC, message);
out.println("An Email has been sent to your mail account " + to);
}

//Send a mail from sysadmin to user body ="Hi !" + user + " your call has been successfully registered with sysadmin " + ticket id
}
else{
out.println("Sorry! Failure");
}

System.out.println("rowCount=" + rowCount);
// Connection closed
conn.close();
}catch(Exception e)
{
out.print(e.toString());
}

out.flush();
out.close();
}

}
I M ACTUALLY PREPARING A PROGRAM ON CALLLOG WHERE USER CAN ENTER A REQUEST REGARDING A PROBLEM TO SYS ADMIN GUY. THEN AN AUTOMAIL IS RE ROUTED TO HIS ACCOUNT GIVING HIM HIS TICKET ID.
THANK YOU
[ January 18, 2008: Message edited by: Ben Souther ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Welcome to JavaRanch.

In order to get the most out of the Saloon we have prepared a page with tips on how to ask question: HowToAskQuestionsOnJavaRanch. In particular, have a look at UseCodeTags and KeepItDown.

Is there a particular reason you're not using the JavaMail API (which is the usual way to send email with Java)? You're sort of reinventing the wheel by writing your own code to deal with SMTP. Also, Don't println to a Socket.
[ January 18, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Code to send subject & CC in mail system
 
Similar Threads
sending email
Relay access denied
auto-generate new ticket on each page
Please Help, why this servlet is not working on mycgiserver
IO Exception :Connection timed out