2

It seems many solutions involve using a server: smtp.gmail.com and System.Net.Mail.SmtpClient.

Other's suggest using System.Web.Mail (which many say has been deprecated): here for example.

In any case, when I tried it, I wasn't able to get either of the above examples to work. I don't think it's a firewall problem. I am able to use outlook express to send emails through gmail. The error in C# that I am given is that the target machine is actively refusing the connection.

Should I attempt to use outlook from the code? Would the best way to do that be through AutoIt or through some dll/com? Why do I need a server in the first place? In C# I can download things from the web, why can't I just send specifically formatted webpackages (emails) directly from C#, without having to use some sort of server?

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
user420667
  • 6,552
  • 15
  • 51
  • 83

1 Answers1

0

you need smtp relay or gateway. I use http://sendgrid.com/ to send emails from the cloud, they have a free option.

there is also sample code on how to send emails, I re-factored their code so I could call it from other places.

public partial class Email
{
/// <summary>
/// Generic Email method used to send email through SendGrid
/// </summary>
/// <param name="ToAddressName">ToAddress, DisplayName</param>
/// <param name="FromAddress">Email address of sender</param>
/// <param name="FromName">Display Name of sender</param>
/// <param name="SendCopy">Send BCC to FromAddress</param>
public void SendEmail(Dictionary<string, string> toAddressName,
                      String fromAddress, String fromName,
                      String subject, String message,
                      bool sendCopy)
{
  try
  {
    MailMessage mailMsg = new MailMessage();

    // To
    foreach (KeyValuePair<string, string> kvp in toAddressName)
    {
      string toAddress = kvp.Key;
      string displayName = kvp.Value;
      mailMsg.To.Add(new MailAddress(toAddress, displayName));
    }

    // From
    mailMsg.From = new MailAddress(fromAddress, fromName);

    if (sendCopy)
    {
      mailMsg.Bcc.Add(new MailAddress(fromAddress, fromName));
    }

    // Subject and multipart/alternative Body
    mailMsg.Subject = subject;

    string html = message;
    mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

    // Init SmtpClient and send
    SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));

    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("sendgridusername", "sendgridpassword");
    smtpClient.Credentials = credentials;

    smtpClient.Send(mailMsg);
  }
  catch (Exception ex)
  {
    Error error = new Error();
    error.ReportError(ex);
  }
}

(ps if you have any comments on my code I would love to hear it, thanks)

Robbiekaze
  • 121
  • 3
  • If you want comments on your code I would recommend codereview at stackexchange. Your code does look like it would work. Sorry, but this would lead to essentially the same problems as I had before. I think it's a computer issue and not a code issue, although I don't EXACTLY know why. So I'll probably end up trying wireshark or perhaps even a fresh install of the OS. – user420667 Jul 20 '11 at 05:29