sse698.SMTP

SMTP Protocol
Simple Mail Transfer Protocol (SMTP) is used to send e-mails. I had days of trouble trying to get my website to e-mail user (and at the same time e-mail me) when a submission occurred. Turns out I was facing two separate problems. First, my GoDaddy hosting service did not come with a SMTP relay services (it is additional monthly charge). After talking with a friend about this problem, he let me use one of his hosting services' SMTP relay service to send e-mails from my site (I hope GoDaddy doesn't find out about this). The second problem I ran into was that COX, my ISP, blocks port 25, the defaulted port for SMTP protocol. I had to open a new port in order to get my e-mail to work while developing my site. Now that it is live, it should always work because all users will be going through port 80 (Internet) and my GoDaddy server will handle the port 25 access. These problems took several days to iron out but, in the end, I finally got it working.

The code to handle e-mailing is listed below: code format="csharp" private void SendEmailToUser(string username, string subject, string body) {           string emailAddress = GetEMailAddress(username);

MailMessage message = new MailMessage; message.From = new MailAddress("webmaster@timbarnesengineering.com"); message.Bcc.Add(new MailAddress("webmaster@timbarnesengineering.com")); message.To.Add(new MailAddress(emailAddress)); message.Subject = "TimBarnesEngineering Automated Response"; message.Body = "TimBarnesEngineering has recieved your Computer Inquiry regarding:\n\n " + subject + " at " + DateTime.Now + "\n" + "Your business is important to us and we will respond back to you as soon as possible.\n\n" + "Your message: \n" + body + "\n\n Thanks your business"; SmtpClient smtp = new SmtpClient("smtp.1and1.com", 587); smtp.Credentials = new System.Net.NetworkCredential("########", "#######"); smtp.Send(message);

}

private string GetEMailAddress(string username) {           string email = ""; SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT aspnet_Membership.Email FROM aspnet_Membership INNER JOIN aspnet_Users " + "ON aspnet_Membership.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = '" + username + "')"; SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader = null; connection.Open; reader = command.ExecuteReader; while (reader.Read) {               email = reader[0].ToString; }           connection.Close; return email; } code

First, before creating the e-mail message I needed to figure who was logged into the site and cross-reference their login to retrieve their registered e-mail address. This is handle in the GetEMailAddress method. Using the username I was able to use two of the ASP .NET tables to get the e-mail address.

After obtaining the e-mail address, I created the automated e-mail inside the SendEmailToUser method. The MailMessage was straight forward, I send the message from webmaster@timbarnesengineering.com, CC to the same address, and insert the user's address in the To line of the e-mail. I added more verbiage to the body of the message to show the user what was received. The SmtpClient was where the majority of the work was done. I am using another hosting service's smtp (smtp.1and1.com). I am using there alternative smtp port, 587. I them give it a username and password to use (I removed them from this page) for security. Finally I use the SmtpClient to send the message.