Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to send mail from asp .net.
following is the code.

Imports System.Net
Imports System.Net.Mail

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub btnSend_Click(sender As Object, e As System.EventArgs) Handles btnSend.Click
        Dim msgEmail As New MailMessage()
        Dim smtpEmail As New SmtpClient()
        Dim address As New MailAddress(TextBox1.Text.ToString)

        Try
            'Adding to address
            If Not String.IsNullOrEmpty(TextBox1.Text.ToString) Then
                msgEmail.[To].Add("target Mail id")
                msgEmail.From = address
                'Adding subject
                msgEmail.Subject = "test"
                'Adding body
                msgEmail.Body = "This is a test mail"
                'Creating object of SMTP class

                smtpEmail.Host = "smtp.gmail.com"
                smtpEmail.Port = 587
                smtpEmail.EnableSsl = True
                smtpEmail.UseDefaultCredentials = True
                smtpEmail.Credentials = New NetworkCredential(TextBox1.Text.ToString, TextBox2.Text.ToString)

                'Sending message
                smtpEmail.Send(msgEmail)

                Response.Write("<script>alert('Message sent successfully.');</script>")
            Else
                Response.Write("<script>alert('Enter To address.');</script>")
            End If
        Catch ex As Exception
            Response.Write("<script>alert('" + ex.Message + "');</script>")
        Finally
            msgEmail.Dispose()
        End Try
    End Sub

End Class


In Textbox1 and Textbox2, i am providing my username and password respectively.
But when i click on 'Send' button then Exception occurs as "Failure Sending Mail"
Can anybody please help me to solve this problem?
Thanks in advance.
Posted
Comments
Shambhoo kumar 27-Sep-12 2:52am    
smtpEmail.Port = 25
use this..

VB
Imports System.Net
Imports System.Web.Mail

.........

Dim mail As New MailMessage()
mail.From = "From Address"
mail.To = "To Address"

mail.Cc = "CC Address"
mail.Bcc = "BCC Address"

mail.Subject = "Test Subject"
mail.Body = "Test Email Body"
mail.BodyFormat = MailFormat.Html

mail.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "UserName"


mail.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"

SmtpMail.SmtpServer = "Smtp server"

If mail.Body.Trim <> "" Then
    SmtpMail.Send(mail)
End If



Set all email address and credential.

Try this one and reply me.. it's working or not..?
 
Share this answer
 
v3
Comments
Vipul J Patel 27-Sep-12 5:19am    
hi abhijit...

This solution is working fine in My application.

if it's working fine in your application than please accept this solution.

Thanks,
Vipul
Hi Abhijit,

Please try " smtpEmail.Port = 25" this.

Thanks,
Vipul
 
Share this answer
 
Comments
Abhijit Parab 27-Sep-12 2:03am    
I tried as per your solution but still it is not working....
showing same error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900