![]() |
General Programming »
Internet / Network »
Email
Intermediate
Windows Email Client application using .NET (C#)By Fabian TangAn article on a Windows email client application using .NET. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article is to show the reader how easy it is to send an email using the .NET framework. Not only can the user determine the format of the email (HTML or text), non-ASCII characters can also be sent with relatively simple coding. A Windows email client application along with the source code can be found in this article.
Some configuration needs to be done before you can make use of the application.
In order to make use of the mail services from .NET, you need to have a reference to System.Web in your solution explorer. This reference is added to the ASP.NET application by default but is not present when you create a Windows application.
First, you need to create a MailMessage object:
MailMessage mm = new MailMessage();
Next, you need to set the members of the MailMessage object you created:
//Setting the fields of the emails
mm.To = toText.Text;
mm.From = fromText.Text;
mm.Cc = ccText.Text;
mm.Bcc = bccText.Text;
mm.Subject = subjectText.Text;
//The body of the message text
mm.Body = messageText.Text;
//The Priority of the mail
switch (settingDialog.PrEnum)
{
case Priorities.High:
mm.Priority = MailPriority.High;
break;
case Priorities.Normal:
mm.Priority = MailPriority.Normal;
break;
case Priorities.Low:
mm.Priority = MailPriority.Low;
break;
default:
break;
}
//The encoding format of the mail
switch (settingDialog.EdEnum)
{
case Encodings.ASCII:
mm.BodyEncoding =Encoding.ASCII;
break;
case Encodings.Unicode:
mm.BodyEncoding =Encoding.Unicode;
break;
case Encodings.UTF7:
mm.BodyEncoding =Encoding.UTF7;
break;
case Encodings.UTF8:
mm.BodyEncoding =Encoding.UTF8;
break;
default:
break;
}
//Format of mail is in HTML format
mm.BodyFormat = MailFormat.Html;
if (!attachText.Text.Equals(""))
{
//adding attachments.
//You can add multiple attachments if you want
MailAttachment ma = new MailAttachment(attachText.Text);
if (ma != null)
{
mm.Attachments.Add(ma);
}
}
To send the mail, make use of the Send method:
SmtpMail.Send(mm);
As you can see, to send a mail with .NET is really that simple.
This is a known issue with .NET. The priority will work with Microsoft Outlook but not with Outlook Express.
Make sure you follow point #2 in the section �Configuring the PC� above.
Make sure you are not behind any firewall that disrupts the sending of the mail. Make sure your port 25 is not blocked. You can check if there are any bad mails or mails under queue in your mailroot directory. Usually, this folder can be found in �C:\Inetpub\mailroot�.
Make sure there isn�t any mail under the queue in your mailroot directory. You may also want to make sure that the string in �SmtpMail.SmtpServer� is referring to the localhost.
You may want to use UTF8 encoding instead. Check if the email is being stored in the queue folder in your mailroot directory. You may want to open the .eml attachment to see if the Unicode message is being encoded correctly.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 Dec 2005 Editor: Smitha Vijayan |
Copyright 2005 by Fabian Tang Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |