views:

46

answers:

2

I am using Visual Studio 2008 Express with C#. I have been trying to get an email routine to work in the code-behind on an aspx page.

All the MSDN examples, even those stated to be for .Net 3.5, do not compile. The MailMessage class has apparently changed a few times. Here is code that does compile, but this line of code, SmtpMail.Send(msg), has an error message that is vague:

"The best overloaded method match for 'System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage)' has some invalid arguements.

Can anyone see what the invalid arguements could be? This is all that is preventing this from working.

using System.Net;
using System.Net.Mail;

MailMessage msg = new MailMessage();

msg.ToAddress = new MailAddress("[email protected]");
msg.FromAddress = ("[email protected]");
msg.CCAddress = ("[email protected]");

msg.EmailMessage = "Order message test";
msg.EmailSubject = "Order Confirmation";
msg.MailEncoding = "html";
msg.MailPriority = MailPriority.Normal.ToString();

SmtpClient SmtpMail = new SmtpClient();

SmtpMail.Host = "smtpout.secureserver.net";
SmtpMail.Port = 25;

try
{
    SmtpMail.Send(msg);    // This is where the error occurs.
}
catch (Exception ex)
{
    //  Incomplete here
}
A: 

It looks like you're using a custom wrapper for MailMessage. None of those properties are members of the .NET classes (for any version I can find).

For 3.5, you'd use:

msg.To.Add(new MailAddress("[email protected]"));
msg.From.Add(new MailAddress("[email protected]"));
msg.CC.Add(new MailAddress("[email protected]"));

msg.Body = "Order message test";
msg.Subject = "Order Confirmation";
msg.BodyEncoding = Encoding.Default;
msg.Priority = MailPriority.Normal;
Jeff Sternal
A: 

It looks like you're trying to do something quite complicated; I would try with an even simpler example first and work your way up. The website SystemNetMail.com has a lot of resources you might find useful. This is their simple example:

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
Colin Pickard