New way to send mail in SQL Server

xiaoxiao2021-03-06  51

Said to be a new way, in fact, it is also used, so put it up!

In .NET, you know that you can use System.Web.mail to send emails. Verification is supported under Framework 1.1.

Private void page_load (object sender, system.eventargs e) {mailmessage mail = new mailmessage (); mail.to = "me@mycompany.com"; mail.from = "@Yourcompany.com"; mail.subject = " This Is A Test Email. "; mail.body =" some text goes "; mail.fields.add (" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate, "1"); // Basic Authentication Mail.fields.add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "My_USERNAME_HERE"); // set your username here mail.fields.add ("http: // Schemas. Microsoft.com/cdo/configuration/sendpassword "," super_secret "); // set your password here

SMTPMAIL.SMTPSERVER = "mail.mycompany.com"; // Your real server goes here smtpmail.send (mail);

I used to write a way to send mail under. Net, see:

http://dev.9cbs.net/develop/Article/17/17189.shtm

In SQL Server, we generally use the SQL itself's email sending mode, but you need to configure Exchage Server, Outlook, etc., which is also a cumbersome thing. Many people complain that the configuration is unsuccessful.

In fact, we can create an OLE object instance in SQL Server, call the IIS SMTP's own transmitting components to implement mail delivery.

We build this stored procedure, where you need to modify, the name of SMTPServer

Create Procedure Sys_sendmail @From Varchar (100), @To varchar (100), @BCC VARCHAR (500), @SUBJECT VARCHAR (400) = "" ", @Body nText ="

AS

Declare @Object int declare @hr int

Exec @hr = sp_oacreate 'cdo.message', @Object Out

EXEC @hr = sp_OASetProperty @object, 'Configuration.fields ( "http://schemas.microsoft.com/cdo/configuration/sendusing") .Value', '2' EXEC @hr = sp_OASetProperty @object, 'Configuration.fields ("http://schemas.microsoft.com/cdo/configuration/smtpserver").value ',' SMTP.163.com '- The following three statements are SMTP verification. If the server needs to be verified, these three sentences must be you need to modify the user name and password EXEC @hr = sp_OASetProperty @object, 'Configuration.fields ( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") .Value', '1' EXEC @hr = sp_OASetProperty @object, 'Configuration.fields ( "http://schemas.microsoft.com/cdo/configuration/sendusername") .Value', 'lihonggen0' EXEC @hr = sp_OASetProperty @object, 'Configuration.fields ( "http: / /schemas.microsoft.com/cdo/configuration/sendpassword").value' ,'XXX '

EXEC @hr = sp_OAMethod @object, 'Configuration.Fields.Update', nullEXEC @hr = sp_OASetProperty @object, 'To', @ToEXEC @hr = sp_OASetProperty @object, 'Bcc', @BccEXEC @hr = sp_OASetProperty @object, 'From', @Fromexec @hr = sp_oasetproperty @Object, 'Subject', @SUBJECT

Exec @hr = sp_oasetproperty @Object, 'textbody', @BodyExec @object, 'send', NULL

- Judging error IF @hr <> 0begin exec sp_oageterrorinfo @object return @objectndprint 'success'exec @hr = sp_oadestroy @Object @OADESTROY @Object

Go

Note: You must ensure that SMTP is installed, you can access the CDO object.

转载请注明原文地址:https://www.9cbs.com/read-117398.html

New Post(0)