A few years ago, the enthusiasm of C #, wrote the C # send mail and C # sent with attachments to the mail article, put on Baidu. There have been put on the Ttlsa, but the search found that the article seems to have been deleted, today deliberately to add again. This article is kept as a record.
The following code includes sending attachments, in addition to implementing simple messages. The from map is not posted, with two buttons above, one "add Attachment" and one "send". Click Add Attachment Select File, file path is stored in ListBox1. In the Send button method, add all the listbox1 files to the MailMessage object as a message sent out, see the code
| The code is as follows |
Copy Code |
|
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using System.Net.Mail;
Using System.Net;
Using System.Net.Security;
Using System.IO;
Using System.Net.Mime;
Namespace Smtptest
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
Try
{
Define a Mail object
MailMessage mailmessage = new MailMessage ("", "", "This is a test", "yes!test!");
From email,to email, subject, email content
MailMessage. Priority = Mailpriority.normal; Message priority
SmtpClient smtpclient = new SmtpClient ("smtp.163.com", 25); SMTP address and port number
Smtpclient.credentials = new NetworkCredential ("ttlsa.com", "xxxxxx");//smtp username password
Smtpclient.enablessl = true; Enable SSL
Add attachment
Attachment attachment =null;
if (listbox1.items.count>0)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
String pathfilename = Listbox1.items[i]. ToString ();
String extname = Path.getextension (pathfilename). ToLower (); Get extension name
if (extname== ". rar" | | extname== ". zip")//.rar and. zip files belong to a compressed file type
{
Attachment = new Attachment (pathfilename,mediatypenames.application.zip);
}else
{
attachment = new Attachment (pathfilename,mediatypenames.application.octet);
}
//Set MIME information for attachments
Contentdisposition cd = attachment. Contentdisposition;
CD. CreationDate = File.getcreationtime (pathfilename);//Set the creation time for the attachment
CD. Modificationdate = File.getlastwritetime (pathfilename);//Set the modification time for the attachment
CD. Readdate = File.getlastaccesstime (pathfilename);//Set the access time for the attachment
MailMessage. Attachments.Add (attachment);//Add the attachment to the MailMessage object
}
}
Smtpclient.send (mailmessage);
MessageBox.Show ("Send Success");
}
catch (smtpexception se)
{
MessageBox.Show (SE). Statuscode.tostring ());
}
}
//Add attachments, add files to the listbox
private void Button2_Click (object sender, EventArgs e)
{
OpenFileDialog opd = new Ope Nfiledialog () A dialog box
OPD that defines a selection file. MultiSelect = true;//allows multiple files to be selected
OPD. Checkfileexists = true;//checks to see if the file exists
OPD. Validatenames = true;//Check the availability of the file name
OPD. ShowDialog ()//Opens the dialog box
if (OPD). FILENAMES.LENGTH>0)//writes the selected file path to the
{
ListBox1.Items.AddRange (OPD) in the listbox. FileNames);
}
}
}
}
|