none
Mail handling through ASP.NET or VB.NET RRS feed

  • Question

  •  

    Hello Friends,

    Here is another query from my side.

    I want to generate mails in ASP.Net/VB.Net.

    How can I handle this functionality?

    Plz guide me.

    Tuesday, December 4, 2007 7:03 AM

Answers

  • "Generating" an e-mail message is no different than how you would deal with normal text/html but I assume your query is on how to send e-mails through ASP.NET (VB).

     

    Pretty much everything you would ever need to deal with e-mails is encapsulated in the System.Net.Mail namespace. I have put together a quick sample to send a simple plain text e-mail. Obviously, you will need to modify certain settings to suit your needs.

     

    Code Block

    Imports System.Net.Mail

     

    Public Sub SendEmail(ByVal address As String, ByVal message As String)

     

    Dim maFrom As MailAddress = New MailAddress("NOREPLY@blah.com", "Hello World!")

    Dim maTo As MailAddress = New MailAddress(address)

    Dim maReplyTo As MailAddress = New MailAddress("NOREPLY@blah.com", "Hello World!")

    Dim mm As MailMessage = New MailMessage(maFrom, maTo)

     

    mm.ReplyTo = maReplyTo

    mm.IsBodyHtml = True

    mm.Priority = MailPriority.Normal

    mm.Subject = "Hello World!"

    mm.Body = message

     

    Dim sc As SmtpClient = New SmtpClient("smtp.gmail.com", 587)

    sc.EnableSsl = True

    sc.Credentials = New System.Net.NetworkCredential("blah@gmail.com","password")

    sc.Send(mmBody)

     

    End Sub

     

    I use the gmail SMTP server to send my e-mails. You could easily run a local SMTP server and send e-mails locally as well. In that case, all you have to do is set the host to "localhost" and most likely you won't need any credentials, so leave it on default.

    Tuesday, December 4, 2007 2:22 PM

All replies

  • "Generating" an e-mail message is no different than how you would deal with normal text/html but I assume your query is on how to send e-mails through ASP.NET (VB).

     

    Pretty much everything you would ever need to deal with e-mails is encapsulated in the System.Net.Mail namespace. I have put together a quick sample to send a simple plain text e-mail. Obviously, you will need to modify certain settings to suit your needs.

     

    Code Block

    Imports System.Net.Mail

     

    Public Sub SendEmail(ByVal address As String, ByVal message As String)

     

    Dim maFrom As MailAddress = New MailAddress("NOREPLY@blah.com", "Hello World!")

    Dim maTo As MailAddress = New MailAddress(address)

    Dim maReplyTo As MailAddress = New MailAddress("NOREPLY@blah.com", "Hello World!")

    Dim mm As MailMessage = New MailMessage(maFrom, maTo)

     

    mm.ReplyTo = maReplyTo

    mm.IsBodyHtml = True

    mm.Priority = MailPriority.Normal

    mm.Subject = "Hello World!"

    mm.Body = message

     

    Dim sc As SmtpClient = New SmtpClient("smtp.gmail.com", 587)

    sc.EnableSsl = True

    sc.Credentials = New System.Net.NetworkCredential("blah@gmail.com","password")

    sc.Send(mmBody)

     

    End Sub

     

    I use the gmail SMTP server to send my e-mails. You could easily run a local SMTP server and send e-mails locally as well. In that case, all you have to do is set the host to "localhost" and most likely you won't need any credentials, so leave it on default.

    Tuesday, December 4, 2007 2:22 PM
  • Hi Rahul,

    Thnx for prompt reply.

     

    Tuesday, December 4, 2007 3:49 PM
  • IF ur making it for ur personal use.. i.e.    local host.. using c#

    then u can also use   name space.. using System.Web.Mail;

     

    Code Block

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Mail;
    using System.IO;


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            MailMessage mesg = new MailMessage();
            StringWriter striter = new StringWriter();
            HtmlTextWriter htxt = new HtmlTextWriter(striter);
            htxt.RenderBeginTag("html");
            htxt.RenderBeginTag("head");
            htxt.RenderBeginTag("title");
            htxt.Write("urmail@gmail.com");
            htxt.RenderEndTag();
            htxt.RenderEndTag();
            htxt.RenderBeginTag("body");
          
            htxt.WriteLine(TextBox2.Text);
            htxt.WriteEncodedText(TextBox2.Text);
           htxt.RenderEndTag();
            htxt.RenderEndTag();

            //  in case u want attachments with it..
            MailAttachment  att = new MailAttachment("C:\\Documents and Settings\\user\\My Documents\\Visual Studio 2005\\WebSites\\sendmail\\final.wmv");
            mesg.Attachments.Add(att);
           
            mesg.From ="admin<urmail@gmail.com>";
            //mesg.Priority = high;
            mesg.To = TextBox1.Text;
            mesg.Bcc = TextBox1.Text;
            mesg.Subject ="to chek";
            mesg.Body = striter.ToString();
            mesg.BodyFormat = MailFormat.Html;
            SmtpMail.Send(mesg);
        }
      }

     

     

    all the best..
    Wednesday, December 5, 2007 1:14 PM

  • You can download FREE EBOOKs on ASP.Nett/VB.Net from Tutorials, Ebooks, Source Codes, Question Bank and get all the codes and also get FREE Tutorials from this website http://allfree-stuff.com/education/indexEdu.php
     
    ASP.Net

    VB.Net


    i hope this will be of some help to you Dear

    Thursday, December 27, 2007 4:45 AM

  • Nice thrad Guys !!!!!!

    Sunday, January 20, 2008 5:10 PM