In. NET common to the online e-mail instance, the site plus this feature can be convenient for webmasters and users to communicate.
NET sends Mail sometimes uses the mail server in the IIS component, but is complex. The configuration of the virtual host is also more cumbersome,
Third-party components such as JMail are also available, but. NET itself already encapsulates the class that sent the message: WebMail;
But first introduce this class: using system.web.mail;//import Mail Send class
Using system.io;//File Upload type
This class contains some common objects commonly used with the From, to,subject,body,attchments (attachment);
The specific code is as follows:
MailMessage objmail = new MailMessage ();
Objmail.from = Mailfrom.text;
objmail.to = Mailto.text;
Objmail.subject = Subject.text;
Objmail.body = Body.text;
Objmail.bodyformat = Mailformat.text;
Uploading attachments
string filename, filepath, filepic;
filepic = path.getextension (uploadfile. Postedfile.filename). ToLower ();
if (filepic = = ". exe")
{
Response.Write ("<script> Alert (' Sorry, the format is wrong! ') </script> ");
//response.end ();
}
filename = Path.getfilename (uploadfile. Postedfile.filename);
filepath = Server.MapPath ("uploadfiles/" + filename);
UploadFile. Postedfile.saveas (filepath);//Save the upload file to the server folder;
MailAttachment attach = new MailAttachment (filepath); Create an Attachment object based on the uploaded path;
OBJMAIL.ATTACHMENTS.ADD (attach);//Add an attachment instance
Smtpmail.smtpserver = "";
Smtpmail.send (objmail);
Message. Text = "Sent successfully!" :)";
}
When uploading attachments here, the extension of the attachment is checked, the suffix named. exe is an illegal upload format, and of course you can add more illegal formats;
Second: Upload files;
There are many ways to upload files, the common database to save the file path, the file is stored in the folder, the following gives me to upload files to the folder under the Code;
string filename, filepath, filepic;//definition file name, file path, file name extension;
Filepic = Path.getextension (uploadfile. Postedfile.filename). ToLower ();//Determine the file name to upload
if (Filepic = = ". exe" | | filepic = = ". rar" | | filepic== ". Rename")//To see if it is compatible with these types of extensions, if you want to prohibit a certain type, add it directly here;
{
Response.Write ("<script>alert (' Sorry, format is wrong! ') </script> ");
Response.End ();
}
Else
{
Try
{
filename = Path.getfilename (uploadfile. Postedfile.filename);
filepath = Server.MapPath ("uploadfiles/" + filename);
UploadFile. Postedfile.saveas (filepath);
Response.Write ("Save path:" + filepath + "<br>");
Response.Write ("File type:" + uploadfile. PostedFile.ContentType);
Message. InnerHtml = "<br><a href= ' uploadfiles/" + filename + "> View file </a>" + "<a href= ' uploadfiles.aspx ' > Back to </a> ";
}
catch (OleDbException er)
{
Response.Write ("The response time is too long, upload failed!! ");
}
}