In .net 1.1, how to send email using program control. For example, after a website user successfully registers, an email will be automatically sent to the registered user to prompt for the user's registration information. Furthermore, sometimes it is necessary to send a report to a few fixed people on a regular basis every day. Taken together, the following issues need to be addressed:
1. SMTP server identity authentication.
2. How to send when there are multiple recipients.
3. Send email attachments.
The implementation procedure is as follows:
In order to use System.Web.Mail, reference it in the namespace:
using System.Web.Mail;
Define a process for sending emails
/**//// <summary>
/// Send email message
/// </summary>
/// <param name="to">Receive email address</param>
/// <param name="subject">Email subject</param>
/// <param name="body">Email text</param>
/// <param name="mailatta">Attachment</param>
public void SendMail(string to,string subject,string body,MailAttachment mailatta)
{
MailMessage m_Mail = new MailMessage();
//Define the name of the SMTP server
SmtpMail.SmtpServer = "mail.13590.com";
//Define the SMTP mail server to require identity authentication
m_Mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate ", "1");
//Authenticated username
m_Mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendusername ", "username");
//Authentication password
m_Mail.Fields.Add(" http://schemas.microsoft.com/cdo/configuration/sendpassword","password ");
//Define the email sending address
m_Mail.From=""Qiu Yu"< [email protected] >";
//Define the email receiving address
m_Mail.To = to.ToString();
//Define the BCC address of the email
m_Mail.Bcc=" [email protected] ";
//Define the carbon copy address of the email
m_Mail.Cc = " [email protected] ";
//Define the subject of the email
m_Mail.Subject = subject;
//Define the body of the email
m_Mail.Body = body;
//'Email is sent in HTML format
m_Mail.BodyFormat = MailFormat.Text;
//Define the limited level of mail, set it to high here
m_Mail.Priority = MailPriority.High;
// Attach an attachment to the sent email
if (mailatta!=null)
{
m_Mail.Attachments.Add(mailatta);
}
try
{
SmtpMail.Send(m_Mail);
}
catch
{
//Error in sending email
Memo1.Text=Memo1.Text + subject;
}
}
Pay attention to the email address ""Qiu Yu"< [email protected] >";
The front is the displayed name, followed by the real email address. If the recipient address is multiple people, separate them with ";", for example:
"Qiu Yu" < [email protected] >; "Administrator" < [email protected] > Pay attention to the use of escape characters.
We use the program to call
private void button_Click(object sender, System.EventArgs e)
{
string MailAddr=""Qiu Yu"< [email protected]>; "Administrator"< [email protected] > ";
string Subject="Please check the statistical report";
string MailBody="Test";
string PathMailAtta=@"C: Statistical Report.xls";
MailAttachment mailatta;
try
{
mailatta= new MailAttachment(PathMailAtta);
}
catch
{
Memo1.Text=Memo1.Text+PathMailAtta+"There is an exception, check whether it is open";
return;
}
SendGroupMail(MailAddr,Subject,MailBody,mailatta);
}
The above program has been tested under VS.Net2003 and Windows 2003 (SP1) platforms.