我正在尝试使用SQL Server sp_send_dbmail
从java发送电子邮件从SQL Server本身,以下工作正常,可以发送HTML电子邮件。 [即我已正确设置所有内容以发送电子邮件 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';
但是在尝试使用以下代码从java发送时
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;
}
我收到以下异常com.microsoft.sqlserver.jdbc.SQLServerException: com.microsoft.sqlserver.jdbc.SQLServerException: Parameter profile_name was not defined for stored procedure
我做错了什么?
答案 0 :(得分:1)
请将您的查询字符串更改为使用call
,如下所示:
final String sendEmailStr "{ call msdb.dbo.sp_send_dbmail (?,?,?,?,?,?) }";
要使用当前数据库中存储的proc,请在当前数据库中执行此操作,以通过本地过程包装MSDB调用:
Use [MyDatabase];
CREATE PROCEDURE [MyDB].[SEND_EMAIL_DB]
@profile_name2 varchar(max),
@recipients2 varchar(max),
@body2 varchar(max),
@body_format2 varchar(max),
@subject2 varchar(max),
@p_result int OUTPUT
AS
BEGIN
exec msdb.dbo.sp_send_dbmail @profile_name=@profile_name2,
@recipients=@recipients2,@body=@body2,
@body_format=@body_format2,@subject=@subject2
END