sending email in from asp.net page - asp.net

I want to add this to my email body and i want it to look like this but it doesnt work.
Tour: lotour.text
Date: loddate.text
Party: lolnumparty.text
first name: locfname.text
last name: loclname.text
as you see i want them right after another and it doesnt work when i use a </br>
this is my email body.
objEmail.Body = "There was a booking rquest made by " & Request.QueryString("comp") & " to see more details click the link " + x
this is my full code
If Page.IsValid And ValidateCCNumber(cardnumber.Text) = True Then
SqlDataSource1.Insert()
Dim x As String
x = "http://www.clubabc.com/bookingrequest/confirm.aspx?date=" & HttpUtility.UrlEncode(now.Text) & "&tfname=" & HttpUtility.UrlEncode(p1fname.Text) & "&tlname=" & HttpUtility.UrlEncode(p1lname.Text) & "&comp=" & HttpUtility.UrlEncode(Request.QueryString("comp") & "&land=" & HttpUtility.UrlEncode(land.Text))
Dim objEmail As New MailMessage()
objEmail.To = "cnna#BC.com"
objEmail.From = "page#bc.com"
objEmail.Cc = memail.Text
objEmail.Subject = "Booking for " + p1fname.Text + " " + p1lname.Text + " made by " & Request.QueryString("comp")
objEmail.Body = "There was a booking rquest made by " & Request.QueryString("comp") & " to see more details click the link " + x
SmtpMail.SmtpServer = "mail.bc.com"
Try
SmtpMail.Send(objEmail)
Catch exc As Exception
Response.Write("Send failure: " + exc.ToString())
End Try
Response.Redirect("http://www.clubabc.com/bookingrequest/confirm.aspx?date=" + now.Text + "&tfname=" + p1fname.Text + "&tlname=" + p1lname.Text + "&comp=" + Request.QueryString("comp") & "&land=" & HttpUtility.UrlEncode(land.Text))
ElseIf ValidateCCNumber(cardnumber.Text) = False Then
invalidcard.Visible = True
End If

I guess you are searching for (MSDN):
Environment.NewLine
The <br> tag will only work in HTML Emails.
Update
This would be a very simple example, how you could use Environment.NewLine:
Imports System
Class Sample
Public Shared Sub Main()
Dim firstName As String = "John"
Dim lastName As String = "Doe"
Dim city As String = "Brooklyn"
Console.WriteLine("First name: " + firstName + Environment.NewLine + "Last name: " + lastName + Environment.NewLine + "City: " + city + Environment.NewLine)
End Sub
End Class

Try to set IsHtml property for objEmail variable to get some more formatting possibilities.

Do not use Enviornment.NewLine, instead use vbCrLf
Environment.NewLine may only output Cr or Lf, which may cause your email to either:
a)Be blocked because it's not RFC complient
or
b)Be marked as spammy
To have new lines in email, always use CrLf.
--Dave

Related

One or more errors occurred during processing of command. Provider error '80040e14'

I am getting a Provider error '80040e14' when I do a LIKE on an LDAP query in Classic ASP.
My code is:
<%
response.Buffer = True
function get_ldap_query(firstname, lastname, unit)
dim ADUser, objCom, objCon, objRS
ADUser = "LDAP://OU=Staff,OU=Users,DC=example,DC=internal"
' Make AD connection and run query'
Set objCon = Server.CreateObject("ADODB.Connection")
objCon.provider ="ADsDSOObject"
objCon.Properties("User ID") = "EXAMPLE\test"
objCon.Properties("Password") = "example"
objCon.Properties("Encrypt Password") = TRUE
objCon.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objCon
objCom.CommandText = "SELECT givenName, sn, mail, telephonenumber, mobile, description, sAMAccountName, cn, UserAccountControl FROM '"+ ADUser + "' where (cn LIKE *" + firstname + "* OR cn like *" + lastname + "*) AND UserAccountControl <> 514 ORDER by cn ASC"
Set objRS = objCom.Execute
Response.Write "<table>" + vbCrLf
Do While Not objRS.EOF Or objRS.BOF
Response.Write " <tr>"
Response.Write "<td>" + objRS("givenName") + "</td>"
Response.Write "<td>" + objRS("sn") + "</td>"
Response.Write "<td>" + objRS("mail") + "</td>"
' Check if field is null to avoid error'
If IsNull(objRS.Fields("Description").Value) Then
sDesc = ""
else
For Each item In objRS.Fields("description").Value
sDesc = item + "<br>"
Next
end if
Response.Write "<td>" + sDesc + "</td>"
Response.Write "<td>" + objRS("mobile") + "</td>"
Response.Write "<td>" + objRS("telephonenumber") + "</td>"
if objRS("sAMAccountName") <> mid(Request.ServerVariables("AUTH_USER"), 11) then
'If the account name held in AD is different to the one the user is logged in with, a blank cell is output. If they are the same, a link allowing the user to edit their entry is displayed'
Response.Write "<td></td>"
else
Response.Write "<td><a href='entry.asp?account_name=" + objRS("sAMAccountName") + "'>Edit</a></td>"
end if
Response.Write "</tr>" + vbCrL
objRS.MoveNext
Response.Flush
Loop
Response.Write "</table>"
'Clean up'
objRS.Close
objCon.Close
Set objRS = Nothing
Set objCon = Nothing
Set objCom = Nothin
end function
%>
When this function is called from the index.asp page, which passes the data to the function, I get the error:
Provider error '80040e14'
One or more errors occurred during processing of command.
/pagelets/includes/results.asp, line 19
Which is
Set objRS = objCom.Execute
What am I doing wrong?
The problem is in the below line
objCom.CommandText = "SELECT givenName, sn, mail, telephonenumber, mobile, description, sAMAccountName, cn, UserAccountControl FROM '"+ ADUser + "' where (cn LIKE *" + firstname + "* OR cn like *" + lastname + "*) AND UserAccountControl <> 514 ORDER by cn ASC"
You are missing single quotes around the firstname and last name, it should look like below.
objCom.CommandText = "SELECT givenName, sn, mail, telephonenumber, mobile, description, sAMAccountName, cn, UserAccountControl FROM '"+ ADUser + "' where (cn LIKE '*" + firstname + "*' OR cn like '*" + lastname + "*') AND UserAccountControl <> 514 ORDER by cn ASC"
Your code sample also does not show how these are being set, so they could be empty, in which case you get ALL records, in which case you might be returning too many records. You need to add some logic to check if both are these are empty and exit gracefully, or limit the number of records returned somehow.

Attatch Gridview to email body VB.NET

I am currently sending emails programatically in my web page, the emails work fine but i would also like to attach a gridview into the email body.
this is my VB code I have for sending the email so far...
Dim Uname As String = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1)
strFm = Uname + "#Inscapepeople.co.uk"
If strTo <> "" Then
Dim insMail As New System.Net.Mail.MailMessage(New MailAddress(strFm), New MailAddress(strTo))
With insMail
For Each y As String In strToREST.Split(";")
If y = "" Then
Else
.CC.Add(y)
End If
Next
.Subject = subject
.Body = Greet + "<br />" + "<br />" + bodyTxt.Text + "<br />" + PName + "- " + PNum + "<br />" + "<br />" + "The Required Date was: " + ReqDateNow.Text + " ,The Required Date is now: " + ReqDateAfter.Text + "<br />" + "Regards," + "<br />" + "<br />"
.IsBodyHtml = True
Sig = Uname
If Sig.ToLower = "djones" Then Sig = "GBennett"
Dim source As String = Path + Sig + ".jpg"
If System.IO.File.Exists(source) = False Then
Msgbox.ShowAlertMessage("Signature not found, but the email was still sent.")
Else
Dim lr As New LinkedResource(source)
lr.ContentId = "Signature"
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(insMail.Body + "<img width=""500"" height=""400"" src=cid:Signature>", Nothing, "text/html")
htmlView.LinkedResources.Add(lr)
.AlternateViews.Add(htmlView)
End If
End With
Dim smtp As New System.Net.Mail.SmtpClient
smtp.EnableSsl = False
smtp.Host = "192.168.50.2"
smtp.Port = 25
smtp.UseDefaultCredentials = True
smtp.Send(insMail)
I have searched around online and i cannot find anything to help me, I was wondering if i could get some assistance with this, thank you in advance.
Any help at all would be appreciated.
I would suggest converting the gridview to Html using a function such as:
Private Function GridViewToHtml(ByVal gv As GridView) As String
Dim sb As New StringBuilder
Dim sw As New StringWriter
Dim hw As New HtmlTextWriter(sw)
gv.RenderControl(hw)
Return sb.ToString()
End Function
Then you could call this function and add it to the body of your email.

Read Outlook messages in ASP.NET catches out of bounds error

I am trying to integrate MS outlook in asp.net. My code(below) catches out of bounds error. What am I doing wrong? am I not getting the inbox messages properly? Thanks in advance
EDIT: based on my debugging
inbox = name.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Returns nothing. I am not sure why
Dim ol As Outlook.Application = New Outlook.Application
Dim name As Microsoft.Office.Interop.Outlook._NameSpace
Dim inbox As Microsoft.Office.Interop.Outlook.MAPIFolder
Dim item As Microsoft.Office.Interop.Outlook.PostItem
Dim subF As Microsoft.Office.Interop.Outlook.MAPIFolder
Try
name = ol.GetNamespace("MAPI")
name.Logon(Nothing, Nothing, False, False)
inbox = name.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
subF = inbox.Folders(0)
Dim ctr As Integer
For ctr = 0 To subF.Items.Count
item = subF.Items(ctr)
Label1.Text += "Subject: " + item.Subject + "<br/>"
Label1.Text += "Date: " + item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString() + "<br/>"
Label1.Text += "Category: " + item.Categories + "<br/>"
Label1.Text += "Body: " + item.Body + "<br/>"
Label1.Text += "HtmlBody: " + item.HTMLBody + "<br/>"
Next
Catch ex As System.Runtime.InteropServices.COMException
End Try
Check your index base
Change
For ctr = 0 To subF.Items.Count
to
For ctr = 1 To subF.Items.Count
Or possibly to
For ctr = 0 To subF.Items.Count - 1
...if Microsoft finally got rid of the base-1-indexing scheme with the Outlook objects.
Make sure there's actually a folder!
Check to ensure that the inbox contains subfolders. If not, use the inbox itself.
inbox = name.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox)
If inbox.Folders.Count > 0 Then
subf = inbox.Folders(1)
Else
subf = inbox
End If

Store code line in a string?

I need to store code in a string so that if a value is true, it is in the code line if not true its not in the code line. When I populate summarytextbox if consulting amount is "" then dont use this code if is does have an amount include the code. Is this possible? Other wise I would have to do a bunch if then statements. When I do the following below it cant convert to double.
Dim ConsultingFee As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("ConsultingFeeAmount") = "" Then
Else
'Store the following line in a string????
ConsultingFee = +Environment.NewLine + Session("ConsultingFee") + " Amount: " + Session("ConsultingFeeAmount")
End If
SummaryTextBox.Text = Session("TeachingHospital") + Environment.NewLine + Session("HospitalAddress") + Environment.NewLine + Session("HospitalCity") + Environment.NewLine + Session("HospitalState") + Environment.NewLine + Session("HospitalZip") + Environment.NewLine + Session("HospitalTIN") + ConsultingFee
End Sub
You need to concatenate onto the ConsultingFee string variable, like this:
ConsultingFee = ConsultingFee & Environment.NewLine & Session("ConsultingFee") & " Amount: " & Session("ConsultingFeeAmount")
OR
ConsultingFee &= Environment.NewLine & Session("ConsultingFee") & " Amount: " + Session("ConsultingFeeAmount")

How can I send mail from my localhost application using classic asp

I'm using this with my server to send mails. Which is working perfectly.I want to try it out on my localhost app that I made.
Set myMail=CreateObject("CDO.Message")
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/bodyformat") = 0 ' 0 - html, 1 - text
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/mailformat") = 0 ' 0 - mime, 1 - text
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "206.183.108.132"
myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
myMail.Configuration.Fields.Update
myMail.Subject = "Your New Password for Leave App"
myMail.From = rs("email")
myMail.To = "somename#domain.com"
msgg = msgg & "Dear" & " " & session("Username") & vbcrlf & vbcrlf
msgg = msgg & "This is your new password" & vbcrlf & vbcrlf
msgg = msgg & "YOUR CHANGED PASSWORD" & vbcrlf
msgg = msgg & "- - - - - - - - - - - - - - - - - - - - - - - - - - -" & vbcrlf
msgg = msgg & "User/Login Name :" & session("Username") & vbcrlf
msgg = msgg & "Password :" & request.Form("new_pass2") & vbcrlf
msgg = msgg & "- - - - - - - - - - - - - - - - - - - - - - - - - - -" & vbcrlf & vbcrlf
msgg = msgg & "Please sign in to your account using the user name and password above." & vbcrlf & vbcrlf
msgg = msgg & "Thanks" & vbcrlf
myMail.TextBody = msgg
myMail.Send
set myMail = nothing
Check if your local Machine can reach the SMTP server. It might be blocked in some firewall or router. This website can perform a simple check for you: http://www.canyouseeme.org/
Of course, you might want to check it in the code as well, or via Telnet: http://www.simplescripts.de/smtp-check-port-25-telnet-command.htm
Here is the contents of an include file I created for sending emails:
[note at the top it includes another include that has a fn_dirty() for the purpose of adding things like quotes back in.] ask me if you want that function.
<!-- #INCLUDE FILE = "i_fn_dirty.asp" -->
<%
function email(s_name_from,s_address_from,s_reply_to,s_subject,s_recipients_list,s_msg,s_type_email,s_msg_error_add,s_RemoteHost)
if (s_msg_error_add<>"") then s_msg_error_add = "<hr>" & vbCrLf & s_msg_error_add
if (s_RemoteHost="default") then s_RemoteHost = application("s_mail_server")
'recipients_list = "Scott,scott#changed.net;Andy,andy#changed.net" etc
array_recipients = split(s_recipients_list,";",-1,1)
'so recipients array now looks like this:
'array_recipients(0) = "Scott,scott#changed.net"
'array_recipients(1) = "Andy,andy#changed.net"
'-- Create the Mailer Object
Set Mailer = Server.CreateObject("SoftArtisans.SMTPMail")
'-- Set the Mail Properties
Mailer.RemoteHost = s_RemoteHost
Mailer.FromName = s_name_from
Mailer.FromAddress = s_address_from
if (s_reply_to<>"") then Mailer.ReplyTo = s_reply_to
Mailer.Subject = s_subject
a = ""
For Each Item in array_recipients
array_data = split(Item,",",-1,1)
s_name = array_data(0)
s_email_addr = array_data(1)
if (s_name<>"" and s_email_addr<>"") then
Mailer.AddRecipient s_name, s_email_addr
a = a & "name: " & s_name & ", email: " & s_email_addr & " | "
end if
Next
if (s_type_email = "text") then
Mailer.BodyText = s_msg
else
s_msg_html = replace(s_msg,vbCrLf,"<br>",1,-1,1)
Mailer.HTMLText = s_msg_html
end if
'-- Fire off the email message
if (Mailer.SendMail) then
'yay it worked
Set Mailer = Nothing
else
'try one more time
if (Mailer.SendMail) then
'yay it worked
Set Mailer = Nothing
else
msg = "<br>Error in i_fn_email.asp: " & Mailer.Response & "<br>"
msg = msg & "s_name_from = " & s_name_from & "<br>"
msg = msg & "s_address_from = " & s_address_from & "<br>"
msg = msg & "s_subject = " & s_subject & "<br>"
msg = msg & "recips list = " & a & "<br>"
msg = msg & s_msg_error_add
session("msg") = msg
Set Mailer = Nothing
response.redirect ("error_report.asp")
end if
end if
end function
%>

Resources