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