Problem sending email

Last post 11-19-2009 8:26 AM by israa17. 7 replies.

Sort Posts:

  • Problem sending email

    11-07-2009, 6:26 PM
    • Member
      155 point Member
    • akaii
    • Member since 12-22-2005, 8:53 AM
    • Posts 76

    Hi guys,

    Could someone help me on this please, I followed what was explained in the tutorial video for sending email in www.asp.net but I get this error :

    http://screencast.com/t/37mxw6kvTGot

    Here is the code :

    Imports System.Net.Mail
    Partial Class frmDemandeCompte
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            hlAccueil.Text = "Retour page de login"
            hlAccueil.NavigateUrl = HttpContext.Current.Application("defaultLoginPage")
    
            lblEmailUtilisateur.Text = HttpContext.Current.Application("lblEmailUtilisateur")
            lblEmailUtilisateurConfirm.Text = HttpContext.Current.Application("lblEmailUtilisateurConfirm")
            lblNom.Text = HttpContext.Current.Application("lblNom")
            lblObjet.Text = HttpContext.Current.Application("lblObjetEmail")
            lblPrenom.Text = HttpContext.Current.Application("lblPrenom")
            ccBody.Content = "Merci de me créer un compte <br />" + txtNom.Text + " " + txtPrenom.Text
        End Sub
        Protected Sub btnEnvoyer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnvoyer.Click
            SendMail()
        End Sub
        Private Sub SendMail()
            Dim mailMessage As System.Net.Mail.MailMessage
    
            mailMessage.From = New MailAddress("clasdf@gmail.com")
            mailMessage.To.Add(New MailAddress("xaven@hotmail.fr"))
            mailMessage.Subject = txtObjet.Text.Trim()
            mailMessage.Body = ccBody.Content.Trim()
    
            Dim smtpClient As New SmtpClient()
            Dim userState As Object = mailMessage
    
            AddHandler smtpClient.SendCompleted, AddressOf smtpClient_OnCompleted
            smtpClient.SendAsync(mailMessage, userState)
        End Sub
        Public Sub smtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
            Dim mailMessage As MailMessage
    
            mailMessage = CType(e.UserState, MailMessage)
    
            If (e.Cancelled) Then
                lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To(0).Address
            End If
            If Not (e.Error Is Nothing) Then
                lblMessage.Text = "Error occured, info :" + e.Error.Message
            Else
                lblMessage.Text = "Mail sent successfully"
            End If
        End Sub
    End Class
     
    Thanks in advance


     

  • Re: Problem sending email

    11-07-2009, 6:45 PM
    • All-Star
      98,232 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,355
    • Moderator
      TrustedFriends-MVPs

    you forgot to create an instance of your mailmessage

    you can do it like this:

        Dim mailMessage As System.Net.Mail.MailMessage
        mailMessage = New System.Net.Mail.MailMessage

    or like this:

        Dim mailMessage As New System.Net.Mail.MailMessage

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Problem sending email

    11-07-2009, 8:09 PM
    Answer
    • Participant
      1,279 point Participant
    • bit2009
    • Member since 01-01-2009, 7:56 PM
    • kolkata
    • Posts 309

    try this

     

    Step 1:

    Public Sub SendEmail(ByVal Toaddress as string, ByVal body as string, ByVal subject as string, ByVal ishtml as Boolean)

     

                Dim Msg As New MailMessage

     

                            Try  

                      Msg.From = New MailAddress(ConfigurationManager.AppSettings("EmailFromAddres").ToString, 

                                         ConfigurationManager.AppSettings("EmailFromAddressDisplayName").ToString)

                      Msg.To.Add(New MailAddress(Toaddress))

                       Msg.Subject = subject

                      Msg.Body = body

                      Msg.IsBodyHtml = ishtml

                      Msg.Priority = MailPriority.Normal

                      ' to connect to the exchange server by using info in the web.config

                      Dim client As New SmtpClient(ConfigurationManager.AppSettings("ExchangeServerName").ToString())

                client.DeliveryMethod = SmtpDeliveryMethod.Network

                      client.UseDefaultCredentials = True

                      client.Port = ConfigurationManager.AppSettings("ExchangeServerPort").ToString()

                      client.Timeout = ConfigurationManager.AppSettings("ExchangeServerTimeout").ToString()

                      'send the email

                      Try

                            client.Send(Msg)

                      Catch ex As Exception

     

                            Exit Sub

                      End Try

     

                Catch ex As Exception

     

                Finally

                      Msg = Nothing

                End Try

     

          End Sub

     

    Step 2:

     

    change your web.config to include the following information about your company's exchange server

     

    <configuration>
         <appSettings>
            <add key="ExchangeServerName" value="CWcwcw"/>
            <add key="ExchangeServerPort" value="25"/>
            <add key="ExchangeServerTimeout" value="999999"/>
            <add key="EmailFromAddres" value="something@somedomain.com"/>
            <add key="EmailFromAddressDisplayName" value="Mr Someone"/>
            
        </appSettings>


    </configuration>

    Mark as Answer if this reply helps you

    Rajarshi Sankar Pain
  • Re: Problem sending email

    11-08-2009, 3:26 AM
    • Member
      155 point Member
    • akaii
    • Member since 12-22-2005, 8:53 AM
    • Posts 76

    jitendramcu I can't see your link.

    Now i get this error message :

    Error occured, info :Le serveur SMTP requiert une connexion sécurisée ou le client n'était pas authentifié. La réponse du serveur était : 5.7.0 Must issue a STARTTLS command first

    Here is what I have in my webconfig file :

    <system.net>
        <mailSettings>
          <smtp>
            <network host="smtp.live.com"  port="25" userName ="xaadsn@hotmail.fr" password="********" />
          </smtp>
        </mailSettings>
      </system.net>

    What did I miss?


  • Re: Problem sending email

    11-08-2009, 4:27 AM
    Answer
    • All-Star
      59,913 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    check this out

    http://aspsnippets.com/post/2009/01/18/Send-Email-using-VBNet.aspx

    Download the sample code and use it

  • Re: Problem sending email

    11-08-2009, 12:13 PM
    Answer
    • Contributor
      3,896 point Contributor
    • BryianTan
    • Member since 02-03-2008, 5:50 PM
    • VA
    • Posts 780

    hello,

    We need to enable SSL for it to work, here is a working sample.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
            Dim mailMessage As New System.Net.Mail.MailMessage
    
            mailMessage.To.Add(New MailAddress("toSomeone@gmail.com"))
            mailMessage.From = New MailAddress("fromMe@hotmail.com")
    
            mailMessage.Subject = "my test subject"
            mailMessage.Body = "my test body"
            mailMessage.IsBodyHtml = True
    
            Dim smtpClient As New SmtpClient()
            smtpClient.EnableSsl = True
    
            Dim userState As Object = mailMessage
    
            AddHandler smtpClient.SendCompleted, AddressOf smtpClient_OnCompleted
            smtpClient.SendAsync(mailMessage, userState)
    
        End Sub
    
    
     Public Sub smtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
            Dim mailMessage As MailMessage
    
            mailMessage = CType(e.UserState, MailMessage)
    
            If (e.Cancelled) Then
                lblMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To(0).Address
            End If
            If Not (e.Error Is Nothing) Then
                lblMessage.Text = "Error occured, info :" + e.Error.Message
            Else
                lblMessage.Text = "Mail sent successfully"
            End If
        End Sub


    Thanks,

    Bryian Tan

    Bryian Tan
    MCP, MCAD
    Blog
  • Re: Problem sending email

    11-10-2009, 10:45 AM
    • Member
      30 point Member
    • Baranboz
    • Member since 03-09-2009, 12:01 PM
    • Posts 68

     

    Hey;

    I'm so sorry because I don't know VB but I use this code for sending email for c#. I hope you can understand and use it for yourself:

     

    string from = "*********@gmail.com"; //Replace this with your own correct Gmail Address

    string to = useremail.Text; //Replace this with the Email Address to whom you want to send the mail

    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

    mail.To.Add(to);

    mail.From = new MailAddress(from, "Your Topic Message", System.Text.Encoding.UTF8);

    mail.Subject = "Write the subject of your email";

    mail.SubjectEncoding = System.Text.Encoding.UTF8;

    string strBody = "<p>YOU </p>" +

    "<p>CAN</p>" +

    "<p>WRITE WHATEVER</p>" +

    "<p>YOU WANT AS A BODY OF YOUR MESSAGE</p>" +

    "<p>Thank you!</p>" +

    "<p>Best Regards</p>";

     mail.Body = strBody;  //Put your mail body to strBody to show it in your email...

    mail.BodyEncoding = System.Text.Encoding.UTF8;

    mail.IsBodyHtml = true;

    mail.Priority = MailPriority.High;  //Your Email will have a priority so it will have red "!"

    SmtpClient client = new SmtpClient();

    //Add the Creddentials- use your own email id and password

     client.Credentials = new System.Net.NetworkCredential(from, "YOUR EMAIL'S PASSWORD");

    client.Port = 587; // Gmail works on this port

    client.Host = "smtp.gmail.com";

    client.EnableSsl = true; //Gmail works on Server Secured Layer

    try

    {

    client.Send(mail);

    }

    catch (Exception ex)

    {

    Exception ex2 = ex;

    string errorMessage = string.Empty;

    while (ex2 != null)

    {

    errorMessage += ex2.ToString();

    ex2 = ex2.InnerException;

    }

    HttpContext.Current.Response.Write(errorMessage);

    } // end try

     

    Hope, it works for you as well... If there is a problem and doesn't work, just let me know because may be I copied that code wrong from my application.

     

    Baran Bozoglu

  • Re: Problem sending email

    11-19-2009, 8:26 AM
    • Member
      429 point Member
    • israa17
    • Member since 11-19-2009, 8:24 AM
    • Cairo,Egypt
    • Posts 84

    hi baranboz , thank you very much , u really solved my problem with your C# code i was searching for


    and your code is working perfectly no copying mistakes with it , thanks alot

    Israa Saleh,

    Please Mark as answer if it was helpful for you
Page 1 of 1 (8 items)