Usually my application sends email with a generic system address. But in some cases I want to instead send as the logged in user.
web.config:
<?xml version="1.0"?>
<configuration>
...
<appSettings>
...
<add key="DefaultEmailAddress"
value="[email protected]" />
</appSettings>
<system.net>
<mailSettings>
<smtp>
<network host="servername"
port="25"
userName="noreply"
password="apple" />
</smtp>
</mailSettings>
</system.net>
...
</configuration>
This code fails to override that default sender from web.config:
Dim from As String = ConfigurationManager.AppSettings("DefaultEmailAddress")
Dim to As String = "[email protected]"
Dim m As New Mail.MailMessage(from, to)
m.IsBodyHtml = True
m.Subject = "Test"
m.Body = "<p>This is a test.</p>"
Dim c As New System.Net.Mail.SmtpClient
If CurrentUser.HasExchangeCredentials Then
Dim userName As String = CurrentUser.ExchangeUserName
Dim password As String = CurrentUser.ExchangePassword
Dim address As String = CurrentUser.EmailAddress
c.UseDefaultCredentials = False
c.Credentials = New System.Net.NetworkCredential(userName, password)
m.Sender = New Mail.MailAddress(address)
End If
c.Send(m)
My email goes out, but it is sent as [email protected] and also doesn't show up in my Outlook Sent folder.
I don't want to UseDefaultCredentials. I want to use a new, different NetworkCredential.
I'm using Microsoft Exchange.