Reputation: 67
I am novice in asp.net and am trying to send email via this short code.
Public Class sMail
Public emailBody As String
Public emailSubject As String
'Public sendTo As String
Public Function sendMail() As Boolean
Dim eMail As New System.Net.Mail.MailMessage
Dim smtpClient As New System.Net.Mail.SmtpClient("mydomain", 25)
Dim credentials As New System.Net.NetworkCredential("[email protected]", "password")
smtpClient.Credentials = credentials
smtpClient.UseDefaultCredentials = True
smtpClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
eMail.From = New System.Net.Mail.MailAddress("[email protected]", "Info")
eMail.To.Add(New Net.Mail.MailAddress("[email protected]"))
eMail.Subject = emailSubject
eMail.Body = emailBody
eMail.BodyEncoding = System.Text.Encoding.UTF8
eMail.IsBodyHtml = False
Try
smtpClient.Send(eMail)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
On sending mails outside my domain(gMail,Hotmail etc) I get : Mailbox unavailable. The server response was: Authentication is required for relay
However I have no trouble in sending emails on my own domain.
I have tried several changes but same error. Following does not work
However when i used same SMTP settings in Outlook I got same error Server error: '550 Authentication is required for relay' When sending mails outside my own domain. such as on hotmail, gmail.
But when i changed accounts setting in outlook and ticked : My outgoing (SMTP) server requires authentication -> Use same settings as incoming mail server, I was able to send email to any domain.
I cant figure this out, can any one guide me?
Upvotes: 1
Views: 2055
Reputation: 67
I figured it out somehow, but still cant explain why this is
Instead of declaring credentials in codebehind i declared mailsettings in webconfig
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="[email protected]" port="25" userName="[email protected]" password="secret" defaultCredentials="false" enableSsl="false" clientDomain="mydomain.com"/>
</smtp>
</mailSettings>
</system.net>
Then just used smtp client in code behind without setting credentials :
Public Class sMail
Public emailBody As String
Public emailSubject As String
Public sendTo As String
Public Function sendMail() As Boolean
Dim eMail As New System.Net.Mail.MailMessage
eMail.Sender = New System.Net.Mail.MailAddress("[email protected]")
eMail.Priority = Net.Mail.MailPriority.High
eMail.From = New System.Net.Mail.MailAddress("[email protected]")
eMail.To.Add(sendTo)
eMail.Subject = emailSubject
eMail.Body = emailBody
eMail.IsBodyHtml = True
eMail.BodyEncoding = Encoding.Default
Dim smtpClient As New System.Net.Mail.SmtpClient()
Try
smtpClient.Send(eMail)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
This method worked for me,It should also work with credentials declared in code behind instead of in web.config I guess, I am not sure, but didn't work for me
Upvotes: 0