Probably, your email system requires credentials in order to send.
Here is my generic Email routine:
public static void Email(string to,
string body,
string subject,
string fromAddress,
string fromDisplay,
string credentialUser,
string credentialPassword,
params MailAttachment[] attachments)
{
if (to == null)
{
to = ConfigurationManager.AppSettings["SMTPDefaultToAddress"];
}
string host = ConfigurationManager.AppSettings["SMTPHost"];
body = UpgradeEmailFormat(body);
try
{
MailMessage mail = new MailMessage();
mail.Body = body;
mail.IsBodyHtml = true;
mail.To.Add(new MailAddress(to));
mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
mail.Subject = subject;
mail.SubjectEncoding = Encoding.UTF8;
mail.Priority = MailPriority.Normal;
foreach (MailAttachment ma in attachments)
{
mail.Attachments.Add(ma.File);
}
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
smtp.Host = host;
smtp.Send(mail);
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder(1024);
sb.Append("\nTo:" + to);
sb.Append("\nbody:" + body);
sb.Append("\nsubject:" + subject);
sb.Append("\nfromAddress:" + fromAddress);
sb.Append("\nfromDisplay:" + fromDisplay);
sb.Append("\ncredentialUser:" + credentialUser);
sb.Append("\ncredentialPasswordto:" + credentialPassword);
sb.Append("\nHosting:" + host);
ErrorLog(sb.ToString(), ex.ToString(), ErrorLogCause.EmailSystem);
}
}
Ignore the ErrorLog and UpdateEmailFormat methods: you can remove them or write your own.
My web.config file coantains the following in the <appSettings> section:
<add key="SMTPHost" value="LocalHost"/>
<add key="SMTPCredentialUser" value="DoNotReply@XXX.com"/>
<add key="SMTPCredentialPassword" value="XXXX"/>
<add key="SMTPDefaultFromAddress" value="Webmaster@XXX.com"/>
<add key="SMTPDefaultToAddress" value="Webmaster@XXX.com"/>
Try filling in your details, and trying that - I know it works!