Note that there are some explanatory texts on larger screens.

plurals
  1. POSending Email with C# - Doesn't work, but no error thrown
    primarykey
    data
    text
    <p>as the topic title suggests, I am trying to send email from my C# application and i'm running into a little bit of trouble.</p> <p>I wrote the function below in order to make it easier to send mail from my app, but i believe there must be a problem somewhere and I just can't see it. Perhaps it's the "Can't see the forest for the trees" scenario.</p> <p>The problem occurs when I try to send email via SMTP. The page just seems to time out, with no error message, at all.. LocalPickup works, as does specifying a pickup directory, but in this instance I need to use SMTP.</p> <p>In this case, my website is located on my home development server (running windows server 2003) and my SMTP server is a remote dedicated box running CentOS Linux with Qmail.</p> <p>I've included the function I wrote, and just to answer any questions.. Yes, the SMTP port on this server is definately 26 ;)</p> <pre><code> /// &lt;summary&gt; /// Sends an email /// &lt;/summary&gt; /// &lt;param name="To"&gt;Addresses to send the email to, comma seperated&lt;/param&gt; /// &lt;param name="subject"&gt;Subject of the email&lt;/param&gt; /// &lt;param name="emailBody"&gt;Content of the email&lt;/param&gt; /// &lt;param name="cc"&gt;CC addresses, comma seperated [Optional]&lt;/param&gt; /// &lt;param name="Bcc"&gt;BCC addresses, comma seperated [Optional]&lt;/param&gt; /// &lt;param name="client"&gt;How to send mail, choices: iis, network, directory. [Optional] Defaults to iis&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public bool sendMail(string To, string subject, string emailBody, string from, string cc = "", string Bcc = "", string client = "network", bool html = true) { // Create a mailMessage object MailMessage objEmail = new MailMessage(); objEmail.From = new MailAddress(from); // Split email addresses by comma string[] emailTo = To.Split(','); foreach (string address in emailTo) { // Add these to the "To" address objEmail.To.Add(address); } // Check for CC addresses if (cc != "") { string[] emailCC = cc.Split(','); foreach (string addressCC in emailCC) { objEmail.CC.Add(addressCC); } } // Check for Bcc addresses if (Bcc != "") { string[] emailBCC = Bcc.Split(','); foreach (string addressBCC in emailBCC) { objEmail.Bcc.Add(addressBCC); } } // Set the subject. objEmail.Subject = subject; // Set the email body objEmail.Body = emailBody; // Set up the SMTP client SmtpClient server = new SmtpClient(); switch (client) { case "iis": server.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; break; case "network": server.DeliveryMethod = SmtpDeliveryMethod.Network; NetworkCredential credentials = new NetworkCredential("SmtpUserName", "SmtpPassword"); server.Host = "SmtpHost"; server.Port = 26; server.Credentials = credentials; break; case "directory": server.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; server.PickupDirectoryLocation = "c:\\mailpickup"; break; default: throw new Exception("Invalid delivery method specified, cannot continue!"); } if (html) { // As the email is HTML, we need to strip out all tags for the plaintext version of the email. string s = emailBody; s = Regex.Replace(s, "&lt;.*?&gt;", string.Empty); s = Regex.Replace(s, "&lt;script.*?&lt;/script&gt;", "", RegexOptions.Singleline | RegexOptions.IgnoreCase); AlternateView plainText = AlternateView.CreateAlternateViewFromString(s, null, MediaTypeNames.Text.Plain); objEmail.AlternateViews.Add(plainText); AlternateView rich = AlternateView.CreateAlternateViewFromString(emailBody, null, MediaTypeNames.Text.Html); objEmail.AlternateViews.Add(rich); } try { server.Send(objEmail); return true; } catch(Exception ex) { throw new Exception(ex.ToString()); } </code></pre> <p>As I said, the page just hangs completely after about 60 seconds, with no error message to be seen.</p> <p>Thanks in advance,</p> <p>Dave</p> <p><strong>Addition:</strong> - This is how I am calling sendMail()</p> <pre><code>webMail sendConfirmation = new webMail(); fileSystem fs = new fileSystem(); siteSettings setting = new siteSettings(); string mailBody = fs.file_get_contents("http://myurl.com/mymessage.html"); // Run any replaces. mailBody = mailBody.Replace("{EMAIL_TITLE}", "Your account requires confirmation"); mailBody = mailBody.Replace("{U_FNAME}", u_forename); mailBody = mailBody.Replace("{REG_URL_STRING}", setting.confirmUrl); sendConfirmation.sendMail(u_emailAddress, "Your account requires confirmation", mailBody, setting.siteEmail); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload