send mail

imranbajwa

New member
Joined
Mar 5, 2008
Messages
1
Location
Germany
Programming Experience
Beginner
hi
i want to send email form a web page.

Dim objMail As New MailMessage("bajwa@hotmail.com", "imranbajwa14@hotmail.com", "test", "This is a test mail")
Dim clint As New SmtpClient("localhost")
clint.Send(objMail)

but i found a message "Failure sending mail." SmtpException was unhandled bz user code

how can i handle this problem.
bye
 
I've never send an e-mail using localhost. I think that his your problem. Try the following.

You need to include "import System.Web.Mail" then try using the following code replacing appropriately.

Dim mail as New MailMessage()
mail.To = "xxxx@mailserver.com"
mail.Bcc = "yyyy@mailserver.com; user3@mailserver.net"
mail.From = "your email or name"
mail.Subject = "your subject"
mail.Body = "your subject"
'******************
'your real server goes here or
'******************
SmtpMail.SmtpServer = "10.4.1.2";
'******************
'you can add the address like "smtp.mailserver.com"
'******************
SmtpMail.Send(mail);
 
I have some experiences when I make sendemail to mysite...

The first you define some configurations in web.config file. like this:
(plz replace some info due to your hosting.)

VB.NET:
  <system.net>
    <mailSettings>
      <smtp from="">
        <network host="mail.damxuanthai.edu.vn" password="xxxxxx"
          userName="info@damxuanthai.edu.vn" />
      </smtp>
    </mailSettings>
  </system.net>

Then you make a class called Email.cls:
VB.NET:
Imports Microsoft.VisualBasic
Imports System.Net.Mail

Public Class clsMail
    Sub New()

    End Sub
    Public Function SendMail(ByVal fromEmail As String, ByVal ToEmail As String, ByVal EmailTitle As String, ByVal EmailContent As String) As String
        Dim strError As String = ""
        Try
            Dim mm As New MailMessage(fromEmail, ToEmail)
            mm.Subject = EmailTitle
            mm.Body = EmailContent
            mm.IsBodyHtml = False
            Dim smtp As New SmtpClient
            smtp.Send(mm)
        Catch ex As Exception
            strError = ex.ToString
        End Try
        Return strError
    End Function
End Class

Finally you use this class when send email like this:
VB.NET:
Protected Sub PSendEmail_btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PSendEmail_btnSend.Click
        Dim clsSendMail As New clsMail
        PSendEmail_lblStatus.Text = clsSendMail.SendMail(TextBox_from.Text, TextBox_To.Text, TextBox_Title.Text, TextBox_what.Text)
        If PSendEmail_lblStatus.Text = "" Then
            PSendEmail_lblStatus.Text = "Email was sent"
        End If
    End Sub

This code run well both on Local host (debuging mode) and website mode.

Hope this help.
 
Back
Top