locked
email wont send, please help! RRS feed

  • Question

  • User-467845116 posted

    Hi,

    I am trying to send email from a VP page using code I found online. When I try and send it (after a button is clicked) I get this error: "Unable to read data from the transport connection: net_io_connectionclosed." I have searched and found this is a pretty common error and that it can be anything from not being authorized to send mail to not being able to connect to the mail server. Here is my code:

            Dim message As New MailMessage
            Dim SmtpClient As New SmtpClient

            SmtpClient.Host = "mail.domain.com"
            SmtpClient.Port = "25"
            SmtpClient.Credentials = New System.Net.NetworkCredential("user", "pass")

            message.To.Add("send_to")
            message.From = New MailAddress("from_address")
            message.Subject = "Subject Email"
            message.Body = "<H2>Testing HTML Email</H2>"
            message.IsBodyHtml = True
            SmtpClient.Send(message)

    What is the best way to send mail? What am I doing wrong? I am not running my own SMTP server (just connecting to the mail server). Any and all help is appreciated!!!

     
    Thanks!
     

    Tuesday, September 4, 2007 2:57 AM

Answers

  • User3423363 posted
    hope this code solves ur prob

     

     
    Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
    Download C# System.Net.Mail Helper
    Download VB.NET System.Net.Mail Helper
    Calling the function from code
    MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
    MailHelper.cs
    using System.Net.Mail;
    public class MailHelper
    {
       /// <summary>
       /// Sends an mail message
       /// </summary>
       /// <param name="from">Sender address</param>
       /// <param name="to">Recepient address</param>
       /// <param name="bcc">Bcc recepient</param>
       /// <param name="cc">Cc recepient</param>
       /// <param name="subject">Subject of mail message</param>
       /// <param name="body">Body of mail message</param>
       public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
       {
          // Instantiate a new instance of MailMessage
          MailMessage mMailMessage = new MailMessage();
          // Set the sender address of the mail message
          mMailMessage.From = new MailAddress(from);
          // Set the recepient address of the mail message
          mMailMessage.To.Add(new MailAddress(to)); 
          // Check if the bcc value is null or an empty string
          if ((bcc != null) && (bcc != string.Empty))
          {
             // Set the Bcc address of the mail message
             mMailMessage.Bcc.Add(new MailAddress(bcc));
          }
          // Check if the cc value is null or an empty value
          if ((cc != null) && (cc != string.Empty))
          {
             // Set the CC address of the mail message
             mMailMessage.CC.Add(new MailAddress(cc));
          }       // Set the subject of the mail message
          mMailMessage.Subject = subject;
          // Set the body of the mail message
          mMailMessage.Body = body; 
          // Set the format of the mail message body as HTML
          mMailMessage.IsBodyHtml = true;
          // Set the priority of the mail message to normal
          mMailMessage.Priority = MailPriority.Normal;
          // Instantiate a new instance of SmtpClient
          SmtpClient mSmtpClient = new SmtpClient();
          // Send the mail message
          mSmtpClient.Send(mMailMessage);
       }
    }
    MailHelper.vb
    Imports System.Net.Mail
    Public Class MailHelper
       ''' <summary>
       ''' Sends an mail message
       ''' </summary>
       ''' 
    <param name="<span" class="st">"from">Sender address ''' <param name="recepient"&gt;Recepient address</param> ''' <param name="<span" class="st">"bcc">Bcc recepient ''' <param name="cc"&gt;Cc recepient</param> ''' <param name="<span" class="st">"subject">Subject of mail message ''' <param name="body"&gt;Body of mail message</param> Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String) ' Instantiate a new instance of MailMessage Dim mMailMessage As New MailMessage() ' Set the sender address of the mail message mMailMessage.From = New MailAddress(from) ' Set the recepient address of the mail message mMailMessage.To.Add(New MailAddress(recepient)) ' Check if the bcc value is nothing or an empty string If Not bcc Is Nothing And bcc <> String.Empty Then ' Set the Bcc address of the mail message mMailMessage.Bcc.Add(New MailAddress(bcc)) End If ' Check if the cc value is nothing or an empty value If Not cc Is Nothing And cc <> String.Empty Then ' Set the CC address of the mail message mMailMessage.CC.Add(New MailAddress(cc)) End If ' Set the subject of the mail message mMailMessage.Subject = subject ' Set the body of the mail message mMailMessage.Body = body ' Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = True ' Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal ' Instantiate a new instance of SmtpClient Dim mSmtpClient As New SmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) End Sub End Class Web.config "1.0"?> <configuration> <system.net> <mailSettings> <smtp from="<span" class="st">"defaultEmail@yourdomain.com"> <network host="<span" class="st">"smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/> </network> </smtp> </mailSettings> </system.net>   <configuration> <system.net> <mailSettings> <smtp from="defaultEmail@yourdomain.com"> <network host="smtp.yourdomain.com" port="25" username="yourUserName" password="yourPassword"> </network> </smtp> </mailSettings> </system.net></configuration></configuration>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 15, 2008 2:16 AM

All replies

  • User-1318822910 posted

    Try using SmtpMail.  It works for me. [:)]

     

      SmtpMail.SmtpServer = "your email server";
    MailMessage msg = new MailMessage();
    msg.From = "from";
    msg.To = "to";
    msg.Subject = "subject";
    msg.Body = "html body";

    SmtpMail.Send(msg);
      
    Tuesday, September 4, 2007 3:55 AM
  • User-467845116 posted

    When I copy your code it says that SmtpMail is not declared - how do I declare it? 

    Tuesday, September 4, 2007 2:34 PM
  • User-467845116 posted
    I am .Net 2.0 and not sure if that is the correct class to use.
    Tuesday, September 4, 2007 2:38 PM
  • User-1318822910 posted

    Hi, you are correct. SmtpMail is deprecated for ASP.NET 2.0.

    Try this link for sending mail in ASP.NET 2.0

     http://www.codeproject.com/useritems/EmailApplication.asp

     

    Good luck!

    Tuesday, September 4, 2007 8:01 PM
  • User-989915499 posted

    Hi There,

    Try example in the give link:

    http://systemnetmail.com/faq/4.2.aspx

    You were mention that you are using mailserver to send your email. What mail server are you using ? your workplace ? or freemail ?

    Sometimes, mail server block user from using to send mail.

    Hope it helps!

    Wednesday, September 5, 2007 12:09 AM
  • User-698857717 posted
      SmtpMail.SmtpServer = "your email server";
    MailMessage msg = new MailMessage();
    msg.From = "from";
    msg.To = "to";
    msg.Subject = "subject";
    msg.Body = "html body";

    SmtpMail.Send(msg);
      



    This should work for you. You just need to add a namespace i.e.

    System.Net.Mail
    Wednesday, September 5, 2007 12:27 AM
  • User1920306027 posted

    hi

    here i a code which i tried but it is in C#, convert according to your VB

    for this purpose you need to import namspace using System.Net.Mail;

                                      string to = txtEmail.Text;

                                    string from = "xyz@abc.com";
                                    string subject = "Test mail...";


                                    MailMessage msg = new MailMessage(from, to);

                                    msg.Subject = subject;
                                    msg.IsBodyHtml = true;
                                                                                                                                                                                                               
                                    msg.Body = "<html><body>Hi, Test mail....</body></html>";


                                    SmtpClient smtp = new SmtpClient();
                                    try
                                    {
                                      smtp.Send(msg);
                                    }
                                    catch (Exception ex)
                                    {
                                        lblMsg.Text = "Failure in sending mail" ;  
                                    }

    and in your Web.Config file

    <system.net>
      <mailSettings>
       <smtp>
        <network host="smtp.registeredsite.com" Port="25 "userName="" password=""/>
       </smtp>
      </mailSettings>
     </system.net>

     

     

    Wednesday, September 5, 2007 1:01 AM
  • User-467845116 posted

    I actually came across that link and used the same code and was unable to connect. I go the same error. The mail server I am using is one hosted by my bank of servers - so I have complete control of it. It's SmarterMail. (mail.domain.com) and should work. For some reason it is just not authorized to send. When it authorizes the email, we are trying to "authorize" the username of the email, correct? What iI mean is a user on that mail.domain.com account and not a windows server user, right?

    Thanks for your help, this is the last item I need to fix for this project!
     

    Wednesday, September 5, 2007 2:05 AM
  • User-2067655963 posted

    Public Function SendMail()

    Try Dim objMailMsg As New System.Web.Mail.MailMessage

    objMailMsg.To = strMailTo'' it can be equal to some address strMailTo="abc@hotmail.com"

    objMailMsg.From = strMail_From_Address

    objMailMsg.Subject = strSubject

    objMailMsg.Body = strMailMessage

    System.Web.Mail.SmtpMail.SmtpServer = strSMTP_Server_IP

    objMailMsg.BodyFormat = MailFormat.Html

    System.Web.Mail.SmtpMail.Send(objMailMsg)

    end sub 

    Wednesday, September 5, 2007 9:00 AM
  • User-1509636757 posted

    problem with emails - ASP.NET Forums

    Send Mail - The transport failed to connect to the server - ASP.NET Forums

    The transport failed to connect to the server - ASP.NET Forums

    Send Mail in ASP.NET 2005 Using C# - ASP.NET Forums

    How to send an email with attachment in asp.net 2.0 - ASP.NET Forums

    send email - ASP.NET Forums

     

    code to Send Mail:

    C#

    using System.Net;
    using System.Net.Mail;
    -----------------------

    public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
    {
    try
    {
    // Create the mail message
    MailMessage objMailMsg = new MailMessage(strFrom, strTo);

    objMailMsg.BodyEncoding = Encoding.UTF8;
    objMailMsg.Subject = strSubject;
    objMailMsg.Body = strMsg;
    objMailMsg.Priority = MailPriority.High;
    objMailMsg.IsBodyHtml = true;

    //prepare to send mail via SMTP transport
    SmtpClient objSMTPClient = new SmtpClient();
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    objSMTPClient.Send(objMailMsg);
    return true;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

     
    VB:

    Public Shared Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMsg As String) As Boolean
    Try

    ' Create the mail message
    Dim objMailMsg As MailMessage = New MailMessage(strFrom, strTo)

    objMailMsg.BodyEncoding = Encoding.UTF8
    objMailMsg.Subject = strSubject
    objMailMsg.Body = strMsg
    objMailMsg.Priority = MailPriority.High
    objMailMsg.IsBodyHtml = True

    'prepare to send mail via SMTP transport
    Dim objSMTPClient As SmtpClient = New SmtpClient()
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
    objSMTPClient.Send(objMailMsg)
    Return True
    Catch
    ex As Exception
    Throw ex
    End Try
    End Function
     
       
    Code to send mail with attechment.,

    public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
    {
    try
    {
    // Create the mail message
    MailMessage objMailMsg = new MailMessage(strFrom, strTo);

    objMailMsg.BodyEncoding = Encoding.UTF8;
    objMailMsg.Subject = strSubject;
    objMailMsg.Body = strMsg;
    Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
    objMailMsg.Attachments.Add(at);
    objMailMsg.Priority = MailPriority.High;
    objMailMsg.IsBodyHtml = true;

    //prepare to send mail via SMTP transport
    SmtpClient objSMTPClient = new SmtpClient();
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    objSMTPClient.Send(objMailMsg);
    return true;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    hope it helps./.

     

    Wednesday, September 5, 2007 9:16 AM
  • User1664555100 posted

    Hi,

    You can use this code to send an Email using VB.NET

    VB.NET 2005

     <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

                   Dim from,email,subj,bodytext as String

                   Dim ipaddr As String

                   From=”sijo_raj@yahoo.com

                   Sub=”Hai”

                   Bodytext=”welcome”

                   ipaddr = “192.168.0.1”                                      ‘Your server’s IP Adsdress

                   Dim message As New MailMessage(from, email, subj, bodyText)

                   message.IsBodyHtml = True

                   Dim emailClient As New SmtpClient(ipaddr)

                   emailClient.Send(message)

     <o:p></o:p> <o:p></o:p> <o:p></o:p>

    VB.NET 2003

     <o:p></o:p>

                   objMessage = New Mail.MailMessage

     <o:p></o:p>

                   Mail.SmtpMail.SmtpServer = 192.168.0.1

                   objMessage.BodyFormat = Mail.MailFormat.Html

     <o:p></o:p>

                   objMessage.To = “joy@saccs.com”

                   objMessage.From = Sijonraj@gmail.com

     <o:p></o:p> <o:p></o:p>

                   objMessage.Headers.Add("Reply-To", “abc@yahoo.com”)

                   objMessage.Subject = “sbjt here”

                   objMessage.Body =” mailbody here”

                   Mail.SmtpMail.Send(objMessage)

     <o:p></o:p>

                                                                                                                                                                                                SIJO N RAJ

                                                                                                  .NET Programmer
                                                                                                  Kerala,India
                                                                                                  sijo_raj@yahoo.com
                                                                                                
    Wednesday, November 21, 2007 10:50 PM
  • User-1661396188 posted

    wil u please guide me in sending emails?

    Friday, April 11, 2008 6:39 AM
  • User-1661396188 posted

    please give me a sample code if we are having an gridview , on that i placed an edit link, after clicking on that edit link , email should fired to the email id present on that selected record and in the messege body we should get that selected grid with its column name

    Please guide me

    Friday, April 11, 2008 6:43 AM
  • User-1661396188 posted

    error comes like it is not obsolute

    Friday, April 11, 2008 6:51 AM
  • User-1509636757 posted

    please give me a sample code if we are having an gridview , on that i placed an edit link, after clicking on that edit link , email should fired to the email id present on that selected record and in the messege body we should get that selected grid with its column name
     

    first, you should start a new thread / post to get quick answer...

    second, can you pls post your code what you have reached till now ?!
     

    Friday, April 11, 2008 7:24 AM
  • User-1661396188 posted

     

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

    Try

    Dim row As GridViewRow

    row = GridView1.Rows(CInt(e.CommandArgument))

    Dim uname As String

    Dim con As New SqlConnection

    Dim cmd As New SqlCommand

    Dim SQL As String

    Dim conString As String

    conString = ConfigurationManager.ConnectionStrings.Item("ConfRmBkngConnectionString").ConnectionString

    con.ConnectionString = conString

    con.Open()

    SQL =
    "UPDATE requestrooms SET reqStatus='R' WHERE reqid=" & CInt(row.Cells(0).Text)

    uname = row.Cells(1).Text

    uid = row.Cells(0).Text

    cmd.Connection = con

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    Dim nr As Integer

    nr = cmd.ExecuteNonQuery()

    SQL = "SELECT * FROM requestrooms"

    cmd.Connection = con

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    cmd.ExecuteReader()

    If nr > 0 Then

    SQL = "SELECT [ReqID], [ReqUserID], [ReqDate], [ReqTimeIn], [ReqTimeOut], [RoomID],[ReqMeetDesc],[ReqRemarks] "

    SQL = SQL & " FROM [RequestRooms] WHERE [ReqId]=" & uid

    Dim Adaptor As New SqlDataAdapter(SQL, con)

    Dim ds As New Data.DataSet

    Adaptor.Fill(ds)

    GridView2.DataSource = ds

    GridView2.DataBind()

    GridView2.Visible = True

    GridView1.Visible = True

    Dim msg As New MailMessage("arti.manathkar@gmail.com", "Arti.manathkar@saint-gobain.com")

    Dim mySmtp As New Net.Mail.SmtpClient()

    Dim sb As New StringBuilder

    Dim swr As New StringWriter(sb)

    Dim html As New HtmlTextWriter(swr)

    GridView2.RenderControl(html)

    GridView1.RenderControl(html)

    msg.Body =
    String.Concat("<p> Your request has been Rejected </p>", sb.ToString)

    msg.IsBodyHtml = True

    mySmtp.Send(msg)

    ExtractemaidADUser(uname)

    Response.Redirect(
    "email1.aspx?id=" & uname)

    End If

    SQL = "SELECT * FROM requestrooms"

    cmd.Connection = con

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    cmd.ExecuteReader()

    SqlDataSource2.DataBind()

    Catch ex As Exception

    Response.Redirect("ErrorPage.aspx?e=" & ex.Message)

    End Try

    End Sub

    Tuesday, April 15, 2008 2:10 AM
  • User-1661396188 posted

      Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

    Try

    Dim row As GridViewRow row = GridView1.Rows(CInt(e.CommandArgument))

    Dim uname As String

    Dim con As New SqlConnection Dim cmd As New SqlCommand

    Dim SQL As String

    Dim conString As String

    conString = ConfigurationManager.ConnectionStrings.Item("ConfRmBkngConnectionString").ConnectionString

    con.ConnectionString = conString

    con.Open()

    SQL =
    "UPDATE requestrooms SET reqStatus='R' WHERE reqid=" & CInt(row.Cells(0).Text)

    uname = row.Cells(1).Text

    uid = row.Cells(0).Text

    cmd.Connection = con

    please give me a sample code if we are having an gridview , on that i placed an edit link, after clicking on that edit link , email should fired to the email id present on that selected record and in the messege body we should get that selected grid with its column name
     

    first, you should start a new thread / post to get quick answer...

    second, can you pls post your code what you have reached till now ?!
     

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    Dim nr As Integer

    nr = cmd.ExecuteNonQuery()

    SQL = "SELECT * FROM requestrooms"

    cmd.Connection = con

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    cmd.ExecuteReader()

    If nr > 0 Then

    SQL = "SELECT [ReqID], [ReqUserID], [ReqDate], [ReqTimeIn], [ReqTimeOut], [RoomID],[ReqMeetDesc],[ReqRemarks] "

    SQL = SQL & " FROM [RequestRooms] WHERE [ReqId]=" & uid

    Dim Adaptor As New SqlDataAdapter(SQL, con)

    Dim ds As New Data.DataSet

    Adaptor.Fill(ds)

    GridView2.DataSource = ds

    GridView2.DataBind()

    GridView2.Visible = True

    GridView1.Visible = True

    Dim msg As New MailMessage(xyz@yahoo.com, "abc@yahoo.com")

    Dim mySmtp As New Net.Mail.SmtpClient()

    Dim sb As New StringBuilder

    Dim swr As New StringWriter(sb)

    Dim html As New HtmlTextWriter(swr)

    GridView2.RenderControl(html)

    GridView1.RenderControl(html)

    msg.Body =
    String.Concat("<p> Your request has been Rejected </p>", sb.ToString)

    msg.IsBodyHtml = True

    mySmtp.Send(msg)

    ExtractemaidADUser(uname)

    Response.Redirect(
    "email1.aspx?id=" & uname)

    End If

    SQL = "SELECT * FROM requestrooms"

    cmd.Connection = con

    cmd.CommandType = Data.CommandType.Text

    cmd.CommandText = SQL

    cmd.ExecuteReader()

    SqlDataSource2.DataBind()

    Catch ex As Exception Response.Redirect("ErrorPage.aspx?e=" & ex.Message)

    End Try

    End Sub

    Tuesday, April 15, 2008 2:10 AM
  • User3423363 posted
    hope this code solves ur prob

     

     
    Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.
    Download C# System.Net.Mail Helper
    Download VB.NET System.Net.Mail Helper
    Calling the function from code
    MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")
    MailHelper.cs
    using System.Net.Mail;
    public class MailHelper
    {
       /// <summary>
       /// Sends an mail message
       /// </summary>
       /// <param name="from">Sender address</param>
       /// <param name="to">Recepient address</param>
       /// <param name="bcc">Bcc recepient</param>
       /// <param name="cc">Cc recepient</param>
       /// <param name="subject">Subject of mail message</param>
       /// <param name="body">Body of mail message</param>
       public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
       {
          // Instantiate a new instance of MailMessage
          MailMessage mMailMessage = new MailMessage();
          // Set the sender address of the mail message
          mMailMessage.From = new MailAddress(from);
          // Set the recepient address of the mail message
          mMailMessage.To.Add(new MailAddress(to)); 
          // Check if the bcc value is null or an empty string
          if ((bcc != null) && (bcc != string.Empty))
          {
             // Set the Bcc address of the mail message
             mMailMessage.Bcc.Add(new MailAddress(bcc));
          }
          // Check if the cc value is null or an empty value
          if ((cc != null) && (cc != string.Empty))
          {
             // Set the CC address of the mail message
             mMailMessage.CC.Add(new MailAddress(cc));
          }       // Set the subject of the mail message
          mMailMessage.Subject = subject;
          // Set the body of the mail message
          mMailMessage.Body = body; 
          // Set the format of the mail message body as HTML
          mMailMessage.IsBodyHtml = true;
          // Set the priority of the mail message to normal
          mMailMessage.Priority = MailPriority.Normal;
          // Instantiate a new instance of SmtpClient
          SmtpClient mSmtpClient = new SmtpClient();
          // Send the mail message
          mSmtpClient.Send(mMailMessage);
       }
    }
    MailHelper.vb
    Imports System.Net.Mail
    Public Class MailHelper
       ''' <summary>
       ''' Sends an mail message
       ''' </summary>
       ''' 
    <param name="<span" class="st">"from">Sender address ''' <param name="recepient"&gt;Recepient address</param> ''' <param name="<span" class="st">"bcc">Bcc recepient ''' <param name="cc"&gt;Cc recepient</param> ''' <param name="<span" class="st">"subject">Subject of mail message ''' <param name="body"&gt;Body of mail message</param> Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String) ' Instantiate a new instance of MailMessage Dim mMailMessage As New MailMessage() ' Set the sender address of the mail message mMailMessage.From = New MailAddress(from) ' Set the recepient address of the mail message mMailMessage.To.Add(New MailAddress(recepient)) ' Check if the bcc value is nothing or an empty string If Not bcc Is Nothing And bcc <> String.Empty Then ' Set the Bcc address of the mail message mMailMessage.Bcc.Add(New MailAddress(bcc)) End If ' Check if the cc value is nothing or an empty value If Not cc Is Nothing And cc <> String.Empty Then ' Set the CC address of the mail message mMailMessage.CC.Add(New MailAddress(cc)) End If ' Set the subject of the mail message mMailMessage.Subject = subject ' Set the body of the mail message mMailMessage.Body = body ' Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = True ' Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal ' Instantiate a new instance of SmtpClient Dim mSmtpClient As New SmtpClient() ' Send the mail message mSmtpClient.Send(mMailMessage) End Sub End Class Web.config "1.0"?> <configuration> <system.net> <mailSettings> <smtp from="<span" class="st">"defaultEmail@yourdomain.com"> <network host="<span" class="st">"smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/> </network> </smtp> </mailSettings> </system.net>   <configuration> <system.net> <mailSettings> <smtp from="defaultEmail@yourdomain.com"> <network host="smtp.yourdomain.com" port="25" username="yourUserName" password="yourPassword"> </network> </smtp> </mailSettings> </system.net></configuration></configuration>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 15, 2008 2:16 AM
  • User-1661396188 posted

     

    . Dim msg As New MailMessage(xyz@yahoo.com, sss@gmail.com)

     Dim mySmtp As New Net.Mail.SmtpClient()

    Dim sb As New StringBuilder

    Dim swr As New StringWriter(sb)

    Dim html As New HtmlTextWriter(swr)

     GridView1.RenderControl(html)

    msg.Body = String.Concat(" The Approval Details of the Request", sb.ToString) msg.IsBodyHtml = True mySmtp.Send(msg)

     msg.IsBodyHtml = True

    mySmtp.Send(msg)

     

     

     

    Tuesday, April 15, 2008 5:44 AM
  • User3423363 posted

     

    Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

    Download C# System.Net.Mail Helper
    Download VB.NET System.Net.Mail Helper

    Calling the function from code

    MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")

    MailHelper.cs

    using System.Net.Mail;<o:p></o:p>

    public class MailHelper
    {
       /// <summary>
       ///
    Sends an mail message
       
    /// </summary>
       ///
    <param name="from">Sender address</param>
       ///
    <param name="to">Recepient address</param>
       ///
    <param name="bcc">Bcc recepient</param>
       ///
    <param name="cc">Cc recepient</param>
       ///
    <param name="subject">Subject of mail message</param>
       ///
    <param name="body">Body of mail message</param>
       
    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
       {
          // Instantiate a new instance of MailMessage
          
    MailMessage mMailMessage = new MailMessage();
    <o:p></o:p>

          // Set the sender address of the mail message
          
    mMailMessage.From = new MailAddress(from);
          // Set the recepient address of the mail message
          
    mMailMessage.To.Add(new MailAddress(to));
    <o:p></o:p>

          // Check if the bcc value is null or an empty string
          
    if ((bcc != null) && (bcc != string.Empty))
          {
             // Set the Bcc address of the mail message
             
    mMailMessage.Bcc.Add(new MailAddress(bcc));
          }
    <o:p></o:p>

          // Check if the cc value is null or an empty value
          
    if ((cc != null) && (cc != string.Empty))
          {
             // Set the CC address of the mail message
             
    mMailMessage.CC.Add(new MailAddress(cc));
          }
          // Set the subject of the mail message
          
    mMailMessage.Subject = subject;
          // Set the body of the mail message
          
    mMailMessage.Body = body;

          // Set the format of the mail message body as HTML
          
    mMailMessage.IsBodyHtml = true;
          // Set the priority of the mail message to normal
          
    mMailMessage.Priority = MailPriority.Normal;

          // Instantiate a new instance of SmtpClient
          
    SmtpClient mSmtpClient = new SmtpClient();
          
    // Send the mail message
          
    mSmtpClient.Send(mMailMessage);
       }
    }

    MailHelper.vb

    Imports System.Net.Mail<o:p></o:p>

    Public Class MailHelper
       ''' <summary>
       
    ''' Sends an mail message
       '''
    </summary>
       
    ''' <param name="from">Sender address</param>
       
    ''' <param name="recepient">Recepient address</param>
       
    ''' <param name="bcc">Bcc recepient</param>
       
    ''' <param name="cc">Cc recepient</param>
       
    ''' <param name="subject">Subject of mail message</param>
       
    ''' <param name="body">Body of mail message</param>
       
    Public Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
          ' Instantiate a new instance of MailMessage
          
    Dim mMailMessage As New MailMessage()
    <o:p></o:p>

          ' Set the sender address of the mail message
          
    mMailMessage.From = New MailAddress(from)
          ' Set the recepient address of the mail message
          
    mMailMessage.To.Add(New MailAddress(recepient))
    <o:p></o:p>

          ' Check if the bcc value is nothing or an empty string
          
    If Not bcc Is Nothing And bcc <> String.Empty Then
             
    ' Set the Bcc address of the mail message
             
    mMailMessage.Bcc.Add(New MailAddress(bcc))
          End If
    <o:p></o:p>

          ' Check if the cc value is nothing or an empty value
          
    If Not cc Is Nothing And cc <> String.Empty Then
          
       ' Set the CC address of the mail message
          
       mMailMessage.CC.Add(New MailAddress(cc))
          End If
    <o:p></o:p>

          ' Set the subject of the mail message
       
       mMailMessage.Subject = subject
          ' Set the body of the mail message
          
    mMailMessage.Body = body
    <o:p></o:p>

          ' Set the format of the mail message body as HTML
          mMailMessage.IsBodyHtml = True
          
    ' Set the priority of the mail message to normal
          
    mMailMessage.Priority = MailPriority.Normal
    <o:p></o:p>

          ' Instantiate a new instance of SmtpClient
          
    Dim mSmtpClient As New SmtpClient()
          ' Send the mail message
          
    mSmtpClient.Send(mMailMessage)
       End Sub
    End
    Class
    <o:p></o:p>

    Web.config

    <?xml version="1.0"?>
    <
    configuration>
       <
    system.net>
          <
    mailSettings>
             <
    smtp from="defaultEmail@yourdomain.com">
                <
    network host="smtp.yourdomain.com" port="25" userName="yourUserName" password="yourPassword"/>
             </
    smtp>
          </
    mailSettings>
       </
    system.net>
    </
    configuration>

    <o:p> </o:p>

    Hope it gives complete solution
    Tuesday, April 15, 2008 7:34 AM
  • User-1661396188 posted
    Thanks for the help
    Wednesday, April 16, 2008 2:55 AM
  • User-1661396188 posted

    Hi Everyone

     My Email is now sending .

    But , if i want to send that email to the ldap user , what should i do?

    I m having the function extracting emailid from ldap, but i m unable to send it

     

    plz plz guide me in this problem

     

    THanks

    Wednesday, April 16, 2008 5:53 AM
  • User-1100948251 posted
    Hi dont confused on so many posts. Remember every one has own style of programming. I am going to tell you the best way that I used in more than 20 websites.
    Do the modifications:
    1st write the following tag in web.config after end of your connnection string tag :

    <system.net>
    <mailSettings>
    <smtp from="info@trannumdigitalstudio.com">
    <network host="127.0.0.1" userName="info@trannumdigitalstudio.com" password="--You-specifiy-your-own"/>
    </smtp>
    </mailSettings>
    </system.net>

    OK.
    Now go into form. Import Namespace System.Net.Mail. Now in the Send button click event, write:
    
    Dim myMail as New MailMessage();
    myMail.To="Your-client-email-address"
    myMail.Subject="Your Subject Line"
    myMail.Cc="write cc if any"
    
    Dim mailClient As New SmtpClient()
    mailClient.Send(myMail)
    Server.Transfer("EmailSendMessage.aspx")
    
    
    From: Gaurav Mahajan [Own website - www.sushilindia.com. Web Developer at Amritsar]
    Website Developer and Computer Lecturer
    
    Saturday, March 21, 2009 1:39 AM
  • User-1355816385 posted

    Sending mail using CDO

    SmtpClient smtpClient = new SmtpClient();
               
    string sbody;
    sbody = "<h1>Hello Mr/Ms. </h1>";
    CDO.Message oMsg = new CDO.Message();
    CDO.IConfiguration iConfg;
    iConfg = oMsg.Configuration;
    ADODB.Fields oFields;
    oFields = iConfg.Fields;
    oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
    oFields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 25;
    oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value ="127.0.0.1";
    oFields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "abc";
    oFields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "pass";
    oFields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 2;
    oFields["urn:schemas:mailheader:importanc e"].Value = "high"; 
    oFields.Update();
    oMsg.From = "abc";                
    oMsg.Subject = "Welcome";
    oMsg.To = " ";                
    oMsg.HTMLBody = sbody;
    oMsg.Send();
                  


     

    Sunday, April 11, 2010 3:29 PM