您好,我希望您能帮助我,我正在使用来自 SQL SERVER 的 SQL MAIL 发送邮件,生成的邮件确实到达了,但现在我想附加一个图像,我有一个表,其中有一个存储格式图像的字段IMAGE
.
当我尝试插入图像并在我的过程中执行它时,我收到一个错误:
我生成电子邮件的存储过程代码是这样的:
alter table mail
if exists(select * from TTareasAtender where comentario='' and oficina=9)
begin
Declare @CampoSTOT7 varchar(max)
Declare @NReg7 int
Declare @NRegTot7 int
IF OBJECT_ID('DBProAuxQP_QA..#temp7') IS NOT NULL
BEGIN
DROP TABLE DBProAuxQP_QA..#temp7
END
else
Select ROW_NUMBER() OVER (ORDER BY NumeroRecibo) as NN,* into #temp7 from TTareasAtender where comentario='' and oficina=9
--select * from #temp1
Set @NReg7=0
Set @NRegTot7= (select count(*) as Cantidad from #temp7 where comentario='' and oficina=9 and NN=@NReg6)
set @CampoSTOT7='0'
WHILE @NReg7<=@NRegTot7
begin
set @NReg7= @NReg7 + 1
Declare @cantidad7 as varchar(10),@Asunto7 as Varchar(500), @Mensaje7 as Varchar(500),@Tarea7 as varchar(500),@agencia7 as varchar(160),@img as varbinary(max)
Set @cantidad7= (select count(*) as Cantidad from #temp7 where comentario='' and oficina=9 and NN=@NReg7)
set @agencia7 =(select b.oficina as Agencia from #temp7 a inner join TTareasOficina b on a.oficina=b.idoficina where a.comentario='' and a.oficina=9 and NN=@NReg7)
--set @FVR3=(select fechacumplimiento from #temp3 where IDEstadoTarea=3 and DiasFaltantes=3 and AgenciaID=7 and diasfaltantes is not null and NN=@NReg2)
set @Tarea7 = (select tarea from #temp7 where comentario='' and oficina=9 and NN=@NReg7)
set @Asunto7 = @Tarea7
set @img=(select filecontent from TTareasAtender where oficina=9 and registro=1)
set @tableHTML=
'<style type="text/css">
body
{
font: 70.5% Tahoma;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
.TableFrm {
width:75%;
font: 12px Arial, Helvetica, sans-serif;
align-content:center;
margin: 0 auto;
}
.TableFrm th {
border:1px solid #E0E0E0;
background:#F5F5F5;
padding:6px;
font-weight:bold;
text-align:left;
}
.TableFrm td {
border:1px solid #E0E0E0;
background:#FFF;
padding:6px;
text-align:left;
}
.table-hover tbody tr:hover td,
.table-hover tbody tr:hover th {
background-color: #f5f5f5;
}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>
.: INFORME DE REGISTRO DE TAREA | FINANCIERA QAPAQ S.A :.
</title>
<body >
<form method="post" id="form1">
<table class="TableFrm">
<tr>
<th style="width: 100%" colspan="4" >..:: INFORME DE REGISTRO DE TAREA | FINANCIERA QAPAQ S.A::..</th>
</tr>
<tr>
<th style="width: 30%">Tipo de Tarea: </th>
<td style="width: 70%" colspan="3">'+@Tarea7+'</td>
</tr>
<tr>
<th style="width: 30%">Estado Tarea: </th>
<td style="width: 70%" colspan="3">REGISTRO DE TAREAS</td>
</tr>
<tr>
<th style="width: 30%">Cantidad: </th>
<td style="width: 70%" colspan="3">'+@cantidad7+'</td>
</tr>
<tr>
<th style="width: 30%">Agencia: </th>
<td style="width: 70%" colspan="3">'+@agencia7+'</td>
</tr>
<tr>
<th style="width: 30%">Área Responsable de Atención: </th>
<td style="width: 70%" colspan="3">LOGISTICA - ADMINISTRACIÓN</td>
</tr>
<tr>
<th style="width: 30%">imagen </th>
<img src="'+@img+'"/>
</tr>
<tr>
<th colspan="4">Informe del Seguimiento de Tareas Por Vencer</th>
</tr>
</table></br>
<table>
<tr><td class="PiePagina"> SISTEMA DE REGISTRO Y ATENCIÓN DE TAREAS, FINANCIERA QAPAQ S.A. - ©2017</td></tr>
</table>
</form>
</body> '
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Incidencias_Qapaq',--Perfil de correo configurado.
--@recipients = '[email protected]; [email protected] ; [email protected] ', -- A quien se va enviar el correo.
--@copy_recipients ='[email protected]',--'[email protected]; [email protected]', --Quien lo va a atender
@blind_copy_recipients='[email protected]',
@body =@tableHTML,-- 'Este correo es enviado desde SQL Server 2005 con código TSQL por sqlPsyKrest Blog',--Cuerpo del correo.
@body_format = 'HTML',
@subject = 'REGISTRO TAREA QAPAQ - ESTADO: RECIBO REGISTRADO: LOGISTICA - ADMINISTRACION' --@Asunto ,--Asunto del correo.
--@importance = 'High' --[Low | Normal| High] Prioridad del correo.
END
end
end
首先,让我们看看官方文档是怎么说的:
这只是作为一个注释,以防您将来想要更新 SQL Server 的版本。
同时,我提出了两种可能的解决方案:
解决方案1:
第一个选项是将图像存储到物理路径,然后将该路径包含在存储过程中以附加文件:
要将图像保存到物理路径:
通过电子邮件发送:
参考:
解决方案2:
由于您已将图像保存在数据类型
image
中,我们要做的第一件事就是将其转换为varbinary
,以便稍后将其集成到 HTML 的正文中:我们声明类型变量
VARBINARY
并分配以下内容image
:在 HTML 正文中,我们将标签
<img>
如下:您的问题出在这一行,这让我感到震惊:
用。。。来代替:
使用
varbinary(max)
代替image
:变量
varbinary(max)
可以作为图像参数的值传递。经验建议:当您开发邮件时,样式会内联,因为并非所有电子邮件客户端都呈现相同,请检查:https ://templates.mailchimp.com/resources/email-client-css-support/
版
替换:
经过
如果你在@img 变量中定义了一个路由,那么只要你在这个版本之前的代码版本中替换它就可以工作,即:
经过:
第 2 版
如果您没有可以查看图像的 URL,我建议您执行以下操作:
在 web 项目中,创建一个处理程序或控制器或 web 方法,当传递文件名时,它会呈现它,如下所示:
有了这个工作,在脚本中使用:
并检查在访问显示图像的链接时,它将工作并显示在发送的邮件中:D