System.Net.Mail.SmtpException how do I send mail in .Net 2.0

H

Hugh Van

Hi all,

I'm trying to use .Net.Mail to send email in my program but somehow it
always throws me error. I have checked everything in the configuration:
username, password, smtpserver, smtp port... are properly input. Could
someone please kindly point me the way out. Thanks in advance.

Below is my EmailSender class:


/***************************************/

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

class EmailSender
{
string toEmail, toName, fromEmail, fromName, emailSubject,
emailBody, smtpServer, username, password;
int smtpPort;

public EmailSender()
{
toEmail = "(e-mail address removed)";
toName = "somebody";
fromEmail = "(e-mail address removed)";
fromName = "Test account";
emailSubject = "Email function testing";
emailBody = "Testing...";
smtpServer = "mail.mydomain.com";
username = "(e-mail address removed)";
password = "testaccountPW";
smtpPort = 25;
}

public EmailSender(string toEmail, string toName, string
fromEmail, string fromName, string emailSubject, string emailBody,
string smtpServer, string username, string password, int smtpPort)
{
this.toEmail = toEmail;
this.toName = toName;
this.fromEmail = fromEmail;
this.fromName = fromName;
this.emailSubject = emailSubject;
this.emailBody = emailBody;
this.smtpServer = smtpServer;
this.username = username;
this.password = password;
this.smtpPort = smtpPort;
}

public void sendMail(){
try
{
MailMessage mail = new MailMessage(fromEmail, toEmail,
emailSubject, emailBody);
mail.IsBodyHtml = false;
SmtpClient client = new SmtpClient(smtpServer,
smtpPort);
//client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(username,
password);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.EnableSsl = true;
client.Timeout = 100000;
client.Send(mail);

Console.WriteLine("Mail Sent");
mail.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} //end SendMail()

} //end Class

/*******************************************************/


Then I call the class:



/*************************************************/
EmailSender testmail = new EmailSender();
testmail.sendMail();
/*********************************************/

After 1 or 2 minutes, I receive the following error message:

/*************
Failure sending mail.
A first chance exception of type 'System.Net.Mail.SmtpException'
occurred in System.dll
*************/
 

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