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 = "so******@mydomain.com";
toName = "somebody";
fromEmail = "te*********@mydomain.com";
fromName = "Test account";
emailSubject = "Email function testing";
emailBody = "Testing...";
smtpServer = "mail.mydomain.com";
username = "te*********@mydomain.com";
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
*************/