반응형


김종열

MS SQL Server MVP

엔트리브 소프트


Ms SQL SERVER에서 Email을 보내려고 하실때 어떻게 하시나요?
혹시 불편하신 점은 없었나요? 간간히 Community에 올라오는 것 중에 하나더군요.
SQL server에서 mail을 보낼때 에러가 많이 나느니..
작업의 결과를 email로 보내고 싶다는 등의

역시 만들면 됩니다.
True / False를 리턴하는 함수하나를 만들었습니다.
회사 smtp를 공개할 수도 없는 일이고, 그렇다고 이 때문에 smtp기능을 지원하는 곳의 서비스를 이용할 수 도 없고..
google은 이미 무료의 서비스를 제공하고 있기에 가입하고 사용하기로 했습니다.
smtp는 환경에 맞게 설정 및 변경하시어 사용하시기 바랍니다.


using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.Net;

using System.Net.Mail;

 

public partial class UserDefinedFunctions

{

    [Microsoft.SqlServer.Server.SqlFunction]

    public static SqlBoolean UDF_MAIL_SEND(string toAddr, string mailSubj, string mailCont)

    {

        string frAddr = "id@gmail.com";  //setting

        string frPwd = "password";      //setting

 

        MailMessage cMail = new MailMessage(frAddr, toAddr, mailSubj, mailCont);

        SmtpClient cSmtp = new SmtpClient("smtp.gmail.com", 587);

 

        cSmtp.Credentials = new NetworkCredential(frAddr, frPwd);

        cSmtp.EnableSsl = true;

 

        try

        {

            cSmtp.Send(cMail);

            return true;

        }

        catch

        {

            return false;

        }

    }

};

 

 


쿼리로 확인하기.

use youlyDB

go

 

declare @rst bit

set @rst = dbo.udf_mail_send('youly_92@hotmail.com', 'testMail', 'Go Mail Test Content')

select @rst

 


udf_mail_send의 param은 Reciever Address, Mail subject, Mail Content입니다. 
Return은 당연 True / False 입니다.
(메일을 받은 hotmail의 결과를 그림으로 올리려 했는데
왠지 그림파일이 올라가지 않네요..)

참고파일 :  udf_mail_send.cs
              udf_mail_send.sql


설정 중에 데이터베이스 권한 수준을 '안전하지 않음' (UNSAFE) 로 설정해야 되네요
처음에 '외부' (external) 로 설정하고 했더니 아래와 같은 오류가 나더군요.
System.Security.SecurityException: 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken= ' 형식의 사용 권한을 요청하지 못했습니다.

반응형

'연구개발 > CLR' 카테고리의 다른 글

CLR- 음력을 양력으로 변환하는 함수  (0) 2009.06.22
CLR Utf8String - UDT  (0) 2009.06.22
CLR - FTP download(FTP 2탄)  (0) 2009.06.22
CLR - FTP upload 구현(FTP 1탄)  (0) 2009.06.22
CLR - EXCEL 파일 읽기  (0) 2009.06.22

+ Recent posts