I'm very new to the ASP.NET C# area. I'm planning to send a mail through ASP.NET C# and this is the SMTP address from my ISP:
我对ASP很陌生。c#净面积。我计划通过ASP发送邮件。这是我的ISP的SMTP地址:
smtp-proxy.tm.net.my
Below is what I tried to do, but failed.
下面是我试图做的,但失败了。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SendMail" %>
<html>
<head id="Head1" runat="server"><title>Email Test Page</title></head>
<body>
<form id="form1" runat="server">
Message to: <asp:TextBox ID="txtTo" runat="server" /><br>
Message from: <asp:TextBox ID="txtFrom" runat="server" /><br>
Subject: <asp:TextBox ID="txtSubject" runat="server" /><br>
Message Body:<br>
<asp:TextBox ID="txtBody" runat="server" Height="171px" TextMode="MultiLine" Width="270px" /><br>
<asp:Button ID="Btn_SendMail" runat="server" onclick="Btn_SendMail_Click" Text="Send Email" /><br>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
And below is my code-behind:
下面是我的代码:
using System;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class SendMail : System.Web.UI.Page
{
protected void Btn_SendMail_Click(object sender, EventArgs e)
{
MailMessage mailObj = new MailMessage(
txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
SmtpClient SMTPServer = new SmtpClient("127.0.0.1");
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
}
}
PS: I'm sorry that I couldn't understand the receiver/sender SMTP concept, and so I am trying to understand the whole concept from here.
PS:很抱歉,我不能理解接收者/发送者SMTP的概念,所以我试图从这里了解整个概念。
9 个解决方案
#1
83
Just go through the below code.
请浏览下面的代码。
SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//Setting From , To and CC
mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");
mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));
mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
smtpClient.Send(mail);
#2
21
Try using this code instead:
请尝试使用以下代码:
protected void btn_send_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("to gmail address");
mail.From = new MailAddress("from gmail address", "Email head", System.Text.Encoding.UTF8);
mail.Subject = "This mail is send from asp.net application";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("from gmail address", "your gmail account password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mail);
Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
}
}
Note: In "from address" give your correct email id and password. If you have any doubts contact me.
注意:在“from address”中给出正确的电子邮件id和密码。如果你有任何疑问,请联系我。
#3
10
You can try this using hotmail like this:-
你可以这样使用hotmail:-。
MailMessage o = new MailMessage("From", "To","Subject", "Body");
NetworkCredential netCred= new NetworkCredential("Sender Email","Sender Password");
SmtpClient smtpobj= new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);
#4
8
Try the following :
试试以下:
try
{
var fromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
var fromEmailDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToString();
var fromEmailPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToString();
var smtpHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
var smtpPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();
string body = "Your registration has been done successfully. Thank you.";
MailMessage message = new MailMessage(new MailAddress(fromEmailAddress, fromEmailDisplayName), new MailAddress(ud.LoginId, ud.FullName));
message.Subject = "Thank You For Your Registration";
message.IsBodyHtml = true;
message.Body = body;
var client = new SmtpClient();
client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword);
client.Host = smtpHost;
client.EnableSsl = true;
client.Port = !string.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0;
client.Send(message);
}
catch (Exception ex)
{
throw (new Exception("Mail send failed to loginId " + ud.LoginId + ", though registration done."));
}
And then in you web.config add the followings in between
然后在你的网络中。配置在中间添加以下内容。
<!--Email Config-->
<add key="FromEmailAddress" value="sender emailaddress"/>
<add key="FromEmailDisplayName" value="Display Name"/>
<add key="FromEmailPassword" value="sender Password"/>
<add key="SMTPHost" value="smtp-proxy.tm.net.my"/>
<add key="SMTPPort" value="smptp Port"/>
#5
2
Check this out .... it works
看看这个....它的工作原理
http://www.aspnettutorials.com/tutorials/email/email-aspnet2-csharp/
http://www.aspnettutorials.com/tutorials/email/email-aspnet2-csharp/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(message);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
}
#6
1
This just works perfectly for me.
这对我来说是完美的。
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("test@gmail.com");
mail.From = new MailAddress("abc@gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yourgmail@gmail.com", "Yourpassword"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
#7
0
MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)//file upload select or not
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
Response.write("Send Mail");
View Video: https://www.youtube.com/watch?v=bUUNv-19QAI
视图的视频:https://www.youtube.com/watch?v=bUUNv-19QAI
#8
0
If you want to generate your email bodies in razor, you can use Mailzory. Also, you can download the nuget package from here.
如果你想用razor生成电子邮件,你可以使用Mailzory。另外,您可以从这里下载nuget包。
// template path
var viewPath = Path.Combine("Views/Emails", "hello.cshtml");
// read the content of template and pass it to the Email constructor
var template = File.ReadAllText(viewPath);
var email = new Email(template);
// set ViewBag properties
email.ViewBag.Name = "Johnny";
email.ViewBag.Content = "Mailzory Is Funny";
// send email
var task = email.SendAsync("mailzory@outlook.com", "subject");
task.Wait()
#9
0
This is the easiest script to test.
这是最容易测试的脚本。
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("From email account");
mail.To.Add("To email account");
//set the content
mail.Subject = "This is a test email from C# script";
mail.Body = "This is a test email from C# script";
//send the message
SmtpClient smtp = new SmtpClient("mail.domainname.com");
NetworkCredential Credentials = new NetworkCredential("to email account", "Password");
smtp.Credentials = Credentials;
smtp.Send(mail);
lblMessage.Text = "Mail Sent";
}
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
#1
83
Just go through the below code.
请浏览下面的代码。
SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//Setting From , To and CC
mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");
mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));
mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
smtpClient.Send(mail);
#2
21
Try using this code instead:
请尝试使用以下代码:
protected void btn_send_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add("to gmail address");
mail.From = new MailAddress("from gmail address", "Email head", System.Text.Encoding.UTF8);
mail.Subject = "This mail is send from asp.net application";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("from gmail address", "your gmail account password");
client.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mail);
Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Exception ex2 = ex;
string errorMessage = string.Empty;
while (ex2 != null)
{
errorMessage += ex2.ToString();
ex2 = ex2.InnerException;
}
Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
}
}
Note: In "from address" give your correct email id and password. If you have any doubts contact me.
注意:在“from address”中给出正确的电子邮件id和密码。如果你有任何疑问,请联系我。
#3
10
You can try this using hotmail like this:-
你可以这样使用hotmail:-。
MailMessage o = new MailMessage("From", "To","Subject", "Body");
NetworkCredential netCred= new NetworkCredential("Sender Email","Sender Password");
SmtpClient smtpobj= new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);
#4
8
Try the following :
试试以下:
try
{
var fromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"].ToString();
var fromEmailDisplayName = ConfigurationManager.AppSettings["FromEmailDisplayName"].ToString();
var fromEmailPassword = ConfigurationManager.AppSettings["FromEmailPassword"].ToString();
var smtpHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
var smtpPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();
string body = "Your registration has been done successfully. Thank you.";
MailMessage message = new MailMessage(new MailAddress(fromEmailAddress, fromEmailDisplayName), new MailAddress(ud.LoginId, ud.FullName));
message.Subject = "Thank You For Your Registration";
message.IsBodyHtml = true;
message.Body = body;
var client = new SmtpClient();
client.Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword);
client.Host = smtpHost;
client.EnableSsl = true;
client.Port = !string.IsNullOrEmpty(smtpPort) ? Convert.ToInt32(smtpPort) : 0;
client.Send(message);
}
catch (Exception ex)
{
throw (new Exception("Mail send failed to loginId " + ud.LoginId + ", though registration done."));
}
And then in you web.config add the followings in between
然后在你的网络中。配置在中间添加以下内容。
<!--Email Config-->
<add key="FromEmailAddress" value="sender emailaddress"/>
<add key="FromEmailDisplayName" value="Display Name"/>
<add key="FromEmailPassword" value="sender Password"/>
<add key="SMTPHost" value="smtp-proxy.tm.net.my"/>
<add key="SMTPPort" value="smptp Port"/>
#5
2
Check this out .... it works
看看这个....它的工作原理
http://www.aspnettutorials.com/tutorials/email/email-aspnet2-csharp/
http://www.aspnettutorials.com/tutorials/email/email-aspnet2-csharp/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
emailClient.Send(message);
litStatus.Text = "Message Sent";
}
catch (Exception ex)
{
litStatus.Text=ex.ToString();
}
}
}
#6
1
This just works perfectly for me.
这对我来说是完美的。
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("test@gmail.com");
mail.From = new MailAddress("abc@gmail.com");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("yourgmail@gmail.com", "Yourpassword"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
#7
0
MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text);
mm.Subject = txtSubject.Text;
mm.Body = txtBody.Text;
if (fuAttachment.HasFile)//file upload select or not
{
string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
}
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
Response.write("Send Mail");
View Video: https://www.youtube.com/watch?v=bUUNv-19QAI
视图的视频:https://www.youtube.com/watch?v=bUUNv-19QAI
#8
0
If you want to generate your email bodies in razor, you can use Mailzory. Also, you can download the nuget package from here.
如果你想用razor生成电子邮件,你可以使用Mailzory。另外,您可以从这里下载nuget包。
// template path
var viewPath = Path.Combine("Views/Emails", "hello.cshtml");
// read the content of template and pass it to the Email constructor
var template = File.ReadAllText(viewPath);
var email = new Email(template);
// set ViewBag properties
email.ViewBag.Name = "Johnny";
email.ViewBag.Content = "Mailzory Is Funny";
// send email
var task = email.SendAsync("mailzory@outlook.com", "subject");
task.Wait()
#9
0
This is the easiest script to test.
这是最容易测试的脚本。
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("From email account");
mail.To.Add("To email account");
//set the content
mail.Subject = "This is a test email from C# script";
mail.Body = "This is a test email from C# script";
//send the message
SmtpClient smtp = new SmtpClient("mail.domainname.com");
NetworkCredential Credentials = new NetworkCredential("to email account", "Password");
smtp.Credentials = Credentials;
smtp.Send(mail);
lblMessage.Text = "Mail Sent";
}
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>