SQL server "Cannot send mails to mail server" "Failure sending mail" - sql-server-2008-express

I am using MS SQL Server 2008 R2 SP2 Express edition, 64 bit.
I do not have access to the Database Mail setup wizard, but the Binn directory contains DatabaseMail.exe (v10.50.16600.1) and DatabaseMailengine.dll.
I found several references to "Cannot send mails to mail server", but nothing useful with the annoyingly generic "Failure sending mail" message.
My specific question is where can I find out what that error message means/what causes it? If some kind soul would care to hazard a guess about what is going on, or not going on, I would appreciate it.
Setup info from the database follows:
EXECUTE sp_configure 'show advanced'
name minimum maximum config_value run_value
show advanced options 0 1 1 1
EXECUTE sp_configure 'Database Mail XPs'
name minimum maximum config_value run_value
Database Mail XPs 0 1 1 1
sysmail_server
account_id servertype servername port username credential_id use_default_credentials enable_ssl flags timeout last_mod_datetime last_mod_user
1 SMTP smtp.gmail.com 465 sqlmail#domain.com 65536 0 1 0 NULL 2015-11-23 09:34:26.913 WSCORP\username
sys.credentials
credential_id name credential_identity create_date modify_date target_type target_id
65536 F05ADE33-6AA8-45FC-8FF5-A7631831ECB1 sqlmail#domain.com 2015-11-23 09:34:26.913 2015-11-23 09:34:26.913 NULL NULL
EXEC msdb.sys.sp_helprolemember 'DatabaseMailUserRole'
DbRole MemberName MemberSID
DatabaseMailUserRole WSCORP\username 0x010500000000000515000000F483990DEADF2220C7DB306E6B040000
EXEC msdb.dbo.sysmail_help_principalprofile_sp
principal_id principal_name profile_id profile_name is_default
27 WSCORP\username 1 Default 1
EXECUTE dbo.sysmail_start_sp
Status
STARTED
sysmail_account
account_id name description email_address display_name replyto_address last_mod_datetime last_mod_user
1 SQLMail NULL sqlmail#domain.com SQLExpresspay12 Mail user#domain.com 2015-11-23 09:34:26.843 WSCORP\username
sysmail_profile
profile_id name description last_mod_datetime last_mod_user
1 Default Default profile 2015-11-23 09:56:58.947 WSCORP\username
sysmail_profileaccount
profile_id account_id sequence_number last_mod_datetime last_mod_user
1 1 1 2015-11-23 10:02:29.867 WSCORP\username
sysmail_servertype
servertype is_incoming is_outgoing last_mod_datetime last_mod_user
SMTP 0 1 2010-04-02 17:36:17.340 sa
sysmail_mailitems
mailitem_id profile_id recipients copy_recipients blind_copy_recipients subject from_address reply_to body body_format importance sensitivity file_attachments attachment_encoding query execute_query_database attach_query_result_as_file query_result_header query_result_width query_result_separator exclude_query_output append_query_error send_request_date send_request_user sent_account_id sent_status sent_date last_mod_date last_mod_user
4 1 user#domain.com NULL NULL Test message NULL NULL This is the body of the test message. TEXT NORMAL NORMAL NULL MIME NULL NULL 0 1 256 0 0 0 2015-11-24 08:23:59.493 WSCORP\username NULL 2 11/1124/20158 8:25:20 11/24/2015 08:25:20 sa
Congratulations, Database Mail Received
By you Successfully.
sysmail_log
log_id event_type log_date description process_id mailitem_id account_id last_mod_date last_mod_user
13 1 2015-11-24 08:23:59.787 DatabaseMail process is started 7324 NULL NULL 2015-11-24 08:23:59.787 NT AUTHORITY\NETWORK SERVICE
14 1 2015-11-24 08:24:10.270 The mail queue was started by login "WSCORP\username". NULL NULL NULL 2015-11-24 08:24:10.270 WSCORP\username
15 3 2015-11-24 08:24:10.297 The mail could not be sent to the recipients because of 7324 4 NULL 2015-11-24 08:24:10.297 sa
the mail server failure. (Sending Mail using Account 1
(2015-11-24T08:24:10). Exception Message: Cannot send
mails to mail server. (Failure sending mail.). )

Google wanted me to use the TLS port (587) even though, as far as I can tell, there was no way to tell the SQL server to use TLS. So specifying SSL and using the TLS port worked.

Related

Query NPS Radius SQL log for reject events

I'd like to have a SQL query to find only radius reject events (type=3). The previous row is also required to know the user name that was rejected.
Data looks like this:
id
timestamp
NPS_Svr
Packet_Type
Description
User-Computer
27949
1:25:46 PM
SVR1
2
Access Accept
NULL
27948
1:25:46 PM
SVR1
1
Access Request
user1
27947
1:25:36 PM
SVR1
3
Access REJECT
NULL
27946
1:25:36 PM
SVR1
1
Access Request
user1
27945
1:25:33 PM
SVR3
2
Access Accept
NULL
27944
1:25:33 PM
SVR3
1
Access Request
user2
27943
1:25:21 PM
SVR3
3
Access REJECT
NULL
27942
1:25:21 PM
SVR3
1
Access Request
user2
select *
from accounting_data
where packet_type=3
Returns the reject rows but I also need the previous row from the same NPS Server to know the user name that was rejected.
The server is MS SQL 2014.
As I understand previous row it's
according to sort order of id columns,
So I suggest to self join by id-1
SELECT main.*,past.user_computer FROM (
select m.*,RANK() OVER (PARTITION BY nps_svr ORDER BY id ) as RnKSer
from accounting_data m
) main left join (SELECT nps_svr,RnKSer-1 as PastRnKSer,user_computer
FROM (
select m.*,RANK() OVER (PARTITION BY nps_svr ORDER BY id ) as RnKSer
from
accounting_data m
) prev ) past on main.nps_svr=past.nps_svr and RnKSer=PastRnKSer
where packet_type=3
ORDER BY 1

Get the reply of an outgoing message in the same row in SQL?

I am trying to get the incoming message for an outgoing message in the same row as response? So, the data looks like below:
user_id message type
1 What's your name? outgoing
1 Nitin incoming
1 What's your age? outgoing
1 17 incoming
2 What's your name? outgoing
2 Aayush incoming
2 What's your age? outgoing
3 What's your name? outgoing
4 What's your name? outgoing
4 Shubham incoming
4 What's your age? outgoing
The output should look like:
user_id message type reply
1 What's your name? outgoing Nitin
1 Nitin incoming
1 What's your age? outgoing 17
1 17 incoming
2 What's your name? outgoing Aayush
2 Aayush incoming
2 What's your age? outgoing No-reply
3 What's your name? outgoing No-reply
4 What's your name? outgoing Shubham
4 Shubham incoming
4 What's your age? outgoing No-reply
The reply is an incoming message for an outgoing message for a user_id & if there is no reply for an outgoing message then the reply column should be filled by 'No-reply'. I have to do this in PSQL. I am currently able to do this in python after fetching data from DB.
Below is my current python code.
df['reply'] = np.where((df['user_id'] == df['user_id'].shift(-1))
& (df['type'].eq('Outgoing') & df['type'].shift(-1).eq('Incoming')),
df['message'].shift(-1), 'No-reply')
Assuming that you have a column that can be used to order the records (called id), you could use lead() as follows:
select
t.*,
case
when type = 'outgoing'
and lead(type) over(partition by user_id order by id) = 'incoming'
then lead(message) over(partition by user_id order by id)
when type = 'outgoing' then 'no reply'
end reply
from mytable
This query checks if the current record has type outgoing and if the next one (for the same user) is of type incoming: if both conditions are true, then it recovers the message on the next record and displays it as reply. If type is outgoing but the next message is not incoming, then it gives no reply. In other cases, nothing is printed.

Send email through procedure in PL SQL

I want to make a procedure through which I can send email to my team mail id.
actually there are number of procedures running daily in our system.So i want to send the status of each procedure means if procedure is getting success,failed or running.If the procedure is in running state i have to show the running time in that email.And also if the procedure is failed then i have to show the error message in that email.Actually one table is present in our database in which procedure
id,date,start_time,end_time,status,error
desc columns are there.I shared the details below.
proc id date start_time end_time status error desc
1 6/7/2017 7/5/2017 9:55:16 AM 7/6/2017 1:36:25 AM SUCCESS
2 6/28/2017 6/29/2017 8:30:02 AM 6/29/2017 2:20:15 PM FAIL -1555 - ORA-01555: snapshot too old: rollback segment number 334 with name "_SYSSMU334_1817651691$" too small
ORA-02063:
3 7/5/2017 7/6/2017 9:34:54 AM RUNNING
As i said i have to show all the 3 types of status in the email which is to be sent through procedure.kindly help me regarding this.

Is this possible with an SQL query?

Sorry for the generic title of the question, but I didn't know how else to put it.. So here goes:
I have a single table that holds the following information:
computerName | userName | date | logOn | startUp
| | | |
ID_000000001 | NULL | 2012-08-14 08:00:00.000 | NULL | 1
ID_000000001 | NULL | 2012-08-15 09:00:00.000 | NULL | 0
ID_000000003 | user02 | 2012-08-15 19:00:00.000 | 1 | NULL
ID_000000004 | user02 | 2012-08-16 20:00:00.000 | 0 | NULL
computername and username are self-explanatory I suppose
logOn is 1 when the user logged on at the machine and 0 when he logged off.
startUp is 1 when the machine was turned on and 0 when it got turned off.
the other entry is alway NULL respectively since we can't login and startup at the exact same time.
Now my task is: Find out which computers have been turned on the least amount of time over the last month (or any given amount of time, but for now let's say one month) Is this even possible with SQL? <-- Careful: I don't need to know how many times a PC was turned on, but how many hours/minutes each computer was turned on over the given timespace
There's two little problems as well:
We cannot say that the first entry of each computer is a 1 in the startUp column since the script that logs those events was installed recently and thus maybe a computer was already running when it started logging.
We cannot assume that if we order by date and only show the startUpcolumn that the entries will all be alternating 1's and 0's because if the computer is forced shut down by pulling the plug for example there won't be a log for the shutdown and there could be two 1's in a row.
EDIT: userName is of course NULL when startUp has a value, since turning on/shutting down doesn't show which user did that.
In a stored procedure, with cursors and fetch loops.
And you use a temptable to store by computername the uptime.
I give you the main plan, I'll let you see for the details in the TSQL guide.
Another link: a good example with TSQL Cursor.
DECLARE #total_hour_by_computername
declare #computer_name varchar(255)
declare #RowNum int
--Now in you ComputerNameList cursor, you have all different computernames:
declare ComputerNameList cursor for
select DISTINCT computername from myTable
-- We open the cursor
OPEN ComputerNameList
--You begin your foreach computername loop :
FETCH NEXT FROM ComputerNameList
INTO #computer_name
set #RowNum = 0
WHILE ##FETCH_STATUS = 0
BEGIN
SET #total_hour_by_computername=0;
--This query selects all startup 1 dates by computername order by date.
select #current_date=date from myTable where startup = 1 and computername = #computername order by date
--You use a 2nd loop on the dates that were sent you back:
--This query gives you the previous date
select TOP(1) #previousDate=date from myTable
where computername = #computername and date < #current_date and startup is not null
order by date DESC
--If it comes null, you can decide not to take it into account.
--Else
SET #total_hour_by_computername=#total_hour_by_computername+datediff(hour, #previousDate, #current_date);
--Once all dates have been parsed, you insert into your temptable the results for this computername
INSERT INTO TEMPTABLE(Computername,uptime) VALUES (#computername,#total_hour_by_computername)
--End of the #computer_name loop
FETCH NEXT FROM ComputerNameList
INTO #computer_name
END
CLOSE ComputerNameList
DEALLOCATE ComputerNameList
You only need a select into your temptable to determine which one of the computers has been up the most time.
You could group by computer, and use where to filter for startups in a particular month:
select computerName
, count(*)
from YourTable
where '2012-08-01' <= [date] and [date] < '2012-09-01'
and startup = 1
group by
computerName
order by
count(*) desc
As RoadWarrior pointed out, an accurate reports is not possible when shutdown messages are dropped. But here is an attempt to generate something useful. I'm going to assume the table name is computers:
SELECT c1.computerName,
timediff(MIN(c2.date), c1.date) as upTime
FROM computers as c1, computers as c2
WHERE c1.computerName=c2.computerName
AND c1.startUp=1 AND c2.startUp=0
AND c2.date >= c1.date
GROUP BY c1.date
ORDER BY c1.date;
This will generate a list of all the periods a computer was on. To generate your requested report you can use the above query as a subquery:
SELECT
c3.computerName,
SEC_TO_TIME(SUM(TIME_TO_SEC(c3.upTime))) AS totalUpTime
FROM
(SELECT c1.computerName,
timediff(MIN(c2.date), c1.date) AS upTime
FROM computers AS c1, computers AS c2
WHERE c1.computerName=c2.computerName
AND c1.startUp=1 AND c2.startUp=0
AND c2.date >= c1.date
GROUP BY c1.date
ORDER BY c1.date
) AS c3
GROUP BY c3.computerName
ORDER BY c3.totalUpTime;
Try this query (replace table_name with the name of your table):
SELECT SUM(startUp) AS startupTimes
FROM table_name
GROUP BY computerName
ORDER BY startupTimes
This will output the number of times each computer has been started. To get just the first row (the computer that has the least amount of startups) you can append LIMIT 1 to the query.
If (per your last paragraph) you aren't recording all shutdown events. then you don't have the information available to generate a report showing the amount of time each computer has been switched on. Because you aren't recording all instances of computer shutdown, it doesn't matter what SQL query you use.
FWIW, this schema isn't 3NF. A more common approach would be to have a single column recording each event, for example:
ComputerId:UserId:EventId:EventDate
The first three columns are each a foreign key into another table where the details are stored. Although even with this schema, the UserID would be null for startup/shutdown events.

SQL Server 2005 DbMail not sending

I have a SQL Server 2005 DB Mail profile set up on my server that successfully sends a test email. However, when I queue an email in the msdb.dbo.sysmail_mailitems table, it does not ever try to send it. The status of the item stays at 0 (unsent).
In the Database Mail Log, the following entries are shown:
event_type log_date description process_id mailitem_id account_id last_mod_date last_mod_user
0 2012-05-07 10:07:00.887 Activation successful. NULL NULL NULL 2012-05-07 10:07:00.887 sa
1 2012-05-07 10:07:06.323 DatabaseMail process is started 6504 NULL NULL 2012-05-07 10:07:06.323 NT AUTHORITY\SYSTEM
3 2012-05-07 10:07:07.213 Could not retrieve item from the queue. 6504 NULL NULL 2012-05-07 10:07:07.213 NT AUTHORITY\SYSTEM
1 2012-05-07 10:12:07.453 DatabaseMail process is shutting down 6504 NULL NULL 2012-05-07 10:12:07.453 NT AUTHORITY\SYSTEM
There are no exceptions in the windows event viewer. Can someone direct me to where I can find more info regarding the error? "Could not retrieve item from the queue." doesn't help me much.
Thanks