Reputation: 57373
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.
Upvotes: 1
Views: 1647
Reputation: 21088
I think this answers your question:
Sender and From are separate. Changing the sender will not change the From address.
In your code set the From Property to your address variable.
Upvotes: 2
Reputation: 6289
In your code above, I don't see where you are overriding the default Sender. You are setting the "From" value to [email protected]:
Dim from As String = ConfigurationManager.AppSettings("DefaultEmailAddress")
You should try something like:
Dim from As String = CurrentUser.ExchangeUserName
This will use that user's email address instead of the value located in your web.config file.
Upvotes: 1