ASP.net 3.5 Website Using ASP:Wizard Control and SMTPClient does not send Email - asp.net-3.5

I have a asp:Wizard control on a site running Framework 3.5. I acquired the settings from the web host and have entered them into the Web Configuration Utility. Here's the code behind file:
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
Dim mySMTPClient As New SmtpClient()
Dim sb As New StringBuilder
Dim myMail As New MailMessage("webmaster#mydomain.com", "user#mydomain.com")
Dim myFrom As New MailAddress("webmaster#mydomain.com")
Dim myTo As New MailAddress("user#anotherdomain.com")
myMail.BodyEncoding = System.Text.Encoding.UTF8
myMail.IsBodyHtml = False
myMail.To.Add(myTo)
myMail.Subject = "Request for Information"
sb.Append("Contact Name: ")
sb.Append(txtcontactname.Text)
sb.Append("<br/>Phone Number: ")
sb.Append(txtcontactphone.Text)
sb.Append("<br/>Employer Name: ")
sb.Append(txtemployername.Text)
sb.Append("<br/>City: ")
sb.Append(txtcity.Text)
sb.Append("<br/>State: ")
sb.Append(cmbstate.Text)
sb.Append("<br/>Zip: ")
sb.Append(txtzip.Text)
sb.Append("<br/>Other Location: ")
sb.Append(txtotherloc.Text)
sb.Append("<br/>Nature of Business: ")
sb.Append(txtbusnat.Text)
sb.Append("<br/>Eligible Employees: ")
sb.Append(txteligemps.Text)
sb.Append("<br/>Average Employee Turnover Per Year: ")
sb.Append(txtempturnover.Text)
sb.Append("<br/>Broker Name: ")
sb.Append(txtbrokername.Text)
sb.Append("<br/>Broker Email: ")
sb.Append(txtbrokeremail.Text)
sb.Append("<br/>Proposed Effective Date: ")
sb.Append(txteffdate.SelectedDate)
sb.Append("<br/>Limited Benefit Medical Plans: ")
For Each item As ListItem In chkmedplans.Items
If (item.Selected) Then
sb.Append(item.Text)
sb.Append(" ")
End If
Next
sb.Append("<br/>Voluntary Products/Services: ")
For Each item As ListItem In chkvolserv.Items
If (item.Selected) Then
sb.Append(item.Text)
sb.Append(" ")
End If
Next
sb.Append("<br/>Employer Paid Products/Services: ")
For Each item As ListItem In chkempserv.Items
If (item.Selected) Then
sb.Append(item.Text)
sb.Append(" ")
End If
Next
sb.Append("<br/>Preferred Benefit Enrollment Program(s): ")
For Each item As ListItem In chkenrolprog.Items
If (item.Selected) Then
sb.Append(item.Text)
sb.Append(" ")
End If
Next
sb.Append("<br/>Comments: ")
sb.Append(txtcomments.Text)
myMail.Body = sb.ToString()
Try
mySMTPClient.Send(myMail)
Catch ex As Exception
Dim ex2 As Exception = ex
Dim errorMessage As String = String.Empty
While Not (ex2 Is Nothing)
errorMessage += ex2.ToString()
ex2 = ex2.InnerException
End While
Response.Write(errorMessage)
End Try
End Sub
End Class
The code complies with no error. When loaded onto the shared hosting account, the page loads and the code allows the user to enter information into the wizard. However, the Finish button does not fire the final step. Here's the code for the final wizard step:
<asp:WizardStep ID="WizardStep4" runat="server" StepType="Complete" Title="Complete">
Thank you for your inquiry. A member of our staff will contact you regarding your request.</asp:WizardStep>
I cannot determine what is cuasing this issue. Can someone direct me as to possible causes?
Thanks,
Sid

Have you set breakpoints within the code to see if the code to send mail is actually being reached? For example, I would suggest you set breakpoints at least at the following:
To verify the FinishButtonClick event is firing.
Dim mySMTPClient As New SmtpClient()
To verify the Sendmail code is being hit.
mySMTPClient.Send(myMail)
To see if any exception is occurring.
Dim ex2 As Exception = ex
Also, there are at least a couple of other things to look out for.
The SMTP server may not be configured or properly configured.
Even if the SMTP Server is properly configured, you want to make sure your firewall (or your ISPs) is not blocking the sending of SMTP mail.

Related

Prompt for customized message in mass-emails with Access VBA

I'm trying to put together a tool to send mass emails to a query of users from a database table. I'm using the SendObject method and it works well enough.
SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc, Subject, MessageText, EditMessage, TemplateFile)
I manually modify the VBA code when I need a new message; using vbNewLine to denote line breaks in the MessageText string field.
I want to create a prompt to ask the user to type in the message, and automatically format it with line breaks when the macro runs.
The emails are newsletters several paragraphs long tailored to each recipient (e.g. John Doe = Dear John Doe, Gary Stu = Dear Gary Stu).
SendObject can be set to prompt the user with an option to edit the email before sending it but each of the modifications is unique to each individual email.
You just need to make variables to generate body of email or subject or both.
For picking up data from recordset, assuming tblContactData table with first name, last name, emailID etc.
Dim strSubject as string, strSubjectLine as string
Dim strMessageBody as string, strFirstName as string, strLastName as string, recContactData as DAO.recordset
set recContactData = CurrentDB.OpenRecordset("Select * from tblContactData")
'Loop all the contacts for email
Do Until recContactData.EOF = True
' strSubjectLine = InputBox("Enter Subject Line", "Input")
strSubjectLine = "Ref No" & recContactData.Fields("ContactID")
strSubject = "Your email , " & strSubjectLine
'strFirstName = InputBox("Enter First Name", "Input")
strFirstName = recContactData.Fields("FirstName")
'strLastName = InputBox("Enter Last Name", "Input")
strLastName = recContactData.Fields("LastName")
strMessageBody = "Hello " & strFirstName & strLastName & vbNewLine & vbCrLf & " Let me first congratulate you for registering this program"
SendObject(, "", "", recContactData.Fields("EmailID"), "", "", strSubject, strMessageBody,false, "")
recContactData.MoveNext
Loop
You create a form that has two textboxes, txtFirstName, txtLastName. Then create a button and put this code in the button click event:
Try this VBA code:
Sub btnSend_Click()
Dim message as String
'Make sure to handle the case if textboxes are empty
message = "Dear " & txtFirstName.value & " " & txtLastName.value & ", " & _
vbNewLine & "Let me be the first to congratulate you on your offer!"
DoCmd.SendObject , "", "", rs![Email], "", "", "Congratulations on your hire!", message , False, ""
End Sub

Redemption in MS Access / Outlook (trying to include a separate message file in email)

This code is in MS Access (2010) VBA, using the Redemption library with Microsoft Outlook 2010.
I had this process working before, but we recently had a Citrix upgrade that I guess reset something in my Outlook and now the process no longer works.
I have a folder of .msg files which are basically pre-made email templates with all the proper formatting, images, text, etc.
This is what I was doing before:
Dim outlookApp As Object, namespace As Object
Dim oItem, MyItem
Set outlookApp = CreateObject("Outlook.Application")
Set namespace = outlookApp.GetNamespace("MAPI")
namespace.Logon
Set MyItem = outlookApp.CreateItemFromTemplate(path_to_dot_msg_file)
'And then there are many calls like this:
MyItem.HTMLBody = Replace(MyItem.HTMLBody, "Dear Person,", "Dear " & name)
'...
Set safeItem = CreateObject("Redemption.SafeMailItem")
Set oItem = MyItem
safeItem.Item = oItem
'this next line displays the email, and as of this line, it looks correct
'safeItem.Display
'but as of this line, the issue occurs
safeItem.HTMLBody = "<p>This is an extra message that shows up before the .msg file</p>" & safeItem.HTMLBody
safeItem.Recipients.ResolveAll
safeItem.Send
Now when the email is sent, the .msg contents aren't present at all -- the only thing that shows up is the "extra message" that I prepended to the HTMLBody.
What do I need to change or update? Is this something I need to change in the code, or in my Outlook settings, etc?
Extra: body insertion:
Function insertStringBodyTag(htmlBody As String, stringToInsert As String)
Dim s As String
Dim i As Integer
s = htmlBody
i = InStr(1, s, "<body")
i = InStr(i, s, ">")
s = Left(s, i) & stringToInsert & Right(s, Len(s) - i)
insertStringBodyTag = s
End Function
'Called with safeItem.htmlBody = insertStringBodyTag(safeItem.htmlBody, prefix_string)
You cannot concatenate 2 HTML strings and expect a valid HTML string back - the two must be merged - find the position of the "<body"substring in the original HTML body, then find the positon of the following ">" (this way you take care of the body element with attributes), then insert your HTML string following that ">".

Ms Access - Button with code doesn't work to send email

I have this code set in access, but no email is sending upon clicking the button on the form. I have outlook open. When i click the button on the form, i can't see anything that actually happens. I want the email address to be equal to the value in [text1], and I am trying to make the subject include a fixed message plus the input from [text2]. Even without these variables, I can't get this to work
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
mailto = [text1]
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [text2] & ", Does this work?"
On Error Resume Next
DoCmd.SendObject acSendNoObjectType, , acFormattxt, mailto, ccto, bccto, mailsubj, emailmsg, True
End Sub
I have checked to make sure the onclick property shows event procedure. I am stuck, please help!
Here are a few suggestions and a modified version of your code.
ALWAYS use Option Explicit and compile your module before testing. You had a number of variables that were not defined and incorrect spelling of some options.
NEVER bypass errors when testing (get rid of your "On Error Resume Next") That's why you never saw an error.
Look for every place I entered ">>>" and address that issue.
Always explicitly define your variables and use the proper Type. Removes all doubt of what/where something is.
Option Compare Database
Option Explicit
Public Sub Command495_Click()
Dim mailto As String
Dim ccto As String
Dim bccto As String
Dim emailmsg As String
Dim mailsub As String
mailto = [Text1] ' >>> Where is [Text1]?? Remove for testing
ccto = ""
bccto = ""
emailmsg = "trial"
mailsub = [Text2] & ", Does this work?" ' >>> Where is [Text2]?? Remove for testing
' >>> Bad idea to ignore errors when testing!!
On Error Resume Next
'>>> Following line had: (1) 'acSendNoObjectType' which is incorrect; (2) mailsubj, which is undefined
DoCmd.SendObject acSendNoObject, , acFormatTXT, mailto, ccto, bccto, mailsub, emailmsg, True
End Sub

VBA code to check if Outlook is Setup before sending mail

I have a code in Access where it will send an email to the person in charge when the user click on save button. The code will use Outlook.application to send the email.
The code works fine but if outlook is not setup (i.e. fresh install without any user account setup) then the my email code will get stuck until user reactivates Access to acknowledge the error.
Sub Send_Email()
Dim oApp As Outlook.Application
Dim oMail As MailItem
On Error GoTo MailErr
If IsNull(Email) Then
MsgBox "You do not have an email account! No email will be sent!" & vbNewLine & "Email updates will be sent to your supervisor!" Me.Email.Value = DLookup("[Email]", "tblEmployeeList", "EmpName = '" & Me.txtSupName & "'")
Else
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "IT Incident " & Me.ReqID & " has been created."
oMail.Subject = "Alert: New IT Incident"
oMail.to = Forms!MainForm!lblITAdminEmail.Caption
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End If
MailErr:
'MsgBox Err
If Err = 287 Then
AppActivate "Microsoft Access"
MsgBox "Error 287: Mail not sent! Pls contact IT/BI"
ElseIf Err <> 0 Then
MsgBox "Pls contact BI/IT admin! Error " & Err & " occured!"
End If
Set oMail = Nothing
Set oApp = Nothing
End Sub
Is there a way to use VBA to check if Outlook has been setup properly prior to running this code?
Assuming that you are using at least Outlook 2007; take a look at the DefaultProfileName property of your Outlook.Application Object. This will return an empty string if no profile has been created or if there is no default profile.
You could just check this, but I believe that it's possible that an Outlook profile could exist but no actual e-mail accounts are configured within it (for instance if the user aborted the set-up wizard part way through). In this instance you could look at the Accounts Object which includes a Count property. Obviously if this is 0 then you know there are no accounts configured within the profile.
A simple example of how you could implement this.
Dim oApp As Outlook.Application
Set oApp = Outlook.Application
If Not oApp.DefaultProfileName = "" Then
If oApp.Session.Accounts.Count > 0 Then
' Send the e-mail
End If
End If
Set oApp = Nothing

How to email multiple doclinks of those who are my direct reports in the view?

I have a question. For example there is one view and 10 documents are in that view. Out of all those documents, 8 of them I should be the recipient of the email (based on the field value which is my email address).
Now, what I want to happen is that I will be receiving only one email for all those 8 documents, and in that email I there will be 8 doclinks.
Is that possible?
'Cause currently I am getting 8 emails and for each email, there is one doclink. Thanks in advance for those who can help me.
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
doc.SendTo = addresses.abbreviated
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Call rtitem.appenddoclink(doc, "Link")
doc.Send (True)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
As Thorsten said, it can be done. There are a couple of ways to handle this, depending on how flexible and future proof you want it, and how "clean" of a solution you want.
Let's say you have 10 documents, 8 with your email and 2 with a different user's email. I assume you want to send one mail to you with 8 doc links and one to the other person with 2 doc links.
The way I would do it is to create a class. That class would contain a list of NotesDocuments (and a method to add documents to the list):
Class DocData
Public docs List As NotesDocument
Public Sub New()
End Sub
Public Sub Add(doc as NotesDocument)
Set docs(doc.UniversalID) = doc
End Sub
End Class
In your main code, you have a list of DocData objects, one per mail recipient:
Dim docs List As DocData
You now loop through the view or the document collection you have. You check the email address for each document and if there isn't a list item for that address, you create it and add the document to it. If it already exists, just add the document:
email = doc.GetItemValue("EmailAddress")(0)
If !IsElement(docs(email)) Then
Set docs(email) = New DocData()
End If
Call docs(email).Add(doc)
When all documents have been processed, you should have a list with one item per mail recipient, and you can loop through the list, build one email per item and populate it with doc links for all the documents in the list in the object.
For performance reasons, if you plan to have this working on larger views with more documents, I woudl suggest that you put the email address in one of the columns, and then use view entries to loop though the documents, and ColumnValues() to read the email address.
I wrote about it here: http://blog.texasswede.com/re-using-lotusscript-lists-to-increase-performance/
Your code can be slightly modified to only send one mail:
Dim s As NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim i As Integer
Dim view As NotesView
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("View")
Set doc = New NotesDocument(db)
Dim addresses As NotesName
i=0
'- prepare mail
doc.Form = "Memo"
Set rtitem = New NotesRichTextItem(doc, "Body")
Call rtitem.AppendText("Balance")
Set doc = view.GetFirstDocument
While Not(doc Is Nothing)
Set addresses = New NotesName(doc.Manager(0))
If addresses.abbreviated = "" Then
i = i + 1
Else
'- Set recipient
If not doc.HasItem( "SendTo" ) then
doc.SendTo = addresses.abbreviated
End If
'- Append descriptive text, link and new line
Call rtitem.appendtext(doc.Subject(0) & " " )
Call rtitem.appenddoclink(doc, "Link")
Call rtitem.addnewline(1)
i = i + 1
End If
Set doc = view.GetNextDocument(doc)
Wend
'- send mail
Call doc.Send (True)
with that code the mail is first prepared, then a doclink is added for every document, and in the end the mail is sent.