Testing the email setup especially the smtp sending on remoute server can be a real pain. Getting the parameters right and the code correct can require several iterations. As someone who administers several sites, what I came up with is a standard and simple page where we can test the C# code and parameters for seding an email.
Using the sample below you can easily test sending an email and setting parameters for smtp mail sending.
First the code for seding an email with smtp in C#
Sending an email with SMTP in C# (code behind for ASP.NET test page). Save as SendMails.Aspx.cs
using System;
using System.Net;
using System.Net.Mail;
public partial class Tests_SendMails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage m = new MailMessage(TextBoxLogin.Text,TextBoxDestination.Text);
m.Subject = "test mail";
m.Body = "test mail sending";
SmtpClient s = new SmtpClient(TextBoxHost.Text);
NetworkCredential nc = new NetworkCredential(TextBoxLogin.Text, TextBoxPass.Text);
s.UseDefaultCredentials = false;
s.Credentials = nc;
s.Port = Convert.ToInt32(TextBoxPort.Text.Trim());
s.Send(m);
}
}
The source for the aspx page is simple Save as SendMails.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMails.aspx.cs" Inherits="Tests_SendMails" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Testing Sending of an email...
<br />
Clicking submit sends an email with the test "test nessage"</div>
<br />
smtp host<br />
<asp:TextBox ID="TextBoxHost" runat="server"
Width="241px">smtpout.secureserver.net</asp:TextBox>
<br />
<br />
smtp login<br />
<asp:TextBox ID="TextBoxLogin" runat="server"
Width="239px">info@auxquatrevents.ch</asp:TextBox>
<br />
<br />
smtp password<br />
<asp:TextBox ID="TextBoxPass" runat="server"
Width="241px">shinjuku75</asp:TextBox>
<br />
<br />
<br />
destination<br />
<asp:TextBox ID="TextBoxDestination" runat="server"
Width="251px">shelby.pereira@gmail.com</asp:TextBox>
<br />
<br />
PORT<br />
<asp:TextBox ID="TextBoxPort" runat="server"
Width="251px">25</asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="SendMail"
Width="219px" />
<br />
<br />
<br />
The code used to send the mail is the following (where MailMessage has been
setup with a title and dummy message,<br />
<br />
<br />
public static void SendMail(string login, string password, int port, MailMessage
m,string host) {<br />
<br />
SmtpClient s = new SmtpClient(host);
<br />
NetworkCredential nc = new NetworkCredential(login, password); <br />
s.UseDefaultCredentials = false;<br />
s.Credentials = nc;<br />
s.Port = port;<br />
s.Send(m);<br />
<br />
}
<br />
</form>
</body>
</html>