3

Trying to send an email through an SMTP server, but I'm getting a nondescript error on the smtp.Send(mail); snippet of code.

Server side, the relay IP addresses look correct. Scratching my head about what I'm missing.

MailMessage mail = new MailMessage();
mail.To.Add(txtEmail.Text);

mail.From = new MailAddress("no-reply@company.us");
mail.Subject = "Thank you for your submission...";
mail.Body = "This is where the body text goes";
mail.IsBodyHtml = false;

SmtpClient smtp = new SmtpClient();
smtp.Host = "mailex.company.us";
smtp.Credentials = new System.Net.NetworkCredential
     ("AdminName", "************");

smtp.EnableSsl = false;


if (fileuploadResume.HasFile)
{
    mail.Attachments.Add(new Attachment(fileuploadResume.PostedFile.InputStream,
        fileuploadResume.FileName));
}

smtp.Send(mail);
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
Rex_C
  • 2,436
  • 7
  • 32
  • 54
  • What does txtEmail look like? Have you tried hardcoding an e-mail address in there to see if it sends? – PW Kad Apr 24 '13 at 17:07
  • I assume you've set the correct SMTP settings in a config file? – keyboardP Apr 24 '13 at 17:08
  • You must either specify in code "smtp.UseDefaultCredentials = false;" or in the web.config mail settings. Also, what port are you specifying? – ron tornambe Apr 24 '13 at 17:10
  • 1
    What *is* the "nondescript error?" Post the stack trace (be sure to edit out any confidential data). – Wonko the Sane Apr 24 '13 at 17:10
  • Download [Putty](http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html) and try it manually to make sure there is no problem with the server. Connect through Telnet to server `mailex.company.us` at port 25. Here's [an example of an SMTP conversation](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol#SMTP_transport_example). – Daniel A.A. Pelsmaeker Apr 24 '13 at 17:11

4 Answers4

2

Try adding smtp.DeliveryMethod = SmtpDeliveryMethod.Network; prior to send.

For reference, here is my standard mail function:

public void sendMail(MailMessage msg)
{
    string username = "username";  //email address or domain user for exchange authentication
    string password = "password";  //password
    SmtpClient mClient = new SmtpClient();
    mClient.Host = "mailex.company.us";
    mClient.Credentials = new NetworkCredential(username, password);
    mClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    mClient.Timeout = 100000;
    mClient.Send(msg);
}

Typically called something like this:

MailMessage msg = new MailMessage();
msg.From = new MailAddress("fromAddr");
msg.To.Add(anAddr);

if (File.Exists(fullExportPath))
{
    Attachment mailAttachment = new Attachment(fullExportPath); //attach
    msg.Attachments.Add(mailAttachment);
    msg.Subject = "Subj";
    msg.IsBodyHtml = true;
    msg.BodyEncoding = Encoding.ASCII;
    msg.Body = "Body";
    sendMail(msg);
}
else
{
    //handle missing attachments
}
Chrsjkigs99
  • 850
  • 1
  • 9
  • 18
0
            var client = new SmtpClient("smtp.gmail.com", 587);
            Credentials = new NetworkCredential("sendermailadress", "senderpassword"),
            EnableSsl = true
            client.Send("sender mail adress","receiver mail adress", "subject", "body");
            MessageBox.Show("mail sent");
Adiii
  • 54,482
  • 7
  • 145
  • 148
0

This is Adil's answer formatted for C#:

public static class Email
{
    private static string _senderEmailAddress = "sendermailadress";
    private static string _senderPassword = "senderpassword";

    public static void SendEmail(string receiverEmailAddress, string subject, string body)
    {
        try
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential(_senderEmailAddress, _senderPassword),
                EnableSsl = true
            };
            client.Send(_senderEmailAddress, receiverEmailAddress, subject, body);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception sending email." + Environment.NewLine + e);
        }
    }
}
sapbucket
  • 6,795
  • 15
  • 57
  • 94
0

You didn't specify the port.

smtp.Port = 1111; // whatever port your SMTP server uses

The SMTP has three different "standard" ports: 25, 465 and 587. According to msdn documentation, the default value for the Port property is 25.

Sasino
  • 134
  • 2
  • 11