C# 如何使用gmail和yahoo从mvc中的应用程序发送邮件?
这是我的应用程序,我需要在其中发送电子邮件。它可以与gmail一起使用。是否可以与yahoo mail一起使用?(如果from textbox具有@gmail.com,则必须转到smtp.gmail.com步进控制器;如果from textbox具有@yahoo.com,则必须转到smtp.mail.yahoo.com,是否可能?) 我的控制器:C# 如何使用gmail和yahoo从mvc中的应用程序发送邮件?,c#,asp.net-mvc,C#,Asp.net Mvc,这是我的应用程序,我需要在其中发送电子邮件。它可以与gmail一起使用。是否可以与yahoo mail一起使用?(如果from textbox具有@gmail.com,则必须转到smtp.gmail.com步进控制器;如果from textbox具有@yahoo.com,则必须转到smtp.mail.yahoo.com,是否可能?) 我的控制器: using emailtest1.Models; using System.IO; using System.Net; us
using emailtest1.Models;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web.Mvc;
namespace emailtest1.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmailModel model)
{
using (MailMessage mm = new MailMessage(model.Email, model.To))
{
mm.Subject = model.Subject;
mm.Body = model.Body;
if (model.Attachment.ContentLength > 0)
{
string fileName = Path.GetFileName(model.Attachment.FileName);
mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName));
}
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Host = "smtp.mail.yahoo.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Port = 25;
smtp.Send(mm);
ViewBag.Message = "Email sent.";
}
}
return View();
}
}
}
using System.Web;
namespace emailtest1.Models
{
public class EmailModel
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public HttpPostedFileBase Attachment { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
@model emailtest1.Models.EmailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
table th, table td {
padding: 5px;
}
</style>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 80px">
To:
</td>
<td>
@Html.TextBoxFor(model => model.To)
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
@Html.TextBoxFor(model => model.Subject)
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
@Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
</td>
</tr>
<tr>
<td>
File Attachment:
</td>
<td>
@Html.TextBoxFor(model => model.Attachment, new { type = "file" })
</td>
</tr>
<tr>
<td>
Gmail:
</td>
<td>
@Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { id = "my_custom_id" } })
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
@*@Html.TextBoxFor(model=>model.Password)*@
@Html.TextBoxFor(model => model.Password, new { type = "password" })
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send" />
</td>
</tr>
</table>
<br />
<span style="color:green">@ViewBag.Message</span>
}
</div>
</body>
</html>
我的型号:
using emailtest1.Models;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web.Mvc;
namespace emailtest1.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmailModel model)
{
using (MailMessage mm = new MailMessage(model.Email, model.To))
{
mm.Subject = model.Subject;
mm.Body = model.Body;
if (model.Attachment.ContentLength > 0)
{
string fileName = Path.GetFileName(model.Attachment.FileName);
mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName));
}
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Host = "smtp.mail.yahoo.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Port = 25;
smtp.Send(mm);
ViewBag.Message = "Email sent.";
}
}
return View();
}
}
}
using System.Web;
namespace emailtest1.Models
{
public class EmailModel
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public HttpPostedFileBase Attachment { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
@model emailtest1.Models.EmailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
table th, table td {
padding: 5px;
}
</style>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 80px">
To:
</td>
<td>
@Html.TextBoxFor(model => model.To)
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
@Html.TextBoxFor(model => model.Subject)
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
@Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
</td>
</tr>
<tr>
<td>
File Attachment:
</td>
<td>
@Html.TextBoxFor(model => model.Attachment, new { type = "file" })
</td>
</tr>
<tr>
<td>
Gmail:
</td>
<td>
@Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { id = "my_custom_id" } })
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
@*@Html.TextBoxFor(model=>model.Password)*@
@Html.TextBoxFor(model => model.Password, new { type = "password" })
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send" />
</td>
</tr>
</table>
<br />
<span style="color:green">@ViewBag.Message</span>
}
</div>
</body>
</html>
我的观点:
using emailtest1.Models;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Web.Mvc;
namespace emailtest1.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmailModel model)
{
using (MailMessage mm = new MailMessage(model.Email, model.To))
{
mm.Subject = model.Subject;
mm.Body = model.Body;
if (model.Attachment.ContentLength > 0)
{
string fileName = Path.GetFileName(model.Attachment.FileName);
mm.Attachments.Add(new Attachment(model.Attachment.InputStream, fileName));
}
mm.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.gmail.com";
smtp.Host = "smtp.mail.yahoo.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
smtp.UseDefaultCredentials = false;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Port = 25;
smtp.Send(mm);
ViewBag.Message = "Email sent.";
}
}
return View();
}
}
}
using System.Web;
namespace emailtest1.Models
{
public class EmailModel
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public HttpPostedFileBase Attachment { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
@model emailtest1.Models.EmailModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
table th, table td {
padding: 5px;
}
</style>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 80px">
To:
</td>
<td>
@Html.TextBoxFor(model => model.To)
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
@Html.TextBoxFor(model => model.Subject)
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
@Html.TextAreaFor(model => model.Body, new { rows = "3", cols = "20" })
</td>
</tr>
<tr>
<td>
File Attachment:
</td>
<td>
@Html.TextBoxFor(model => model.Attachment, new { type = "file" })
</td>
</tr>
<tr>
<td>
Gmail:
</td>
<td>
@Html.TextBoxFor(model => model.Email, new { htmlAttributes = new { id = "my_custom_id" } })
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
@*@Html.TextBoxFor(model=>model.Password)*@
@Html.TextBoxFor(model => model.Password, new { type = "password" })
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Send" />
</td>
</tr>
</table>
<br />
<span style="color:green">@ViewBag.Message</span>
}
</div>
</body>
</html>
@model-emailtest1.Models.EmailModel
@{
布局=空;
}
指数
身体{
字体系列:Arial;
字号:10pt;
}
表th、表td{
填充物:5px;
}
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
致:
@Html.TextBoxFor(model=>model.To)
主题:
@Html.TextBoxFor(model=>model.Subject)
正文:
@Html.TextAreaFor(model=>model.Body,新的{rows=“3”,cols=“20”})
文件附件:
@Html.TextBoxFor(model=>model.Attachment,新的{type=“file”})
Gmail:
@Html.TextBoxFor(model=>model.Email,new{htmlAttributes=new{id=“my\u custom\u id”})
Gmail密码:
@*@Html.TextBoxFor(model=>model.Password)*@
@Html.TextBoxFor(model=>model.Password,新的{type=“Password”})
@查看包。留言
}
不能逐个重写主机和端口属性。你必须提出一个条件:
if(model.Email.contains("@gmail.com")){
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
} else if(model.Email.contains("@yahoo.fr")) {
smtp.Host = "smtp.mail.yahoo.com";
smtp.Port = 587;
}
是的,但不是像你做的那样,当你做:
smtp.Host = "smtp.gmail.com";
smtp.Host = "smtp.mail.yahoo.com";
您正在覆盖SMTP
你能做的就是这样
smtp.Port = 587;
if (textbox.value.contains("@gmail.com"))
smtp.Host = "smtp.gmail.com";
ELSE (textbox.value.contains("@yahoo.com"))
smtp.Host = "smtp.mail.yahoo.com";
在那之后,做工作……澄清一下,是与雅虎发送电子邮件的问题,还是“我如何做到两者”的问题?如果是这样,您是否为您编写的语句尝试了if/else条件?(
如果文本框中有@gmail.com,则必须转到smtp.gmail.com,如果文本框中有@yahoo.com,则必须转到smtp.mail.yahoo.com
)?@ashwanth你能解决这个话题吗?是的,我找到了解决方案@GGO谢谢你能解决这个话题(勾选绿色勾号)?对你帮助最大的答案