SQL CLR存储过程未能发送邮件SMTP,而Console Exe成功
我正在尝试实施CLR存储的过程,该过程将使用Office365 SMTP根据请求发送电子邮件。
这是我的代码:
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void SendMail(string recipients, string subject, string from, bool isHtml, string body, string smtpServer, int smtpPort, string smtpUser, string smtpPassword, string cc = "", string bcc = "")
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(from);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = isHtml;
mailMessage.Body = body;
string[] strTo = recipients.Split(';');
foreach (var item in strTo)
mailMessage.To.Add(new MailAddress(item));
if (cc != "")
{
string[] strCc = cc.Split(';');
foreach (var item in strCc)
mailMessage.CC.Add(new MailAddress(item));
}
if (bcc != "")
{
string[] strBcc = bcc.Split(';');
foreach (var item in strBcc)
mailMessage.Bcc.Add(new MailAddress(item));
}
SmtpClient smtpClient = new SmtpClient
{
Host = smtpServer,
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword),
TargetName = smtpServer,
EnableSsl = true
};
smtpClient.Send(mailMessage);
}
}
上面的代码在控制台应用程序中(在同一目标计算机上)运行时没有任何错误,但是当我以CLR存储过程运行时,我会得到此错误:
msg 6522,级别16,状态1,过程brx.uspsendmail,第0行[批处理启动行2]
在执行用户定义的例程或汇总“ uspsendmail”时,发生了.NET框架错误:system.net.mail.smtpexception:失败发送邮件。
system.io.ioexception:无法从传输连接读取数据:net_io_connectionclosed。
system.io.ioexception:
在system.net.mail.smtpreplyreaderfactory.processread(byte [] buffer,int32 offset,int32 read,boolean readline)
上
在System.NET.Mail.SMTPReplyReaderFactory.Readlines(smtpreplyreader呼叫者,布尔利亚线)
在system.net.mail.smtpreplyreaderfactory.readline(smtpreplyreader呼叫者)
上 在system.net.mail.checkcommand.send(smtpConnection conn,string& wenstry)
在system.net.mail.mail.mailcommand.send(smtpConnection conn,byte []命令,biladdress,boolean allyunicode)
在System.net.mail.smtptransport.sendmail(MailAddress发件人,MailAddressCollection收件人,String deliverynotify,boolean允许Notify,smtpfailedrecipientexception& exception&exception)
)
在system.net.mail.smtpclient.send(mailMessage消息)system.net.mail.smtpexception:
在system.net.mail.smtpclient.send(mailMessage消息)
)
上 在storedProcedure.sendmail(字符串收件人,字符串主题,字符串,来自布尔iShtml,字符串主体,字符串smtpserver,int32 smtpport,string smtpuser,string smtppassword,smtppassword,string cc,string bcc))
在StordProcedure.sendmail(字符串收件人,字符串 (准确地说是30/5/2022) - 当Google停止支持Gmail SMTP时。
有什么想法吗?
提前致谢, 埃雷斯
I'm trying to implement a CLR stored procedure that will send emails upon request using office365 SMTP.
This is my code:
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void SendMail(string recipients, string subject, string from, bool isHtml, string body, string smtpServer, int smtpPort, string smtpUser, string smtpPassword, string cc = "", string bcc = "")
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(from);
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = isHtml;
mailMessage.Body = body;
string[] strTo = recipients.Split(';');
foreach (var item in strTo)
mailMessage.To.Add(new MailAddress(item));
if (cc != "")
{
string[] strCc = cc.Split(';');
foreach (var item in strCc)
mailMessage.CC.Add(new MailAddress(item));
}
if (bcc != "")
{
string[] strBcc = bcc.Split(';');
foreach (var item in strBcc)
mailMessage.Bcc.Add(new MailAddress(item));
}
SmtpClient smtpClient = new SmtpClient
{
Host = smtpServer,
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(smtpUser, smtpPassword),
TargetName = smtpServer,
EnableSsl = true
};
smtpClient.Send(mailMessage);
}
}
The above code runs without any errors while in a console app (on the same target machine), but when I run it as a CLR stored procedure, I get this error:
Msg 6522, Level 16, State 1, Procedure brx.uspSendMail, Line 0 [Batch Start Line 2]
A .NET Framework error occurred during execution of user-defined routine or aggregate "uspSendMail":System.Net.Mail.SmtpException: Failure sending mail.
System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
System.IO.IOException:
at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)System.Net.Mail.SmtpException:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at StoredProcedure.SendMail(String recipients, String subject, String from, Boolean isHtml, String body, String smtpServer, Int32 smtpPort, String smtpUser, String smtpPassword, String cc, String bcc)
The strange thing is this same stored procedure worked up till lately (30/5/2022 to be exact) - when google stopped supporting gmail SMTP.
Any thoughts?
Thanks in advance,
Erez
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人偶然发现了这个错误,我找到了一个解决方案:将以下内容添加到代码的顶部:
我不确定为什么需要这一点,以及为什么Gmail的SMTP不需要它才能正常工作,但是至少一切都是终于很好。
我发现此解决方案线程,@Simon Fallai。
If anyone stumbles upon this error, I found a solution: Add the following to the top of the code:
I'm not sure why this is needed, and why gmail's SMTP didn't need it to work properly, but at least all is good at last.
I found this solution on another Stack Overflow thread, by @Simon Fallai.