Send email with out using network credentials
I want to send email using the SMTP client in C#. Currently I am using this code:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("receiver");
message.Subject = "";
message.From = new System.Net.Mail.MailAddress("sender");
message.Body = "This is a test mail";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(message);
But I want to send mail with out using username and password by just providing the sender address. Is this possible in C#? Is this possible with any SMTP s开发者_运维问答erver(Not google). I mean login to server anonymously and just provide your mail address.
No. Gmail does not allow anonymous senders. Thankfully. If it was possible anyone could pretend to send emails from everyone else, rendering gmail as one big spam engine.
It depends on your server. In your code you are using Gmail and Gmail requires this.
You can set up your own SMTP server.
The SMTP server in IIS allows this per default if your application is on the same server as IIS.
In the end you'll have to send credentials if the server demands them.
You could send the credentials of the current security context if that's what you're looking for. See: http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials.aspx.
精彩评论