Asp. NET send and receive mail

Source: Internet
Author: User
Tags types of extensions mailmessage net send

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

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.