Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Email With Office 365 Using System.Web.Mail

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I have an old VB.net 2003 program that has been sending emails through an external server suing system.web.mail. I need to maintain the program in Net 1.1 for a while longer but want to move the email to Office 365 and am having trouble authenticating. Does anyone have any sample code using Office 365 I could review to see if I am doing anything wrong?

Auguy
Sylvania/Toledo Ohio
 
Thanks, been there and that works for most smtp, but Office 365 is a little different. I can get it to work in the newer Imports System.Net.Mail in the code below, but can't seem to dumb it down to System.Web.Mail. The main problem seems to be that 365 wants the "name" of the sender in addition to the email address and password as seen in the "message.From = New MailAddress("me@myemail.com", "my name")" line below. Not sure how to accomplish that in the older version.
Code:
  Public Sub SendMail()
    Try
      Dim mailClient As New SmtpClient("smtp.office365.com")

      mailClient.Port = 587
      mailClient.EnableSsl = True

      Dim cred As New System.Net.NetworkCredential("me@myemail.com", "password")
      mailClient.Credentials = cred

      Dim message As New MailMessage()

      'This DOES NOT work  
      'message.From = New MailAddress("me@myemail.com")

      'This DOES work  
      message.From = New MailAddress("me@myemail.com", "my name")

      message.[To].Add("somebody@inc.com")
      message.Subject = "Test Email From 365"
      message.Body = "This is the body"

      mailClient.Send(message)

    Catch ex As Exception
      Throw ex
    End Try
  End Sub


Auguy
Sylvania/Toledo Ohio
 
Per the article I posted, you use the Fields parameter of the MailMessage object to set the required value:

Dim msg As New MailMessage()

msg.Fields.Add(" userName)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
That is the username or ID of the account used to authenticate the sender which is the same as.
Code:
Dim cred As New System.Net.NetworkCredential("me@myemail.com", "password")
But what I am trying to duplicate in the the older version is the "my name" in the following which is different from the username, it is the actual name of the sender (like John Smith) not the ID.
Code:
message.From = New MailAddress("me@myemail.com", "my name")
For some reason 365 wants this display name which is not available in system.web.mail. See Maybe I'm missing something obvious.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top