I am trying to send email from java using SQL Server sp_send_dbmail
From within SQL Server itself the following works fine to send an HTML email. [i.e. I have set everything up correctly for sending emails as per Microsoft's instructions ]
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'example profile',
@recipients = 'me@example.com',
@body = @tableHTML,
@body_format = 'HTML' ,
@subject = 'Email from SQL Server';
However when trying to send from java using the following code
public static synchronized int sendEmail(String profileName,String recipients,String body, String body_format,String subject) {
final String sendEmailStr = "{execute msdb.dbo.sp_send_dbmail (?,?,?,?,?,?)}";
dbConn = DBConnection.getInstance();
Connection conn = dbConn.getConnection();
CallableStatement stmt = null;
int result = RESULT_FAILED;
try {
stmt = conn.prepareCall(sendEmailStr);
stmt.setString("profile_name", profileName);
stmt.setString("recipients", recipients);
stmt.setString("body", body);
stmt.setString("body_format", body_format);
stmt.setString("subject", subject);
stmt.registerOutParameter(6, java.sql.Types.INTEGER);
stmt.execute();
result = stmt.getInt(6);
} catch(SQLException e) {
System.out.println(e);
Log.getInstance().write("Exception on sendEmail " + e.toString());
} finally {
try {
stmt.close();
dbConn.returnConnection(conn);
} catch(SQLException e) {
System.out.println(e);
Log.getInstance().write("Exception on sendEmail " + e.toString());
}
}
return result;
}
I get the following exception com.microsoft.sqlserver.jdbc.SQLServerException: com.microsoft.sqlserver.jdbc.SQLServerException: Parameter profile_name was not defined for stored procedure
What am I doing wrong?