Sending a mail from SQL Server? - sql

is it possible to send an email with SQL Server? I would like to be able to create a procedure for sending an email. It seems that I have to do some configuration according to this site but I don't have admin access on my machine. How can I achieve that ?
Thanks for your help !!
P.S : I am working on Windows

If you have limited privileges on the SQL Server why don't you write the query to obtain the users' names, email adresses and forgotten DVD titles and send the email from you application?
As long as you have access to an SMTP server you should be good to go.
Or you could always try the obvious:
USE YourDB;
GRANT CONTROL SERVER TO ME AS (JON_SKEET OR CHUCK_NORRIS);
GO
:-)

You could use a CLR Stored Procedure.
See this example for instance: Send Email from SQL Server Express Using a CLR Stored Procedure
Sending it via .NET would allow you to relay on a external SMTP and not having to configure Database Mail.

xp_sendmail is the command you want for older versions of SQL Server. Note that this is being removed from future versions, so for versions which support it use Database Mail.
EDIT Sorry - just read your full post. If you don't have the rights to enable mail sending, you'll need to speak to the admin who has. Server security is not designed to be "worked-around". Why does the mail have to be sent from SQL Server?

Related

SQL Database Mail Not Working - But I can send email on same server using VBscript CDO

I have searched everywhere (it feels) for a solution and / or guidance. My last resort is to ask. I have SQL Server 2016 running on a Windows Server. Database mail is not working. I have setup the profile and have submitted a test email by right clicking on the Database Mail node in the SSMS left window.
The email is in an 'unsent' status when I query SQL using SELECT * FROM msdb.dbo.sysmail_allitems;
I created a VBscript file to send an email using CDO - using the same email server values (host name, port, email from, user name, password, etc). The CDO script successfully sends the email.
I'm not sure where to go to troubleshoot SQL.
There is no data being written to SELECT * FROM msdb.dbo.sysmail_event_log;
Admittedly I am not very good with the SQL configuration - I just need to get it setup. I'm trying to determine why the status is just staying in 'unsent'. Is there an action I can do to force a send? Is there some message or log somewhere I can look at to see why it isn't move forward?
EDIT: I have put the same credentials into sql reporting services configuration 'email settings' - and I am able to create subscriptions and successfully send emails. And that confuses me because I see that SSRS Subscriptions require access to the SQL agent and creates a scheduled job.
So what is different about my database mail account in SQL then the one I created in SSRS? ( I understand that I am not posting pictures but that those wizards have personal info so I don't know the value in posting screenshots for data values I can't share ).
Any help is appreciated.
Thanks
This issue is resolved. Thank you #AlwaysLearning. I had to update the server by installing .Net Framework 3.5. Then - following your guidance I ultimately found my problem was with the SSL check box and port 465. I was able to send email using port 25 with no SSL, and also to send TLS on port 587. But SSL and port 465 are still causing problems.
For me however, that is not my main concern. That problem has been passed to someone else in the chain to work on. I am able to use Database Mail.

Send email from SQL Server 2005 / 2008

I have to send emails from SQL Server 2005 or 2008. I have done this in the past with additional stored procedures (sp_send_html_mail) and complex configuration / activations. Is there standard build-in stored procedure which sends email via SMTP (not MAPI/CAPI/Outlook profiles)? I found sp_send_dbmail on this topic but not sure is it the only one and what security issues are possible (except spam)?
Yes, sp_send_dbmail is the one to use for both SQL Sever 2005 and 2008.
You will need to make sure it has been configured on your server. It's been a while since I did such a config, but if memory serves me correctly, it is off by default. Have a look at the MS docs for that process, Configure Database Mail. Setting an SMTP server is doable as well. There are lots of options in there so give it a thorough review.
If you are worried about unwanted use of that sp you can use roles and/or execute permission to limit access to it. From the MS docs on troubleshooting dbmail:
To send Database mail, users must be a user in the msdb database and a
member of the DatabaseMailUserRole database role in the msdb database.
To add msdb users or groups to this role use SQL Server Management
Studio or execute the following statement for the user or role that
needs to send Database Mail.
EXEC msdb.dbo.sp_addrolemember #rolename = 'DatabaseMailUserRole'
,#membername = '<user or role name>';
GO

SQL Server 2005: Difference between SQL mail and DB mail

Ok. I know SQL Mail is less secure and old, but what are the real advantages and differences between the two?
I read this article, which is pretty straight forward, but I was hoping to get someone to tell me why (if any) would you want to use SQL mail? Is it really out dated?
Thanks
Benefits of Database Mail over SQL Mail
Database Mail has the option of limiting file sizes to prevent sending large attachments
Database Mail can be configured with multiple SMTP accounts and with multiple profiles
No impact on the database performance, the mailing process is external
Having access to the mail history
Database mail :
Based on SMTP (Simple Mail Transfer Protocol).
Introduced in Sql 2005.
No need to install Outlook.
Depend on Service Broker service.
More secure than Sqlmail.
SQLMail
Based on MAPI (Messaging Application Programming Interface).
Used prior versions of Sql server 2005.
Require Outlook to be installed.
Leass secure than Database mail.
Link :
Database Mail
database-mail-vs-sql-mail

Send mail by SQL server 2005

i am not getting this store procedure , can you tell where is it "master..xp_sendmail"
i have send the mail when any insertion take place in particular table like "Emp"
According to Microsoft you should not use xp_sendmail anymore. Use this instead:
http://msdn.microsoft.com/en-us/library/ms175887(v=SQL.90).aspx
As for the process of sending the emails... I would decouple the insert and the sending of the emails. You could have a SQL Server job that polls for new entries in a table and sends the emails if needed. This job could be sheduled to run every 5 minutes.
Note from MS:
This feature will be removed in a
future version of Microsoft SQL
Server. Avoid using this feature in
new development work, and plan to
modify applications that currently use
this feature. To send mail from SQL
Server, use Database Mail.
EDIT: incorporated the usefull links from the comment of adolf garlic
http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/
http://www.databasejournal.com/features/mssql/article.php/3626056/Database-Mail-in-SQL-Server-2005.htm

Using a Trigger to Email from SQL Express

I would like to send an email from SQL Server 2005 Express using a trigger.
The solutions I have seen use the System Stored Procedures xp_sendmail
or sp_send_dbmail, but these are not available under SQL Express (will only work under full SQL Server).
Any suggestions would be appreciated.
Thanks
Sending an Email from a trigger sounds like a bad idea.
Why not poll the table and pick up modified records, then send an Email for each new/modified entry?
As Leather said, trying to send an email in a trigger is a bad idea.
While in my opinion upgrading to SQL Standard and using sp_send_dbmail is the best way to send email through a database, I believe you can accomplish what you want to do through the low-fi CDOSYS .
Described here: http://support.microsoft.com/kb/312839
I have not used CDOSYS since SQL 2000 so I don't know for sure that it will be your solution, but it does give you another avenue to learn about sending email pro grammatically.