It is not uncommon in a project to implement a functionality of sending out email from an ASP.Net page in the form of a Feedback or something similar. Here's how you can leverage the Web.Mail class to build a simple yet powerful functionality:
Imports System.Web.Mail
Public Class SendEmail
Private Sub cmdSendEmail_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdSendEmail.Click
Dim objMail As New MailMessage()
objMail.From = Me.txtFrom.Text.ToString
objMail.To = Me.txtTo.Text.ToString
objMail.Subject = Me.txtSubject.Text.ToString
objMail.Body = Me.txtMessage.Text.ToString
objMail.BodyFormat = MailFormat.Html
SmtpMail.Send(objMail)
Me.lblMessage.Text = "Email has been sent"
End Sub
End Class
*********************************************************************************
Below is the code I wrote for a Windows app using Framework 1.1
Public Sub SendEmail(ByVal sFromEmailAddress As String, ByVal sToEmailAddress As String, _
ByVal sEmailSubject As String, ByVal sEmailBody As String)
Dim objMail As New MailMessage
Try
SmtpMail.SmtpServer.Insert(0, "MY_SERVER")
objMail.From = sFromEmailAddress
objMail.To = sToEmailAddress
objMail.Subject = sEmailSubject
objMail.Body = sEmailBody
objMail.BodyFormat = MailFormat.Text
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0")
SmtpMail.Send(objMail)
Catch ex As Exception
Throw ex.InnerException
End Try
End Sub
***********************************************************************************
While the above is a plain vanilla code to do in framework 1.1. However, to do it in 2.0 is little different. Please see below:
It seems that the protocol for sending email in .NET 2.0 has changed from what it was in 1.1. The System.Web.Mail namespace has been replaced with the new System.Net.Mail names code samplepace. The following is a C# code sample on how to send mail with 2.0:
System.Net.Mail.SmtpClient o_Client = new System.Net.Mail.SmtpClient(this.MailServer);
System.Net.Mail.MailAddress o_FromAddress = new System.Net.Mail.MailAddress(this.FromEmail, this.FromName);
System.Net.Mail.MailAddress o_ToAddress = new System.Net.Mail.MailAddress(this.ToEmail, this.ToEmail);
System.Net.Mail.MailMessage o_Message = new System.Net.Mail.MailMessage(o_FromAddress, o_ToAddress);
o_Message.Subject = this.Subject;
o_Message.Body = this.Body;
o_Client.Send(o_Message);
The properties contained in “this“ should be obvious. As you can see, we now get a “friendly” send name so we no longer have to rig it as we did in 1.1.
VB.Net Version in .Net 2.0
Private Sub SendEmail(ByVal MsgFrom As String)
Dim objMM As New MailMessage(MsgFrom, "ToAddress@mail.com")
'Create the SmtpClient object
Dim smtp As New SmtpClient
'Assign the MailMessage's properties
objMM.Subject = txtSubject.Text
objMM.Body = txtBody.Text
objMM.IsBodyHtml = False
objMM.CC.Add(CELL_PHONE) 'Constant value
objMM.CC.Add(WEB_MASTER) 'Constant value
'Send the MailMessage
smtp.Send(objMM)
End Sub
Obi Oberoi
In my current project I was required to read the entire data from a DataGridView (VS.Net 2005) and dump it onto an Excel spreadsheet. Here's how:
Before you start working with Excel, add an Excel reference to your project by selecting 'Microsoft Excel 9.0 Object Library' or later.
The following code simply iterates through the rows and columns of a DataGridView, adds the contents to a StringBuilder object and then writes to an excel file and finally opens the excel file.
Try
Dim iRow, iCol As Int16
Dim value As String
Dim col0 As String = String.Empty
Dim col1 As String = String.Empty
Dim sbHeader As New Text.StringBuilder
Dim sbMain As New Text.StringBuilder
sbHeader.Append("ID," & vbTab & "Expense")
sbHeader.Append(vbCrLf)
For iRow = 0 To grdCustomer.RowCount - 1
For iCol = 0 To grdCustomer.ColumnCount - 1
value = grdCustomer.Item(iCol, iRow).Value.ToString
Select Case iCol
Case 0
col0 = value
Case 1
col1 = value
sbMain.Append(col0 & vbTab & col1)
sbMain.Append(vbCrLf)
End Select
Next
Next
Print("test.xls",sbHeader, sbMain)
Catch ex As Exception
ex.Message.ToString()
End Try
Friend Sub Print(ByVal fileName As String, ByVal sbHeader As StringBuilder, ByVal sbMain As StringBuilder)
Try
Dim dirName As String = DIRECTORY_NAME
If Not Directory.Exists(dirName) Then
Directory.CreateDirectory(dirName)
End If
Dim filePath As String = dirName & fileName
System.IO.File.WriteAllText(filePath, sbHeader.Append(sbMain).ToString)
xlBook = xlApp.Workbooks.Open(filePath)
xlApp.Visible = True
Catch ex As Exception
Throw
End Try
Cheers,
Obi Oberoi