Introduction
Recently I involved in .NET project, where last developer that worked in project has used a non native class to send mail. Well, I have problems with used solution. The used component doesn't work well on windows 2k3. This problem was solved using SMTP object form .NET 2.0. It's very simple to use.
Using the code
There are one basic class myMail. This class has a method to setting SMTP and sends mail.
static class myMail
{
public static string sendMail(string to, string cc, string bcc, string subj, string msg)
{
SmtpClient smtp = new SmtpClient();
MailMessage message = new MailMessage();
string[] myCC;
string [] myBCC;
myCC = cc.Split (';');
myBCC = bcc.Split (';');
try
{
foreach (string ccCopy in myCC)
{
message.CC.Add(new MailAddress(ccCopy));
}
foreach (string bccHide in myBCC)
{
message.Bcc.Add(new MailAddress(bccHide));
}
message.To.Add(new MailAddress(to));
message.Subject = subj;
message.Body = msg;
smtp.Host = "teste"; smtp.Port = 25; message.From = new MailAddress("<a href="mailto:paulo.passos@xxxxxxxxxx.com.br">paulo.passos@xxxxxxxxxx.com.br</a>");
smtp.Send(message);
}
catch (SmtpException E)
{
return "Mail send failed with message: " + E.Message;
}
return "Mail was send";
}
}
private void btnSend_Click(object sender, EventArgs e)
{
MessageBox.Show (myMail.sendMail(txtTO.Text , txtCC.Text , txtBCC.Text , txtSubject.Text , rtMsg.Text));
}
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here