Saturday, March 24, 2012

smtpclient error

Hi

I want to send email from my website, I found that I can use Smtpclient class,

but error occurs saying this,

No connection could be made because the target machine actively refused it

Does anybody know why?

I will show u my code to make clear,

Dim mail As New MailMessage()
mail.From = New MailAddress("me@dotnet.itags.org.test.com")
mail.To.Add("mgkoko84@dotnet.itags.org.gmail.com")

'set the content

mail.Subject = "This is an email"
mail.Body = "this is a sample body"

testmail = New SmtpClient("localhost", 25)
testmail.UseDefaultCredentials = False
testmail.Send(mail)

thx in advance

MgKo

try this code... having some minor changes from your code... check it out...

'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@.mycompany.com")
mail.To.Add("you@.yourcompany.com")

'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."

'send the message
Dim smtp As New SmtpClient("127.0.0.1")

'to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = New NetworkCredential("username", "secret")
smtp.Send(mail)


Hisuresh modiya

Thank you for your reply, I appreciate it.

It does not work yet though.

Thank You

Best Regard

MgKo


Can you please tell me what is the problem or error encountered right now?


This is Ryan's code (strongtypes) you can't go wrong here:

Imports

System.Net.Mail

Public

ClassMailHelper
'''<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 SubSendMailMessage(ByValfromAsString,ByValrecepientAsString,ByValbccAsString,ByValccAsString,ByValsubjectAsString,ByValbodyAsString)
' Instantiate a new instance of MailMessage
DimmMailMessageAsNewMailMessage()

' Set the sender address of the mail message
mMailMessage.From =NewMailAddress(from)
' Set the recepient address of the mail message
mMailMessage.To.Add(NewMailAddress(recepient))

' Check if the bcc value is nothing or an empty string
IfNotbccIsNothingAndbcc <>String.EmptyThen
' Set the Bcc address of the mail message
mMailMessage.Bcc.Add(NewMailAddress(bcc))
EndIf

' Check if the cc value is nothing or an empty value
IfNotccIsNothingAndcc <>String.EmptyThen
' Set the CC address of the mail message
mMailMessage.CC.Add(NewMailAddress(cc))
EndIf

' 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
DimmSmtpClientAsNewSmtpClient()
' Send the mail message
mSmtpClient.Send(mMailMessage)
EndSub
EndClass

Web.config

<?

xmlversion="1.0"?>
<configuration>
<system.net>
<mailSettings>
<smtpfrom="defaultEmail@.yourdomain.com">
<networkhost="smtp.yourdomain.com"port="25"userName="yourUserName"password="yourPassword"/>
</smtp>
</mailSettings>
</system.net>
</configuration>

That will do it!

thnx,


Hi Suresh

Sorry, I didn't try properly

It doesn't work yet but the result changes to

The SMTP server requires a secure connection or the clientwas not authenticated. The server response was: 5.7.0 Must issue aSTARTTLS command first b30sm2769016ika

Do I have to configure IIS SMTP mail ?

here is my code.

Dim mail As New MailMessage()
mail.From = New MailAddress("mgkoko84@.gmail.com")
mail.To.Add("mgkoko84@.gmail.com")
mail.Subject = "This is an email"
mail.Body = "this is a sample body"
testmail = New SmtpClient("smtp.gmail.com", 25)
testmail.Host = "smtp.gmail.com"
testmail.UseDefaultCredentials = False
testmail.Credentials = New Net.NetworkCredential("name", "password")
testmail.Send(mail)

Thx again

MgKo


In this scenario, you just need to add:

<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" host="mail.blah.com"password="xxxx" port="25" userName="ex@.blah.com"/>
</smtp>
</mailSettings>
</system.net>

to your web.config and then you will be authenticated

regards,


Thank you revkev,

It still not working. ::(

I set true to EnableSsl

Dim mail As New MailMessage()
mail.From = New MailAddress("mgkoko84@.gmail.com")
mail.To.Add("mgkoko84@.gmail.com")
mail.Subject = "This is an email"
mail.Body = "this is a sample body"
testmail = New SmtpClient("smtp.gmail.com", 25) <<< If I Use 465, its shows ping time out, if I use 587 it show same error.
testmail.Host = "smtp.gmail.com"
testmail.UseDefaultCredentials = False
testmail.Credentials = New Net.NetworkCredential("mgkoko84", "r9r0nald0")
testmail.EnableSsl = True
testmail.Send(mail)

Thank You

MgKo


Thank you guys, I've got it

I've done it wrong 1st, thank you for your time

here is my code

Dim mail As New MailMessage()
mail.From = New MailAddress("mgkoko84@.gmail.com")
mail.To.Add("mgkoko84@.gmail.com")
mail.Subject = "This is an email"
mail.Body = "this is a sample body"
testmail = New SmtpClient()

Dim aut As New NetworkCredential("mgkoko84", "password")
testmail.Host = "smtp.gmail.com"
testmail.Port = 587
testmail.UseDefaultCredentials = False
testmail.Credentials = aut
testmail.EnableSsl = True
testmail.Send(mail)


Keep it up..... Finally you got the solution... best of luck......

0 comments:

Post a Comment