Monica
Monica

Reputation: 45

How do I use VB.NET to send an email from an Outlook account?

I'm trying to send an email via an outlook email account through a vb.net program. When I run the code I get an error telling me that I don't have a secure connection. I've been searching online and have tried all the adjustments I've found but I'm still not having any luck.

Here is my code at the moment(specific email addresses and passwords are changed):

Private Sub LEmail_Click(sender As Object, e As EventArgs) Handles LEmail.Click
    Try
        Dim SmtpServer As New SmtpClient("smtp.outlook.com")
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "Password")
        SmtpServer.Port = 587
        SmtpServer.EnableSsl = True
        mail = New MailMessage()
        mail.From = New MailAddress("[email protected]")
        mail.To.Add("[email protected]")
        mail.Subject = "Test Mail"
        mail.Body = "This is for testing SMTP mail from OUTLOOK"
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.Send(mail)
        MessageBox.Show("mail sent")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Any suggestions and help would be greatly appreciated. Thanks :)

EDIT: I've also tried SmtpServer.Port = 25

EDIT 2: Error message reads:

System.Net.Mail.SmtpExeption: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM as System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Sent(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at WindowsApplication.Form1.LEmail_Click(Object sender, EventArgs e) in C:\Users\Monica\Documents\Current\Visual Studio\WindowsApplication\Form1.vb:line 553

EDIT 3: Second Error:

System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: 5.3.4 Requested action not taken; We noticed some unusual activity in your Hotmail account. To help protect you, we've temporarily blocked your account. at System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode, String serverResponse) at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args) at System.Net.CloseableStream.Close() at System.Net.MailWriter.Close() at System.Net.Mail.SmtpClient.Send(MailMessage message) at WindowsApplication.Form1.LEmail_Click(Object sender, EventArgs e) in C:\User\Monica\Documents\Current\Visual Studio\WindowsApplication\Form1.vb:line 553

Upvotes: 0

Views: 4953

Answers (2)

SteveFerg
SteveFerg

Reputation: 3580

change the smtpserver from smtp.outlook.com to smtp-mail.outlook.com

web.config settings

<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network host="smtp-mail.outlook.com" userName="[email protected]" password="passwordhere" port="587" enableSsl="true"/>
  </smtp>
</mailSettings> 

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300837

Add this:

SmtpServer.UseDefaultCredentials = false 

before this line:

SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "Password")

[Whoever designed this API obviously wasn't thinking like a consumer!]

Upvotes: 0

Related Questions