Link to home
Start Free TrialLog in
Avatar of bpl5000
bpl5000

asked on

Sending SMTP relay email with VB.net

I have the following code that will work if I specify credentials, but I want to sent this without using credentials.  Can someone tell me how to do this without specifying credentials?  I setup an SMTP relay server that does not require credentials.

Please, no links.  I think it's just one line of code that I don't have right or that needs to be added.

Dim strTo As String = "me@mydomain.com"
Dim strFrom As String = "account@mydomain.com"
Dim strSubject As String = "Subject"
Dim mailClient As New System.Net.Mail.SmtpClient("10.10.10.14", 25)
Dim strBody As String = "message"
Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strBody)
'mailClient.UseDefaultCredentials = False
mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
mailClient.Send(mailMessage)

Open in new window

Avatar of unknown_routine
unknown_routine
Flag of United States of America image

UseDefaultCredentials default value is false.


If this is set to false email is sent annoymously.

If you do not want to specify credentials you have to

UseDefaultCredentials=true
Avatar of bpl5000
bpl5000

ASKER

I tried...

mailClient.UseDefaultCredentials = True

But that did not work.  It does work when I use credentials using this line...

mailClient.Credentials = New System.Net.NetworkCredential("myaccount", "password")

Maybe I need to set mailClient.Credentials equal to something?
ASKER CERTIFIED SOLUTION
Avatar of unknown_routine
unknown_routine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, I forgot  to mention one important piece of code:


You also need to set the client credentials to the default:

mailClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
Avatar of bpl5000

ASKER

I added this line, but still can't get it to work.  Here's how the code looks now...

Dim strTo As String = "me@mydomain.com"
Dim strFrom As String = "account@mydomain.com"
Dim strSubject As String = "Subject"
Dim mailClient As New System.Net.Mail.SmtpClient("10.10.10.14", 25)
Dim strBody As String = "message"
Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strBody)
mailClient.UseDefaultCredentials = True
mailClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
mailClient.Send(mailMessage)

Open in new window

Avatar of Mohammed Khawaja
Take out account name and password and make section for that to "",""
Avatar of bpl5000

ASKER

I restarted the SMTP server service (it's an IIS SMTP server) and then it worked.  Maybe I need to restart the service after I add an accepted IP to relay from.  Either that or it just needed a restart.  Anyway, it turns out I don't need any of the statements concerning credentials.  This is all I need...

Dim strTo As String = "me@mydomain.com"
Dim strFrom As String = "account@mydomain.com"
Dim strSubject As String = "Subject"
Dim mailClient As New System.Net.Mail.SmtpClient("10.10.10.14", 25)
Dim strBody As String = "message"
Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strBody)
mailClient.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
mailClient.Send(mailMessage)

Open in new window