Practical webmail for ASP. net mvc 3 beta

Source: Internet
Author: User

Asp.net MVC 3 beta provides a very useful component for sending emails: webmail. I tried it, similar to system. Web. Mail. This article will briefly introduce the use of this component. This section describes two scenarios: sending emails without attachments and sending emails with attachments. An Application Scenario for requesting help is used as an example.

Emails without attachments

First, define the controller. Emailrequest is used to request a page for sending an email. processrequest is used to process the request for sending the email and send the email in view.

Code

         [HttpGet]
public ActionResult EmailRequest()
{
return View();
}

[HttpPost]
public ActionResult ProcessRequest()
{
return View();
}

The emailrequest. cshtml code is as follows:

Code

 <! Doctype HTML>
<HTML>
<Head>
<Title> help center </title> <H2> send email for help </H2>
<Form method = "Post" Action = "processrequest">
<Div> your name:
<Input type = "text" name = "customername"/>
</Div>
<Div> Your Problem description: <br/>
<Textarea name = "customerrequest" Cols = "45" rows = "4">
</Textarea>
</Div>
<Div>
<Input type = "Submit" value = "Submit"/>
</Div>
</Form>
</Body>
</Html>

View of the sent Email:

 @{
VaR customername = request ["customername"];
VaR customerrequest = request ["customerrequest"];
Try
{
// Initialization
Webmail. smtpserver = "smtp.126.com ";
Webmail. smtpport = 25;
Webmail. enablessl = false;
Webmail. Username = "zhuqi0 ";
Webmail. From = "zhuqi0@126.com ";
Webmail. Password = "**********";
// Send an email
Webmail. Send (to: "zhuqi0@126.com ",
Subject: "Help From-" + customername + ",
Body: customerrequest
);
}
Catch (exception ex)
{
<Text>
<B> email sending <em> failed </em>. </B>
The Code does not provide the correct SMTP service name, user name, password, and other information.
</Text>
}
}
<! Doctype HTML>
<HTML> <Title> help center </title> <P> sorry to hear you have trouble,
<B> @ customername </B>.
</P>
<P> the email about the following problem has been sent to our Customer Service, and relevant departments will promptly handle it. </P>
<P> <B> @ customerrequest </B> </P> </body>

Run:

Sent successfully

Email Notification:

Email sending with attachments:

Email sending with attachments is similar, but you need to know the list of additional addresses. The email sending code with attachments is as follows:

 @{
VaR customername = request ["customername"];
VaR subjectline = request ["subjectline"];
VaR fileattachment = request ["fileattachment"];
Try {
// Initialization
Webmail. smtpserver = "smtp.126.com ";
Webmail. smtpport = 25;
Webmail. enablessl = false;
Webmail. Username = "zhuqi0 ";
Webmail. From = "zhuqi0@126.com ";
Webmail. Password = "**********";
// Create an array containing attachments
VaR fileslist = new string [] {fileattachment };
// Add attachments and send emails
Webmail. Send (to: "zhuqi0@126.com", Subject: subjectline,
Body: "file attached. <br/> from:" + customername,
Filestoattach: fileslist );
}
Catch (exception ex)
{
<Text>
<B> email sending <em> failed </em>. </B>
The Code does not provide the correct SMTP service name, user name, password, and other information.
</Text>
}
}
<! Doctype HTML>
<HTML>
<Head>
<Title> help center </title>
</Head>
<Body>
<P> <B> @ customername </B>. Thank you for your support. </P> <p> the email about the following problem has been sent to our Customer Service. Relevant departments will promptly handle the problem. <B>
@ Fileattachment </B>
File attached. </P>
</Body>
</Html>

From the above two cases, we can see that webmail and System. Web. Mail use the same method, but it is easier to use webmail in Asp.net MVC 3 beta.

Step 1: Initialize the email sending server.

Webmail. smtpserver = "smtp.126.com ";

Step 2: Specify the port.

Webmail. enablessl = false;

Step 3: Specify the user name.

Webmail. Username = "zhuqi0 ";

Step 4: your email address and password.

Webmail. From = "zhuqi0@126.com ";
Webmail. Password = "********";

Step 5: if an attachment exists, specify the attachment address.

VaR fileslist = new string [] {fileattachment };

Step 6: send an email.

Webmail. Send (to: "zhuqi0@126.com", Subject: subjectline,
Body: "file attached. <br/> from:" + customername,
Filestoattach: fileslist );

Summary:This article briefly introduces the use of webmail in ASP. net mvc 3 beta.

Code: http://files.cnblogs.com/zhuqil/MvcApplicationWebMail.rar

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.