Sunday, August 22, 2010

Using Gmail with Webmatrix ASP.Net Web Pages

OK, here is a basic example of how to use Gmail with Razor/WebMatrix ASP.Net web pages, in this case a .cshtml page.

  1. @{  
  2. try {  
  3.         // Initialize Mail helper  
  4.         Mail.SmtpServer = "smtp.gmail.com";  
  5.         Mail.SmtpPort = 587; //25 465; - 25 default, 465 in documentation, 587 works  
  6.         Mail.EnableSsl = true;  
  7.         Mail.UserName = "your user name"// include domain if not gmail user@mydomain.com  
  8.         Mail.From = "return email address";   
  9.         Mail.Password = "your password";  
  10.            
  11.         // Send email  
  12.         Mail.Send(to: "target email",   
  13.             subject: "test",  
  14.             body:"yo!"  
  15.         );  
  16.     }  
  17.     catch (Exception ex ) {  
  18.         <text>  
  19.           <b>The email was <em>not</em> sent.</b>  
  20.           The code on the PrcessRequest page must provide the   
  21.           STMP server, user name, password, and email addresses   
  22.           required.  
  23.         </text>  
  24.     }  
  25. }  

2 comments:

Anonymous said...

unable to work :/

infocyde said...

I used it before and it was working.