Usually my application sends email with a generic system address. But in some cases I want to instead send as the logged in user.


<?xml version="1.0"?>
        <add key="DefaultEmailAddress"
             value="[email protected]" />
                <network host="servername"
                         password="apple" />

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


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.

+1  A: 

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.

@Ricardo - The from is set on the third line of his sample. Dim m As New Mail.MailMessage(from, to)
Exactly, it is being set to the variable "from" which has the value from the web.config file ([email protected]). The value of "from" needs to be set to the current user's email address as wanted.
@Ricardo - Setting the from to the CurrentUser.Anything doesn't matter by itself because the object m (MailMessage) was already set to the from in it's constructor on line 3. To change it after the fact you will have to modify a property of the object as Zach is attempting to do in m.Sender = New Mail.MailAddress(address). AKA -Zach already has a new email address in the address variable and he is attempting to set it to m.Sender and it doesn't seem to be taking.... The variable address is essentially his "overridden" / new email address value. :)
Yes, and my suggestion is that he changes the from variable in the constructor. Once the run that decides what email to use is executed, then the constructor should be created with the correct value, in this case using CurrentUser.blabla instead of ConfigurationManager.AppSettings("DefaultEmailAddress"). It is clear and simple.
@Ricardo - I understand now. You didn't state that in your original answer. Without this clarification it seems you are just stating to change the from variable without any consideration of when to call the constructor. ;-)
+2  A: 

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.

Thank you! I set m.From. It now appears to be from the user. But it still doesn't show up in the user's Sent folder in Outlook. Why?
Zack Peterson
I'm not entirely sure but I think the sent items is an operation of the Outlook Client itself and/or the MAPI profile perhaps.... I don't think the mail in .net would put anything in the sent items. Here's a link on this: