VB code to automatically send email in an ASP.NET app - html

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.

Related

ms access module to add attachment from button

I am having the following code in a module, which it is send email with attachment to user.
Public Sub EmailToUser()
Dim mail As Object ' CDO.MESSAGE
Dim config As Object ' CDO.Configuration
Set mail = CreateObject("CDO.Message")
Set config = CreateObject("CDO.Configuration")
config.Fields(cdoSendUsingMethod).Value = cdoSendUsingPort
config.Fields(cdoSMTPServer).Value = "my smtp server"
config.Fields(cdoSMTPServerPort).Value = 465
config.Fields(cdoSMTPConnectionTimeout).Value = 10
config.Fields(cdoSMTPUseSSL).Value = "true"
config.Fields(cdoSMTPAuthenticate).Value = cdoBasic
config.Fields(cdoSendUserName).Value = "e=mail"
config.Fields(cdoSendPassword).Value = "password"
config.Fields.Update
Set mail.Configuration = config
With mail
.To = "e-mail"
.From = "e-mail"
.Subject = "subject"
.AddAttachment strPathReport & FileName '<== My question.
.Send
End With
Set config = Nothing
Set mail = Nothing
End Sub
I have a form with 8buttons and each button is send an email with an attachment.
Now, I have in my module 8 times the same code with different attachment.
Is it possible to have only one time the above code and from the button to add the attachment?
Thank you.
Basically you want to add parameters to the procedure. And then pass in the e-mail address, etc. as arguments.
This question should give you some ideas on how to do that: multiple argument subs vba

Insufficient system storage. The server response was: 4.5.3 Too many recipients (AS780090)

Insufficient system storage. The server response was: 4.5.3 Too many recipients (AS780090)
Line 121: Dim mSmtpClient As New SmtpClient()
Line 122:
Line 123: mSmtpClient.Send(mMailMessage)
Public Function SendMail(ByVal emailto As String, ByVal strtitle As String, ByVal strmsg As String) As String
' Try
Dim mMailMessage As New MailMessage()
Dim from1 As String = "*******#*****.com"
' Set the sender address of the mail message
mMailMessage.From = New MailAddress(from1, strtitle)
' Set the recepient address of the mail message
mMailMessage.To.Add(New MailAddress(emailto, strtitle))
mMailMessage.Subject = strtitle
mMailMessage.Subject = strtitle
'mMailMessage.To.Add(New MailAddress(email).ToString)
mMailMessage.To.Add(New MailAddress(emailto).ToString)
' Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = True
'mMailMessage.Body = strmsg
mMailMessage.Body = strmsg
' Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.High
mMailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("windows-1256")
mMailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("windows-1256")
' Instantiate a new instance of SmtpClient
Dim mSmtpClient As New SmtpClient()
mSmtpClient.Send(mMailMessage)
End Function
What is solution for this error ??or where is my code wrong ??
I have added the code too ,please review my code !
There is some limit to how many recipients can be added in one single email. Recipients include:
To
CC
BCC
In gmail its set to 100 for different mail provider its diff value.
If you are sending more than that no of ID in single email then it will give this error

Trying to use a HTML file as my email body using iMsg in VB.NET

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

MS Access send email (not from outlook or user's email)

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.

Log4Net - Create an Smtpappender programmatically

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.