I am a beginner in log4net and I am trying to add an appender in my code and then to send an email in case of an Error. I don't want to use a config file. My program runs but I never receive the email. What's wrong in my code?
Dim test2 As log4net.Appender.SmtpAppender = New log4net.Appender.SmtpAppender()
test2.To = toto#hotmail.com
test2.From = toto#hotmail.com
test2.Subject = "test Email report"
test2.SmtpHost = "localhost"
test2.BufferSize = 1
test2.Lossy = true
Dim layout As log4net.Layout.PatternLayout = New log4net.Layout.PatternLayout()
layout.ConversionPattern = "%newline%message"
test2.Layout = layout
test2.ActivateOptions()
Dim l As log4net.Repository.Hierarchy.Logger = log4net.LogManager.GetLogger("EmailLog").Logger
l.Level = log4net.Core.Level.Error
l.Additivity = True
l.Repository.Configured = True
l.AddAppender(test2)
msg = "test test test" & vbNewLine & vbNewLine
Dim logger As log4net.ILog = log4net.LogManager.GetLogger("EmailLog")
logger.Error(msg)
I don't have a direct answer to your question, but a useful suggestion.
Test it using the configuration setup first, that way you can quickly eliminate any external problems unrelated to your code.
Related
I have a file in witch there is a mix of HTML, Javascript, and Visual Basic, it is actually a web site (when up and running).
On a click event, I'm able to reach a VB function (tested) but now, the email part.
Dim SmtpServer As New SmtpClient()
SmtpServer.UseDefaultCredentials = true
SmtpServer.Port = 25
SmtpServer.Host = "12345.com"
Dim mail As New MailMessage()
mail.From = "si#csnavigateurs.qc.ca"
mail.To.Add("pelletiera77#csnavigateurs.qc.ca")
mail.Subject = "Email Sending"
mail.Body = "Testing the 1 and 2"
SmtpServer.Send(mail)
I was thinking it would work by itself but no, surely there is something to config out in order to make this work. It throws a server error right at Dim SmtpServer As New SmtpClient() So I guess I have to include something?
The error is:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
The problem is here (from property is not string):
mail.From = "si#csnavigateurs.qc.ca"
I used this and worked:
mail.From = new System.Net.Mail.MailAddress(emailAddress, name);// Email-ID of Sender
If you do not like use name of sender you can do this:
mail.From = new System.Net.Mail.MailAddress(emailAddress);
Try this:
Try
Dim SmtpServer As New System.Net.Mail.SmtpClient()
SmtpServer.UseDefaultCredentials = true
SmtpServer.Port = 25
SmtpServer.Host = "csnavigateurs-qc-ca.mail.protection.outlook.com"
Dim mail As New MailMessage()
mail.From = "si#csnavigateurs.qc.ca"
mail.To.Add("pelletiera77#csnavigateurs.qc.ca")
mail.Subject = "Email Sending"
mail.Body = "Testing the 1 and 2"
SmtpServer.Send(mail)
Response.Write("Success")
Catch ex As Exception
Response.Write(ex.Message)
End Try
This should give you a clearer error message if it fails.
I got this code and getting an error prompt. Database Login. Even I put the right password, it always says Login Failed.
Dim report As New ReportDocument
report.Load("rptPrntIss.rpt")
report.RecordSelectionFormula = "{tbl_issued.TransactionID}=" & txtIssID.Text & "AND ({tbl_transaction.Department}=" & cBoxDpt.Text & ")"
frmPrnt.CrystalReportViewer1.ReportSource = report
frmPrnt.CrystalReportViewer1.Refresh()
frmPrnt.ShowDialog()
Maybe this code is not right, because everything was perfect without this code.
use setdatabaselogon() function to prevent prompting database logon each time.
it will be like
report.SetDatabaseLogon("username", "password", "server", "dbname", false)
in some cases this will also wont work(had some situations recently for me),in that case we need to specify database login for each tables in report.
like
dim connInfo as new ConnectionInfo()
connInfo.ServerName = yourserver
connInfo.DatabaseName = "dbname"
connInfo.UserID = "username"
connInfo.Password = "password"
dim tableLogOnInfo as new TableLogOnInfo()
tableLogOnInfo.ConnectionInfo = connInfo
foreach Table as table in reportDoc.Database.Tables
table.ApplyLogOnInfo(tableLogOnInfo)
table.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName
table.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName
table.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID
table.LogOnInfo.ConnectionInfo.Password = connInfo.Password
next
hope this helps.
I've wrote a script to create a HTML file based on a SQL Query.... It has become necessary to have that HTML be emailed. Most of our execs use blackberry's and I want to send the HTML file as the body. I have found a round about way to get this done, by adding a WebBrowser, and having the web browser then load the file, and then using the below code to send. The problem i'm facing is if I automate the code fully, it will only email part of the HTML document, now if I add a button, and make it do the email function, it sends correctly. I have added a wait function in several different location, thinking it may be an issue with the HTML not being fully created before emailing. I have to get this 100% automated. Is there a way I can use the .HTMLBody to link to the actual HTML file stored on the C:(actual path is C:\Turnover.html). Thanks all for any help.
Public Sub Email()
Dim strdate
Dim iCfg As Object
Dim iMsg As Object
strdate = Date.Today.TimeOfDay
iCfg = CreateObject("CDO.Configuration")
iMsg = CreateObject("CDO.Message")
With iCfg.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "xxxxx.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = """Turnover Report"" <TurnoverReports#xxxxx.com>"
.Update()
End With
With iMsg
.Configuration = iCfg
.Subject = "Turnover Report"
.To = "xxxxx#xxxxx.com"
'.Cc = ""
.HTMLBody = WebBrowserReportView.DocumentText
.Send()
End With
iMsg = Nothing
iCfg = Nothing
End Sub
used the below function to read in a local html file. then set
TextBox2.Text = getHTML("C:\Turnover2.html")
and also
.HTMLBody = TextBox2.Text
Private Function getHTML(ByVal address As String) As String
Dim rt As String = ""
Dim wRequest As WebRequest
Dim wResponse As WebResponse
Dim SR As StreamReader
wrequest = WebRequest.Create(address)
wResponse = wrequest.GetResponse
SR = New StreamReader(wResponse.GetResponseStream)
rt = SR.ReadToEnd
SR.Close()
Return rt
End Function
I know this question has been asked a few times in various context, but I have not found a clear answer. I have email implemented for an access application using outlook, but I'd like to move away from this. One of the purposes of the email is to email a user his/or password if he forgot it. They can select their username for the login screen, and if they click 'forgot password' and email is sent containing their login information (to the email address associated with the user name).
The problem with this is that the email function as is sends an email with outlook from the user's machine. So, users would be able to 'forgot password' other usernames and view their own outlook outbox(sent items) to see the sensitive information.
Is there a way to e-mail like php's mail function, sending mail from the server? I would like the emails to be sent from the same email address i.e(support#company.com), instead of from the user's outlook address after a security prompt. If this is not possible, I am open to the idea of any other workarounds.
I will also add that installing any software that would have to be installed on every potential user's machine is not feasible.
Is this possible?
Windows includes an object called Collaborative Data Objects or CDO. This object allows you to send emails using any SMTP server assuming that other prerequisites are met (firewall open, ISP not blocking ports, account is configured on the SMTP server, SMTP server allows relaying, etc).
Most of the examples I've found use late binding, which is preferred. In my testing on XP it appeared that the correct library reference, if you prefer to use early binding, is "Microsoft CDO for Windows 2000 Library".
It's important to know that any time you send email you will have to send it through (or out of) some kind of email server. This means you will have to authenticate with that email server and also usually means that you need to send the email out using a "From" email address that exists on that very email server.
Here's some code using late binding:
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const cdoNTLM = 2 'NTLM
Public Sub SendEmail()
Dim imsg As Object
Dim iconf As Object
Dim flds As Object
Dim schema As String
Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields
' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "mail.myserver.com"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "email#email.com"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update
With imsg
.To = "email#email.com"
.From = "email#email.com"
.Subject = "Test Send"
.HTMLBody = "Test"
'.Sender = "Sender"
'.Organization = "My Company"
'.ReplyTo = "address#mycompany.com"
Set .Configuration = iconf
.Send
End With
Set iconf = Nothing
Set imsg = Nothing
Set flds = Nothing
End Sub
This works for me in MS Access 2010 / Windows 7
sMailServer = "myISPsmtp" 'Not just any old smtp
sMailFromAddress = "me"
sMailToAddress = "me"
Set ObjMessage = CreateObject("CDO.Message")
sToAddress = sMailToAddress
sSubject = "Subject"
sBody = "MailBody"
ObjMessage.Subject = sSubject
ObjMessage.From = sMailFromAddress
ObjMessage.To = sToAddress
'ObjMessage.cc = sCCAddress
ObjMessage.TextBody = sBody
'ObjMessage.AddAttachment sMailAttachment
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailServer
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjMessage.Configuration.Fields.Update
ObjMessage.send
More info: http://msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx
I cannot add this to the comments because I do not have enough reputation, so please don't axe me.
"Seems like this method lets you spoof about anything on my server. Just noticed that there's an addAttachment method. Could that work with just a relative path to say, an excel sheet? "
It works for me (Access 2010, Exchange 2010):
.AddAttachment ("URL HERE")
https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx
https://msdn.microsoft.com/en-us/library/ms526983(v=exchg.10).aspx
The following MS-Access VBA code works for smtp.office365.com. You DO indicate smtpusessl=true, but you do NOT specify the port, otherwise you get error 5.7.57.
Sub SMPTTest2()
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "name#myaddress.com"
emailObj.To = "name#youraddress.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
'emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Exclude the following line
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name#myaddress.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub
At my company I used a other solution. I have created a C# Class Library with COM classes / objects. COM classes can be implemented in your Access application and this way you can use all the advantages of C# (Mailing for example) and still use it (calling it) in Access.
The only disadvantage is that you have to register your Class Library (DLL) at all the computers who use your access application. I have done that with a simple power-shell script which executes at the start of the Access application.
A good start for A COM based library is here: https://www.codeproject.com/Articles/7859/Building-COM-Objects-in-C
If you would like some more information about it then I am always happy to help you.
I have a number of reports in a folder, and it will grow in a time. To subscribe to all reports user has to go to each one and create subscription. With about 10 reports it's manageable, but when there are 30 reports and a new user should be added to a subscription it's becoming difficult.
How can I create some kind of batch subscription? I mean - to make easy subscription to all reports in a folder and send it to the user(s) (no matter where - e-mail or file share). Is there some administrative option or should I write some script to achive it?
Environment: SQL Server 2008 R2 + SSRS 2008 R2 (Standard Edition) on W2K8 R2 (Enterprise Edition)
The simplest way to send a subscription to all reports users by e-mail is to set up the subscription on an e-mail group, and add (and remove) users from that e-mail group as desired.
The simplest way to send a subscription to all reports users by file share is to set up the subscription on a file share, and grant (and remove) access to that file share as desired.
I don't know of any way to set up the equivalent of a batch subscription. However, it should be possible to write a stored procedure to update existing schedules or create new schedules in the ReportServer database, based on a user ID passed to the query as a parameter.
You can see examples of queries that access the subscription tables on the ReportServer database here.
I found the solution following this link where rs.exe is used. Esentially it's example from BOL. I've changed it to suit my needs and it works. Usage:
rs.exe -i CreateSubscriptionTest.rss -s http://myreportserveraddresshere/reportserver
' CreateSubscriptionTest.rss
Public Sub Main()
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim desc As String = "Report description"
Dim eventType As String = "TimedSubscription"
Dim scheduleXml As String = "<ScheduleDefinition><StartDateTime>2012-03-22T09:30:00</StartDateTime><WeeklyRecurrence><WeeksInterval>1</WeeksInterval><DaysOfWeek><Thursday>True</Thursday></DaysOfWeek></WeeklyRecurrence></ScheduleDefinition>"
Dim extensionParams(7) As ParameterValue
extensionParams(0) = New ParameterValue()
extensionParams(0).Name = "TO"
extensionParams(0).Value = "my.address#email.com"
extensionParams(1) = New ParameterValue()
extensionParams(1).Name = "ReplyTo"
extensionParams(1).Value = "admin#email.com"
extensionParams(2) = New ParameterValue()
extensionParams(2).Name = "IncludeReport"
extensionParams(2).Value = "True"
extensionParams(3) = New ParameterValue()
extensionParams(3).Name = "RenderFormat"
extensionParams(3).Value = "MHTML"
extensionParams(4) = New ParameterValue()
extensionParams(4).Name = "Subject"
extensionParams(4).Value = "#ReportName was executed at #ExecutionTime"
extensionParams(5) = New ParameterValue()
extensionParams(5).Name = "Comment"
extensionParams(5).Value = "Some HTML code inside email's body<br><br>Go!"
extensionParams(6) = New ParameterValue()
extensionParams(6).Name = "IncludeLink"
extensionParams(6).Value = "True"
extensionParams(7) = New ParameterValue()
extensionParams(7).Name = "Priority"
extensionParams(7).Value = "NORMAL"
Dim parameters() As ParameterValue
' If you need setup parameters
'Dim parameter As New ParameterValue()
'parameter.Name = "EmpID"
'parameter.Value = "288"
'parameters(0) = parameter
'parameter.Name = "ReportMonth"
'parameter.Value = "12"
'parameters(1) = parameter
'parameter.Name = "ReportYear"
'parameter.Value = "2003"
'parameters(2) = parameter
Dim matchData As String = scheduleXml
Dim extSettings As New ExtensionSettings()
extSettings.ParameterValues = extensionParams
extSettings.Extension = "Report Server Email"
Dim returnValue As String
Dim reports() As String = { _
"/MyReports/Executive/SalesYear", _
"/MyReports/Executive/SalesMonth", _
"/MyReports/Executive/SalesWeek"}
For Each report As String In reports
returnValue = rs.CreateSubscription(report, extSettings, desc, eventType, matchData, parameters)
Console.WriteLine(returnValue)
Next
End Sub 'Main
The key part is to define reports() variable with report names. Name is the full report path.
The above example creates new schedule for each subscription. It's getting very messy in SQL Server Agent with a lot of reports schedules, so I changed it to use shared schedule. The difference is in scheduleXml declaration - you use schedule ID instead of XML string:
Dim scheduleXml As String = "924b9bb6-2340-4f5c-a897-465af7ff310e"
How can you obtain the schedule ID:
-- using T-SQL
SELECT
ScheduleID,
Name
FROM ReportServer.dbo.Schedule
WHERE
EventType = 'SharedSchedule'
' using rss file: GetSchedulers.rss
' run with rs.exe -i GetSchedulers.rss -s http://myreportserveraddresshere/reportserver
Public Sub Main()
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim returnValue As Schedule()
returnValue = rs.ListSchedules()
For Each sch As Schedule In returnValue
Console.WriteLine(sch.ScheduleID & " - " & sch.name)
Next
End Sub 'Main