Brammam’s Weblog

Sending Mail without SMTP Server Details

August 14, 2008 · No Comments

Hi in this post i am telling how to send a mail without using smtp server. this is possible in web application i didn,t check in windows. By using System.web.Mail namespace it is possible

code in VB.Net

Imports System.Web.Mail

Public Shared Function SendEmail(ByVal FromAddress As String, ByVal ToAddress As String, ByVal CCAddress, ByVal BccAddress, ByVal EmailSubject As String, ByVal MessageText As String) As Boolean
    Try
            Dim objMailMessage As New MailMessage()
            Dim obj As SmtpMail
            obj.SmtpServer = “”
            objMailMessage.From = FromAddress
            objMailMessage.To = ToAddress
     If CCAddress.Length > 0 Then
             objMailMessage.Cc = CCAddress
            End If
            If BccAddress.Length > 0 Then
             objMailMessage.Bcc = BccAddress
            End If
            objMailMessage.Subject = EmailSubject
            objMailMessage.Body = MessageText
            objMailMessage.BodyFormat = MailFormat.Html
            obj.Send(objMailMessage)
            Return True
        Catch ex As Exception
            WriteErrorToLog(GetExecutingScriptName() & ” - SendMail”, ex.Message)
            Return False
        End Try
End Function

To send a mail call the above function as
SendEmail(txtFromEmailID.Text.Trim(), txtToEmailID.Text.Trim(),”",”", txtSubject.Text.Trim(), strEMailText.ToString())

 

Code in C#.Net

using System.Web.Mail;

public static Boolean SendMail(string FromAddress,string ToAddress, string CCAddress, string BccAddress,string MailSubject, string MailBody)
    {
        try
        {
            System.Web.Mail.MailMessage msgMail = new MailMessage();
            msgMail.From = FromAddress;
            msgMail.To = ToAddress;
            if (CCAddress.Length > 0)
            {
                msgMail.Cc = CCAddress;
            }
            if (BccAddress.Length > 0)
            {
                msgMail.Bcc = BccAddress;
            }
            msgMail.Subject = MailSubject;
            msgMail.Body = MailBody;
            msgMail.BodyFormat = MailFormat.Html;
            SmtpMail.SmtpServer = “”;
            SmtpMail.Send(msgMail);

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

call this function as

SendEmail(txtFromEmailID.Text.Trim(), txtToEmailID.Text.Trim(),”",”", txtSubject.Text.Trim(), strEMailText.ToString())

We can attach a file also to this mail.

→ No CommentsCategories: Uncategorized

Exporting grid to Excel

August 13, 2008 · No Comments

This is to get the gridview values of your page in to microsoft excel. Recently i tried this. we can export values from gridview or DataList or any DataRenderControl. For this code is as follows..

first in aspx page put a gridview and a button to export to excel and in page load bind data to gridview.

in button click write this code.

 Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles

 btnExcel.Click  
Response.ContentType = “application/vnd.ms-excel”

 Dim objStringWriter As New IO.StringWriter   

Dim objHtmlTextWriter As New Html32TextWriter(objStringWriter)gdvDetails.RenderControlobjHtmlTextWriter)
 Response.AppendHeader( “Content-Disposition”, “attachment; filename=Details.xls”)
Response.Write(objStringWriter)
Response.Flush()
Response.End()

 End Sub

 

 

 

 when you click on this button it gives a popup like “download file menu” with the name “Details.xls”. you can open or save into your system

→ No CommentsCategories: Uncategorized

Client IP Address

July 9, 2008 · 1 Comment

when ever you want to find the IP Adress of client use one of these two Response.Write(Request.UserHostAddress) or
Response.Write(Request.ServerVariables(”LOCAL_ADDR”))
But remember one thing you have to access this web page with server address as

http://yourSyatemIP(or)Servername/projectName/page.aspx
not with localhost like 
http://localhost/projectName/page.aspx

Below code is to find the Different types of server variables

Dim NameArray() As String
Dim VisitorSV As NameValueCollection
Dim I As Integer
VisitorSV = Request.ServerVariables
NameArray = VisitorSV.AllKeys
For I = 0 To UBound(NameArray)
Response.Write(”<br>”)
Response.Write(NameArray(I) & “: ” & VisitorSV.Item(I) & “<BR>”)
Next

→ 1 CommentCategories: Uncategorized

How to get the project path in .net?

June 25, 2008 · No Comments

System.AppDomain.CurrentDomain.BaseDirectory

This returns the string of your project path including project name

ex: c:\Inetpub\wwwroot\mysite\

→ No CommentsCategories: Uncategorized

Email validation

May 31, 2008 · No Comments

checking email validation by java script.

aspx page

<script language=”javascript” type=”text/javascript”>

function checkmail()

{

var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

     var MailID = document.getElementById(’txtMailID’);

     var returnval=emailfilter.test(MailID.value)

     if (returnval==false)

      {

         alert(”Please enter a valid email address.”);

        MailID.focus();
      }

     return returnval;

}

<script>

<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” />

then add an attribute in codebehind file to submit buttion

 btnSubmit.Attributes.Add(

“OnClick”, “return checkmail();”
 its equal to email validator in .net

→ No CommentsCategories: Java Script