ASP.Net 邮箱发送

邮件发送的页面

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="bootstrap-3.3.7/css/bootstrap.css" rel="stylesheet" />
<script src="Scripts/jquery-3.4.1.min.js"></script>
<script src="Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
function SendMails() {
var obj = {
RecMails:$("#txt_RevMail").val(),
Title:$("#txt_Title").val(),
BodyContent:$("#ta_Content").val(),
};
$.ajax({
url: "http://localhost:57820/api/Mail/SendMailMethod",
dataType: "json",
type: "post",
data: obj,
success: function (data) {
if (data > 0) {
alert("发送成功!");
}
}
})
}
</script>
</head>
<body>
<div align="center">
<table class="table table-bordered" style="width:50%;">
<tr>
<th>标题:</th>
<td><input id="txt_Title" type="text" /></td>
</tr>
<tr>
<th>收件人:</th>
<td><input id="txt_RevMail" type="text" /></td>
</tr>
<tr>
<th>正文:</th>
<td><textarea id="ta_Content" rows="4" cols="40"></textarea></td>
</tr>
<tr>
<td colspan="2"><input id="btn_Send" type="button" class="btn btn-primary" value="发送邮件" onclick="SendMails()" /></td>
</tr>
</table>
</div>
</body>
</html>

 

邮件发送的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using G6.WeekTwo.Models;
namespace G6.WeekTwo.Controllers
{
public class MailController : ApiController
{
[HttpPost]
public int SendMailMethod(EmailModel model)
{
//List<string> list = new List<string>() {
// "691805617@qq.com",
// "31900477@qq.com"
//};
try
{
string[] arr = null;
if(model.RecMails.Contains(","))
{
arr = model.RecMails.Split(',');
}else
{
arr = new string[] { model.RecMails };
}
SendMail mail = new SendMail(arr, "31900477@qq.com", model.BodyContent, model.Title, "sfeamvgjafykbgdh");
mail.Send();
return 1;
}
catch (Exception ex)
{
return 0;
}
}
}
}

邮件发送的类

 

请在这个链接查询

https://www.cnblogs.com/freedomlan/p/13184891.html

posted @ 2020-06-26 20:56  我是一只快乐的码农  阅读(142)  评论(0编辑  收藏  举报