asp.net C # using SMTP to send an attachment program

Source: Internet
Author: User
Tags rar zip mailmessage port number smtpclient

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);
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.