Sending mail via SMTP server

I

iambenb

Hi all

This is very frustrating. As far as I can tell my code should work.
Indeed, I have got something similar working using VBA (and the
CDO.Message object).

I get the error "The operation has timed out" so I think that it's not
connecting to the SMTP server at all. Anyone got ideas? Here's the
code:

<code>
MailMessage m = new MailMessage();
SmtpClient c = new SmtpClient();
c.DeliveryMethod = SmtpDeliveryMethod.Network;

// I've tried this with and without the domain
System.Net.NetworkCredential cred = new
System.Net.NetworkCredential("SmtpUserName", "SmtpPassword");

c.EnableSsl = true;
c.UseDefaultCredentials = false;
c.Host = "SmtpServer";
c.Port = 465;
c.Credentials = cred;

m.To.Add(new MailAddress("(e-mail address removed)");
m.From = new MailAddress("(e-mail address removed)");
m.Subject = "DotNet email";
m.Body = "Hi Ben" + Environment.NewLine + "This email
was sent via code through SMTP";

c.Send(m);
</code>

I can't see anything wrong with that. If anyone wants me to post the
VBA code that does work, I will.
I'm thinking that I may have to use CDO, which I don't really want to
do.

Thanks,
Ben
 
A

AA2e72E

I had problems with SMTP: mine were down to the virus checker blocking port
25; this cause stray error to be reported.
 
I

iambenb

Thanks. I've definitely not got that problem, and in fact I can get
this working using System.Web.Mail. Microsoft tells me that I should
be using System.Net.Mail instead, but it doesn't work!!
 
A

Andreas Kleiböhmer

iambenb said:
I can't see anything wrong with that.

Works for me with the same construction (other variable values, of
course, for server, username etc..)

But I did not see you supplied a "sender". If I leave that out, our
mailserver fortunately complains.

Does it work for you, if you also supply a sender to the message?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top