Monday, April 7, 2008

C#- SMTP mail - System.Net.Mail

 

   1: MailMessage message = new MailMessage();
   2: message.From = new MailAddress("sender@foo.bar.com");
   3: message.To.Add(new MailAddress("recipient1@foo.bar.com"));
   4: message.To.Add(new MailAddress("recipient2@foo.bar.com"));
   5: message.To.Add(new MailAddress("recipient3@foo.bar.com"));
   6: message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
   7: message.Subject = "This is my subject";
   8: message.Body = "This is the content";
   9: SmtpClient client = new SmtpClient();
  10: client.Send(message);
  11:  
  12:  

Example of web.config configuration:

   1: <system.net>
   2:     <mailSettings>
   3:       <smtp from="test@foo.com">
   4:         <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
   5:       </smtp>
   6:     </mailSettings>
   7: </system.net>

My example using "localhost":

   1: MailMessage message = new MailMessage();
   2: message.From = new MailAddress("hello@world.com");
   3: message.To.Add(new MailAddress("hello@world.com"));
   4: message.Subject = "Mail from Idea Catcher";
   5: message.Body = "New idea was posted";
   6: SmtpClient client = new SmtpClient("Localhost");
   7: client.UseDefaultCredentials = false;
   8: client.Send(message);

here is the link for iis configuration in case of error - 5.7.1 Unable to relay for xxx - here

source1

source2

No comments:

Post a Comment