SmtpClient UserName overrides MailAddress.From

39 views
Skip to first unread message

DanH

unread,
Sep 30, 2011, 2:46:22 PM9/30/11
to google-app...@googlegroups.com

Hi,

This is a follow-on to a previous thread, where I was having trouble sending mail via smtp.gmail.com.  Here is the code which works properly:
 
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);  
client
.EnableSsl = true;  
client
.Credentials = new NetworkCredential("myuse...@mydomain.com", "password");   
client
.Timeout = 5000;  
MailMessage message = new MailMessage();  
message
.IsBodyHtml = false; 
message
.From = new MailAddress("FromA...@gmail.com");  
message
.To.Add("som...@gmail.com");  
message
.Subject = "The Subject";  
message
.Body = "this is the body";  
try  
{  
  client
.Send(message);  
}  
catch (Exception ex) { //  do something }
 
 

Except - when the mail arrives, the From is the username passed in as a credential, myuse...@mydomain.com.  Instead, I want the From to be the e-mail address explicitly set as FromA...@gmail.com.  How do I get the message.From to be correct?  When stepping through the code in the debugger, Intellisense reports that message.From is what I want, not what actually shows up.

Thanks.

Dan

Jay Lee

unread,
Sep 30, 2011, 8:12:21 PM9/30/11
to google-app...@googlegroups.com
Hi DanH,

  Google rewrites the From: header to the user you authorized as because otherwise, it'd be possible to spoof any other user's email address. I'd be able to send an email from you to your boss telling him that you quit, that would be a bad thing :-)

  In order to send an email from FromA...@gmail.com you'll need to get a 3-legged OAuth authorized token for FromA...@gmail.com and use it to authenticate to smtp.gmail.com. See:


for details on how SMTP OAuth works.

Jay

Andy "Rufus" Rothfusz

unread,
Oct 3, 2011, 1:51:12 PM10/3/11
to google-app...@googlegroups.com
Hi Dan,

Are you able to send from that address ("FromA...@gmail.com") through the UI?  You should try to verify that you own that address first before trying to send as that user: https://mail.google.com/support/bin/answer.py?answer=22370

If you can't send as that user through the UI, then you won't be able to do it through SMTP.

/Rufus
--
Andy "Rufus" Rothfusz | Developer Programs Engineer | Google | Mountain View, CA

DanH

unread,
Oct 15, 2011, 5:24:57 PM10/15/11
to google-app...@googlegroups.com
Thanks. This was very helpful. I followed the link and ended up downloading the library and sample app from DotNetOpenAuth, and got that working on my machine. But I don't see how to get from their sample to my requirements.
 
The sample app uses a googleConsumerKey and googleConsumerSecret to retrieve the first 5 contacts from my address book.  However, in my app, I need to change the From address to one of the addresses in the address book.  Simply retrieving the name and e-mail address is not sufficient.
 
You mentioned that I need to get a 3-legged OAuth token for the From address and use it to authenticate to smtp.gmail.com. Here is the code in the sample app for getting the token:
 
protected void Page_Load(object sender, EventArgs e) {
  if (this.TokenManager != null) {
    this.MultiView1.ActiveViewIndex = 1;
    if (!IsPostBack) {
      var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager);
      // Is Google calling back with authorization?
      var accessTokenResponse = google.ProcessUserAuthorization();
      if (accessTokenResponse != null) {
        this.AccessToken = accessTokenResponse.AccessToken;
      } else if (this.AccessToken == null) {
        // If we don't yet have access, immediately request it.
        GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts);
      }
    }
  }
}
 
Is this a 3-legged token?  If so, how do I integrate this with the SMTP client in my previous code listing?
 
Thanks.
 
--dan
 

DanH

unread,
Oct 17, 2011, 10:42:23 AM10/17/11
to google-app...@googlegroups.com
Actually, I missed this link before I sent the previous post. This turned out to solve my problem exactly without writing a single line of code.  All I did was follow the steps in the linked article to add the necessary e-mail addresses to the main account name.
 
Thanks.
 
Dan
Reply all
Reply to author
Forward
0 new messages