【问题标题】:Sending email from java using SQL Server sp_send_dbmail使用 SQL Server sp_send_dbmail 从 java 发送电子邮件
【发布时间】:2012-10-24 18:09:22
【问题描述】:

我正在尝试使用 SQL Server sp_send_dbmail 从 java 发送电子邮件

在 SQL Server 内部,以下内容可以正常发送 HTML 电子邮件。 [IE。我已经正确设置了发送电子邮件的所有内容 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

我做错了什么?

【问题讨论】:

    标签: java sql-server-2012 email


    【解决方案1】:

    请将您的查询字符串更改为使用call,如下所示:

     final String sendEmailStr "{ call msdb.dbo.sp_send_dbmail (?,?,?,?,?,?) }";
    

    要使用当前数据库中的存储过程,请在当前数据库中执行此操作以通过本地过程包装 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
    

    【讨论】:

    • 我收到了com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'.
    • 然后我尝试从 sendEmailStr 中删除大括号并得到Exception on sendEmail com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'。
    • 如果我使用索引,计算机怎么知道哪个参数去哪里了。授予 profile_Name 是第一个参数,但 body 不是第三个参数而是technet.microsoft.com/en-us/library/ms190307.aspx中的第八个参数
    • @gordon613:这是@PO。我在代码中没有看到。我得到了索引/名称的东西,但你已经在使用索引来注册参数了。
    • 见stackoverflow.com/questions/7038818/… 似乎是根据米奇的rowcount参数
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多