ASP.NET SMTP and Gmail
The question: Is it possible to configure multiple SMTP servers or update the SMTP object server information at runtime?
Description of problem: The site I'm working on has a few pages which send emails. Our site is using Gmail so even though it's a custom domain our email is sent out though Gmail SMTP servers.
Some emails come from the system and some emails come from users in certain groups. I have an SMTP server configured with the admin email for the system e.g. admin@mysite.com This works fine and when I get the email it shows from admin@mysite.com
One of our user groups let's call it "groupA" has an email configured on our domain i.e. groupA@mysite.com When I send the email and specify a from address for the MailMessage object as groupA@mysite.com it still shows up in my outlook as coming from admin@mysite.com I did a little further testing and if I specify both the from address and the ReplyTo as groupA@mysite.com it will show as From=admin@mysite.com but when I choose reply in outlook it shows groupA@mysite.com My best guess is that Gmail is overwriting the From address with the SMTP user to prevent email spoofing.
I found this: http://开发者_开发知识库www.systemnetmail.com/faq/4.1.aspx but it won't work for me since group A, B, and C all need to send emails from the same page.
To my knowledge, it's impossible to fake the "from" address when using gmail. Even if you use apps premiere, and set up address aliases, it still says "sent on behalf of ".
system.net.mail configuration in web.config only supports one config you can easily create your own custom config and configure email server settings at runtime, like so:
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 465;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.UserCredentials = new System.Net.NetworkCredential("yourusername", "yourpassword");
smtp.Send(mail);
you can obviously store yourusername and yourpassword in app settings
精彩评论